Indirect.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
  3. use Exception;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  5. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  6. use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
  7. use PhpOffice\PhpSpreadsheet\Cell\Cell;
  8. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  9. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  10. class Indirect
  11. {
  12. /**
  13. * Determine whether cell address is in A1 (true) or R1C1 (false) format.
  14. *
  15. * @param mixed $a1fmt Expect bool Helpers::CELLADDRESS_USE_A1 or CELLADDRESS_USE_R1C1,
  16. * but can be provided as numeric which is cast to bool
  17. */
  18. private static function a1Format($a1fmt): bool
  19. {
  20. $a1fmt = Functions::flattenSingleValue($a1fmt);
  21. if ($a1fmt === null) {
  22. return Helpers::CELLADDRESS_USE_A1;
  23. }
  24. if (is_string($a1fmt)) {
  25. throw new Exception(ExcelError::VALUE());
  26. }
  27. return (bool) $a1fmt;
  28. }
  29. /**
  30. * Convert cellAddress to string, verify not null string.
  31. *
  32. * @param array|string $cellAddress
  33. */
  34. private static function validateAddress($cellAddress): string
  35. {
  36. $cellAddress = Functions::flattenSingleValue($cellAddress);
  37. if (!is_string($cellAddress) || !$cellAddress) {
  38. throw new Exception(ExcelError::REF());
  39. }
  40. return $cellAddress;
  41. }
  42. /**
  43. * INDIRECT.
  44. *
  45. * Returns the reference specified by a text string.
  46. * References are immediately evaluated to display their contents.
  47. *
  48. * Excel Function:
  49. * =INDIRECT(cellAddress, bool) where the bool argument is optional
  50. *
  51. * @param array|string $cellAddress $cellAddress The cell address of the current cell (containing this formula)
  52. * @param mixed $a1fmt Expect bool Helpers::CELLADDRESS_USE_A1 or CELLADDRESS_USE_R1C1,
  53. * but can be provided as numeric which is cast to bool
  54. * @param Cell $cell The current cell (containing this formula)
  55. *
  56. * @return array|string An array containing a cell or range of cells, or a string on error
  57. */
  58. public static function INDIRECT($cellAddress, $a1fmt, Cell $cell)
  59. {
  60. [$baseCol, $baseRow] = Coordinate::indexesFromString($cell->getCoordinate());
  61. try {
  62. $a1 = self::a1Format($a1fmt);
  63. $cellAddress = self::validateAddress($cellAddress);
  64. } catch (Exception $e) {
  65. return $e->getMessage();
  66. }
  67. [$cellAddress, $worksheet, $sheetName] = Helpers::extractWorksheet($cellAddress, $cell);
  68. if (preg_match('/^' . Calculation::CALCULATION_REGEXP_COLUMNRANGE_RELATIVE . '$/miu', $cellAddress, $matches)) {
  69. $cellAddress = self::handleRowColumnRanges($worksheet, ...explode(':', $cellAddress));
  70. } elseif (preg_match('/^' . Calculation::CALCULATION_REGEXP_ROWRANGE_RELATIVE . '$/miu', $cellAddress, $matches)) {
  71. $cellAddress = self::handleRowColumnRanges($worksheet, ...explode(':', $cellAddress));
  72. }
  73. try {
  74. [$cellAddress1, $cellAddress2, $cellAddress] = Helpers::extractCellAddresses($cellAddress, $a1, $cell->getWorkSheet(), $sheetName, $baseRow, $baseCol);
  75. } catch (Exception $e) {
  76. return ExcelError::REF();
  77. }
  78. if (
  79. (!preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/miu', $cellAddress1, $matches)) ||
  80. (($cellAddress2 !== null) && (!preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/miu', $cellAddress2, $matches)))
  81. ) {
  82. return ExcelError::REF();
  83. }
  84. return self::extractRequiredCells($worksheet, $cellAddress);
  85. }
  86. /**
  87. * Extract range values.
  88. *
  89. * @return mixed Array of values in range if range contains more than one element.
  90. * Otherwise, a single value is returned.
  91. */
  92. private static function extractRequiredCells(?Worksheet $worksheet, string $cellAddress)
  93. {
  94. return Calculation::getInstance($worksheet !== null ? $worksheet->getParent() : null)
  95. ->extractCellRange($cellAddress, $worksheet, false);
  96. }
  97. private static function handleRowColumnRanges(?Worksheet $worksheet, string $start, string $end): string
  98. {
  99. // Being lazy, we're only checking a single row/column to get the max
  100. if (ctype_digit($start) && $start <= 1048576) {
  101. // Max 16,384 columns for Excel2007
  102. $endColRef = ($worksheet !== null) ? $worksheet->getHighestDataColumn((int) $start) : 'XFD';
  103. return "A{$start}:{$endColRef}{$end}";
  104. } elseif (ctype_alpha($start) && strlen($start) <= 3) {
  105. // Max 1,048,576 rows for Excel2007
  106. $endRowRef = ($worksheet !== null) ? $worksheet->getHighestDataRow($start) : 1048576;
  107. return "{$start}1:{$end}{$endRowRef}";
  108. }
  109. return "{$start}:{$end}";
  110. }
  111. }