| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\LookupRef; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Cell\AddressHelper; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
| 9 |
use TablePress\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 mixed[]|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 |
*/ |
| 51 |
public static function cell($row, $column, $relativity = 1, $referenceStyle = true, $sheetName = '') |
| 52 |
{ |
| 53 |
if ( |
| 54 |
is_array($row) || is_array($column) |
| 55 |
|| is_array($relativity) || is_array($referenceStyle) || is_array($sheetName) |
| 56 |
) { |
| 57 |
return self::evaluateArrayArguments( |
| 58 |
[self::class, __FUNCTION__], |
| 59 |
$row, |
| 60 |
$column, |
| 61 |
$relativity, |
| 62 |
$referenceStyle, |
| 63 |
$sheetName |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
$relativity = ($relativity === null) ? 1 : (int) StringHelper::convertToString($relativity); |
| 68 |
$referenceStyle = $referenceStyle ?? true; |
| 69 |
$row = (int) StringHelper::convertToString($row); |
| 70 |
$column = (int) StringHelper::convertToString($column); |
| 71 |
|
| 72 |
if (($row < 1) || ($column < 1)) { |
| 73 |
return ExcelError::VALUE(); |
| 74 |
} |
| 75 |
|
| 76 |
$sheetName = self::sheetName(StringHelper::convertToString($sheetName)); |
| 77 |
|
| 78 |
if (is_int($referenceStyle)) { |
| 79 |
$referenceStyle = (bool) $referenceStyle; |
| 80 |
} |
| 81 |
if ((!is_bool($referenceStyle)) || $referenceStyle === self::REFERENCE_STYLE_A1) { |
| 82 |
return self::formatAsA1($row, $column, $relativity, $sheetName); |
| 83 |
} |
| 84 |
|
| 85 |
return self::formatAsR1C1($row, $column, $relativity, $sheetName); |
| 86 |
} |
| 87 |
|
| 88 |
private static function sheetName(string $sheetName): string |
| 89 |
{ |
| 90 |
if ($sheetName > '') { |
| 91 |
if (str_contains($sheetName, ' ') || str_contains($sheetName, '[')) { |
| 92 |
$sheetName = "'{$sheetName}'"; |
| 93 |
} |
| 94 |
$sheetName .= '!'; |
| 95 |
} |
| 96 |
|
| 97 |
return $sheetName; |
| 98 |
} |
| 99 |
|
| 100 |
private static function formatAsA1(int $row, int $column, int $relativity, string $sheetName): string |
| 101 |
{ |
| 102 |
$rowRelative = $columnRelative = '$'; |
| 103 |
if (($relativity == self::ADDRESS_COLUMN_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) { |
| 104 |
$columnRelative = ''; |
| 105 |
} |
| 106 |
if (($relativity == self::ADDRESS_ROW_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) { |
| 107 |
$rowRelative = ''; |
| 108 |
} |
| 109 |
$column = Coordinate::stringFromColumnIndex($column); |
| 110 |
|
| 111 |
return "{$sheetName}{$columnRelative}{$column}{$rowRelative}{$row}"; |
| 112 |
} |
| 113 |
|
| 114 |
private static function formatAsR1C1(int $row, int $column, int $relativity, string $sheetName): string |
| 115 |
{ |
| 116 |
if (($relativity == self::ADDRESS_COLUMN_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) { |
| 117 |
$column = "[{$column}]"; |
| 118 |
} |
| 119 |
if (($relativity == self::ADDRESS_ROW_RELATIVE) || ($relativity == self::ADDRESS_RELATIVE)) { |
| 120 |
$row = "[{$row}]"; |
| 121 |
} |
| 122 |
[$rowChar, $colChar] = AddressHelper::getRowAndColumnChars(); |
| 123 |
|
| 124 |
return "{$sheetName}$rowChar{$row}$colChar{$column}"; |
| 125 |
} |
| 126 |
} |
| 127 |
|