1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\AddressHelper; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
10
|
|
|
|
11
|
|
|
class Address |
12
|
|
|
{ |
13
|
|
|
use ArrayEnabled; |
14
|
|
|
|
15
|
|
|
public const ADDRESS_ABSOLUTE = 1; |
16
|
|
|
public const ADDRESS_COLUMN_RELATIVE = 2; |
17
|
|
|
public const ADDRESS_ROW_RELATIVE = 3; |
18
|
|
|
public const ADDRESS_RELATIVE = 4; |
19
|
|
|
|
20
|
|
|
public const REFERENCE_STYLE_A1 = true; |
21
|
|
|
public const REFERENCE_STYLE_R1C1 = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* ADDRESS. |
25
|
|
|
* |
26
|
|
|
* Creates a cell address as text, given specified row and column numbers. |
27
|
|
|
* |
28
|
|
|
* Excel Function: |
29
|
|
|
* =ADDRESS(row, column, [relativity], [referenceStyle], [sheetText]) |
30
|
|
|
* |
31
|
|
|
* @param mixed $row Row number (integer) to use in the cell reference |
32
|
|
|
* Or can be an array of values |
33
|
|
|
* @param mixed $column Column number (integer) to use in the cell reference |
34
|
|
|
* Or can be an array of values |
35
|
|
|
* @param mixed $relativity Integer flag indicating the type of reference to return |
36
|
|
|
* 1 or omitted Absolute |
37
|
|
|
* 2 Absolute row; relative column |
38
|
|
|
* 3 Relative row; absolute column |
39
|
|
|
* 4 Relative |
40
|
|
|
* Or can be an array of values |
41
|
|
|
* @param mixed $referenceStyle A logical (boolean) value that specifies the A1 or R1C1 reference style. |
42
|
|
|
* TRUE or omitted ADDRESS returns an A1-style reference |
43
|
|
|
* FALSE ADDRESS returns an R1C1-style reference |
44
|
|
|
* Or can be an array of values |
45
|
|
|
* @param mixed $sheetName Optional Name of worksheet to use |
46
|
|
|
* Or can be an array of values |
47
|
|
|
* |
48
|
|
|
* @return array|string If an array of values is passed as the $testValue argument, then the returned result will also be |
49
|
|
|
* an array with the same dimensions |
50
|
28 |
|
*/ |
51
|
|
|
public static function cell(mixed $row, mixed $column, mixed $relativity = 1, mixed $referenceStyle = true, mixed $sheetName = ''): array|string |
52
|
|
|
{ |
53
|
28 |
|
if ( |
54
|
28 |
|
is_array($row) || is_array($column) |
55
|
|
|
|| is_array($relativity) || is_array($referenceStyle) || is_array($sheetName) |
56
|
1 |
|
) { |
57
|
1 |
|
return self::evaluateArrayArguments( |
58
|
1 |
|
[self::class, __FUNCTION__], |
59
|
1 |
|
$row, |
60
|
1 |
|
$column, |
61
|
1 |
|
$relativity, |
62
|
1 |
|
$referenceStyle, |
63
|
1 |
|
$sheetName |
64
|
|
|
); |
65
|
|
|
} |
66
|
28 |
|
|
67
|
28 |
|
$relativity = ($relativity === null) ? 1 : (int) StringHelper::convertToString($relativity); |
68
|
|
|
$referenceStyle = $referenceStyle ?? true; |
69
|
28 |
|
$row = (int) StringHelper::convertToString($row); |
70
|
1 |
|
$column = (int) StringHelper::convertToString($column); |
71
|
|
|
|
72
|
|
|
if (($row < 1) || ($column < 1)) { |
73
|
27 |
|
return ExcelError::VALUE(); |
74
|
|
|
} |
75
|
27 |
|
|
76
|
15 |
|
$sheetName = self::sheetName(StringHelper::convertToString($sheetName)); |
77
|
|
|
|
78
|
27 |
|
if (is_int($referenceStyle)) { |
79
|
9 |
|
$referenceStyle = (bool) $referenceStyle; |
80
|
|
|
} |
81
|
|
|
if ((!is_bool($referenceStyle)) || $referenceStyle === self::REFERENCE_STYLE_A1) { |
82
|
19 |
|
return self::formatAsA1($row, $column, $relativity, $sheetName); |
83
|
|
|
} |
84
|
|
|
|
85
|
27 |
|
return self::formatAsR1C1($row, $column, $relativity, $sheetName); |
86
|
|
|
} |
87
|
27 |
|
|
88
|
6 |
|
private static function sheetName(string $sheetName): string |
89
|
6 |
|
{ |
90
|
|
|
if ($sheetName > '') { |
91
|
6 |
|
if (str_contains($sheetName, ' ') || str_contains($sheetName, '[')) { |
92
|
|
|
$sheetName = "'{$sheetName}'"; |
93
|
|
|
} |
94
|
27 |
|
$sheetName .= '!'; |
95
|
|
|
} |
96
|
|
|
|
97
|
9 |
|
return $sheetName; |
98
|
|
|
} |
99
|
9 |
|
|
100
|
9 |
|
private static function formatAsA1(int $row, int $column, int $relativity, string $sheetName): string |
101
|
3 |
|
{ |
102
|
|
|
$rowRelative = $columnRelative = '$'; |
103
|
9 |
|
if (($relativity == self::ADDRESS_COLUMN_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) { |
104
|
2 |
|
$columnRelative = ''; |
105
|
|
|
} |
106
|
9 |
|
if (($relativity == self::ADDRESS_ROW_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) { |
107
|
|
|
$rowRelative = ''; |
108
|
9 |
|
} |
109
|
|
|
$column = Coordinate::stringFromColumnIndex($column); |
110
|
|
|
|
111
|
19 |
|
return "{$sheetName}{$columnRelative}{$column}{$rowRelative}{$row}"; |
112
|
|
|
} |
113
|
19 |
|
|
114
|
2 |
|
private static function formatAsR1C1(int $row, int $column, int $relativity, string $sheetName): string |
115
|
|
|
{ |
116
|
19 |
|
if (($relativity == self::ADDRESS_COLUMN_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) { |
117
|
1 |
|
$column = "[{$column}]"; |
118
|
|
|
} |
119
|
19 |
|
if (($relativity == self::ADDRESS_ROW_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) { |
120
|
|
|
$row = "[{$row}]"; |
121
|
19 |
|
} |
122
|
|
|
[$rowChar, $colChar] = AddressHelper::getRowAndColumnChars(); |
123
|
|
|
|
124
|
|
|
return "{$sheetName}$rowChar{$row}$colChar{$column}"; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|