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