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
|
85 |
|
private static function a1Format(mixed $a1fmt): bool |
23
|
|
|
{ |
24
|
85 |
|
$a1fmt = Functions::flattenSingleValue($a1fmt); |
25
|
85 |
|
if ($a1fmt === null) { |
26
|
37 |
|
return Helpers::CELLADDRESS_USE_A1; |
27
|
|
|
} |
28
|
48 |
|
if (is_string($a1fmt)) { |
29
|
2 |
|
throw new Exception(ExcelError::VALUE()); |
30
|
|
|
} |
31
|
|
|
|
32
|
46 |
|
return (bool) $a1fmt; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Convert cellAddress to string, verify not null string. |
37
|
|
|
*/ |
38
|
83 |
|
private static function validateAddress(array|string|null $cellAddress): string |
39
|
|
|
{ |
40
|
83 |
|
$cellAddress = Functions::flattenSingleValue($cellAddress); |
41
|
83 |
|
if (!is_string($cellAddress) || !$cellAddress) { |
42
|
1 |
|
throw new Exception(ExcelError::REF()); |
43
|
|
|
} |
44
|
|
|
|
45
|
82 |
|
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
|
85 |
|
public static function INDIRECT($cellAddress, mixed $a1fmt, Cell $cell): string|array |
65
|
|
|
{ |
66
|
85 |
|
[$baseCol, $baseRow] = Coordinate::indexesFromString($cell->getCoordinate()); |
67
|
|
|
|
68
|
|
|
try { |
69
|
85 |
|
$a1 = self::a1Format($a1fmt); |
70
|
83 |
|
$cellAddress = self::validateAddress($cellAddress); |
71
|
3 |
|
} catch (Exception $e) { |
72
|
3 |
|
return $e->getMessage(); |
73
|
|
|
} |
74
|
|
|
|
75
|
82 |
|
[$cellAddress, $worksheet, $sheetName] = Helpers::extractWorksheet($cellAddress, $cell); |
76
|
|
|
|
77
|
82 |
|
if (preg_match('/^' . Calculation::CALCULATION_REGEXP_COLUMNRANGE_RELATIVE . '$/miu', $cellAddress, $matches)) { |
78
|
3 |
|
$cellAddress = self::handleRowColumnRanges($worksheet, ...explode(':', $cellAddress)); |
79
|
80 |
|
} elseif (preg_match('/^' . Calculation::CALCULATION_REGEXP_ROWRANGE_RELATIVE . '$/miu', $cellAddress, $matches)) { |
80
|
3 |
|
$cellAddress = self::handleRowColumnRanges($worksheet, ...explode(':', $cellAddress)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
try { |
84
|
82 |
|
[$cellAddress1, $cellAddress2, $cellAddress] = Helpers::extractCellAddresses($cellAddress, $a1, $cell->getWorkSheet(), $sheetName, $baseRow, $baseCol); |
85
|
14 |
|
} catch (Exception) { |
86
|
14 |
|
return ExcelError::REF(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ( |
90
|
80 |
|
(!preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/miu', $cellAddress1, $matches)) |
91
|
80 |
|
|| (($cellAddress2 !== null) && (!preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/miu', $cellAddress2, $matches))) |
92
|
|
|
) { |
93
|
8 |
|
return ExcelError::REF(); |
94
|
|
|
} |
95
|
|
|
|
96
|
72 |
|
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
|
72 |
|
private static function extractRequiredCells(?Worksheet $worksheet, string $cellAddress): array |
106
|
|
|
{ |
107
|
72 |
|
return Calculation::getInstance($worksheet !== null ? $worksheet->getParent() : null) |
108
|
72 |
|
->extractCellRange($cellAddress, $worksheet, false); |
109
|
|
|
} |
110
|
|
|
|
111
|
5 |
|
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
|
5 |
|
if (ctype_digit($start) && $start <= 1_048_576) { |
|
|
|
|
115
|
|
|
// Max 16,384 columns for Excel2007 |
116
|
3 |
|
$endColRef = ($worksheet !== null) ? $worksheet->getHighestDataColumn((int) $start) : AddressRange::MAX_COLUMN; |
117
|
|
|
|
118
|
3 |
|
return "A{$start}:{$endColRef}{$end}"; |
119
|
3 |
|
} elseif (ctype_alpha($start) && strlen($start) <= 3) { |
120
|
|
|
// Max 1,048,576 rows for Excel2007 |
121
|
3 |
|
$endRowRef = ($worksheet !== null) ? $worksheet->getHighestDataRow($start) : AddressRange::MAX_ROW; |
122
|
|
|
|
123
|
3 |
|
return "{$start}1:{$end}{$endRowRef}"; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return "{$start}:{$end}"; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|