| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\LookupRef; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Cell\AddressHelper; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Cell\Cell; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\DefinedName; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
| 9 |
|
| 10 |
class Helpers |
| 11 |
{ |
| 12 |
public const CELLADDRESS_USE_A1 = true; |
| 13 |
|
| 14 |
public const CELLADDRESS_USE_R1C1 = false; |
| 15 |
|
| 16 |
private static function convertR1C1(string &$cellAddress1, ?string &$cellAddress2, bool $a1, ?int $baseRow = null, ?int $baseCol = null): string |
| 17 |
{ |
| 18 |
if ($a1 === self::CELLADDRESS_USE_R1C1) { |
| 19 |
$cellAddress1 = AddressHelper::convertToA1($cellAddress1, $baseRow ?? 1, $baseCol ?? 1); |
| 20 |
if ($cellAddress2) { |
| 21 |
$cellAddress2 = AddressHelper::convertToA1($cellAddress2, $baseRow ?? 1, $baseCol ?? 1); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
return $cellAddress1 . ($cellAddress2 ? ":$cellAddress2" : ''); |
| 26 |
} |
| 27 |
|
| 28 |
private static function adjustSheetTitle(string &$sheetTitle, ?string $value): void |
| 29 |
{ |
| 30 |
if ($sheetTitle) { |
| 31 |
$sheetTitle .= '!'; |
| 32 |
if (stripos($value ?? '', $sheetTitle) === 0) { |
| 33 |
$sheetTitle = ''; |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/** @return array{string, ?string, string} */ |
| 39 |
public static function extractCellAddresses(string $cellAddress, bool $a1, Worksheet $sheet, string $sheetName = '', ?int $baseRow = null, ?int $baseCol = null): array |
| 40 |
{ |
| 41 |
$cellAddress1 = $cellAddress; |
| 42 |
$cellAddress2 = null; |
| 43 |
$namedRange = DefinedName::resolveName($cellAddress1, $sheet, $sheetName); |
| 44 |
if ($namedRange !== null) { |
| 45 |
$workSheet = $namedRange->getWorkSheet(); |
| 46 |
$sheetTitle = ($workSheet === null) ? '' : $workSheet->getTitle(); |
| 47 |
$value = (string) preg_replace('/^=/', '', $namedRange->getValue()); |
| 48 |
self::adjustSheetTitle($sheetTitle, $value); |
| 49 |
$cellAddress1 = $sheetTitle . $value; |
| 50 |
$cellAddress = $cellAddress1; |
| 51 |
$a1 = self::CELLADDRESS_USE_A1; |
| 52 |
} |
| 53 |
if (str_contains($cellAddress, ':')) { |
| 54 |
[$cellAddress1, $cellAddress2] = explode(':', $cellAddress); |
| 55 |
} |
| 56 |
$cellAddress = self::convertR1C1($cellAddress1, $cellAddress2, $a1, $baseRow, $baseCol); |
| 57 |
|
| 58 |
return [$cellAddress1, $cellAddress2, $cellAddress]; |
| 59 |
} |
| 60 |
|
| 61 |
/** @return array{string, ?Worksheet, string} */ |
| 62 |
public static function extractWorksheet(string $cellAddress, Cell $cell): array |
| 63 |
{ |
| 64 |
$sheetName = ''; |
| 65 |
if (str_contains($cellAddress, '!')) { |
| 66 |
[$sheetName, $cellAddress] = Worksheet::extractSheetTitle($cellAddress, true, true); |
| 67 |
} |
| 68 |
|
| 69 |
$worksheet = ($sheetName !== '') |
| 70 |
? $cell->getWorksheet()->getParentOrThrow()->getSheetByName($sheetName) |
| 71 |
: $cell->getWorksheet(); |
| 72 |
|
| 73 |
return [$cellAddress, $worksheet, $sheetName]; |
| 74 |
} |
| 75 |
} |
| 76 |
|