|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpOffice\PhpSpreadsheet\Worksheet; |
| 4 | + |
| 5 | +use PhpOffice\PhpSpreadsheet\Cell\AddressRange; |
| 6 | +use PhpOffice\PhpSpreadsheet\Cell\CellAddress; |
| 7 | +use PhpOffice\PhpSpreadsheet\Cell\CellRange; |
| 8 | + |
| 9 | +class Validations |
| 10 | +{ |
| 11 | + /** |
| 12 | + * Validate a cell address. |
| 13 | + * |
| 14 | + * @param null|array<int>|CellAddress|string $cellAddress Coordinate of the cell as a string, eg: 'C5'; |
| 15 | + * or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. |
| 16 | + */ |
| 17 | + public static function validateCellAddress($cellAddress): string |
| 18 | + { |
| 19 | + if (is_string($cellAddress)) { |
| 20 | + [$worksheet, $address] = Worksheet::extractSheetTitle($cellAddress, true); |
| 21 | +// if (!empty($worksheet) && $worksheet !== $this->getTitle()) { |
| 22 | +// throw new Exception('Reference is not for this worksheet'); |
| 23 | +// } |
| 24 | + |
| 25 | + return empty($worksheet) ? strtoupper($address) : $worksheet . '!' . strtoupper($address); |
| 26 | + } |
| 27 | + |
| 28 | + if (is_array($cellAddress)) { |
| 29 | + $cellAddress = CellAddress::fromColumnRowArray($cellAddress); |
| 30 | + } |
| 31 | + |
| 32 | + return (string) $cellAddress; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Validate a cell address or cell range. |
| 37 | + * |
| 38 | + * @param AddressRange|array<int>|CellAddress|int|string $cellRange Coordinate of the cells as a string, eg: 'C5:F12'; |
| 39 | + * or as an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 12]), |
| 40 | + * or as a CellAddress or AddressRange object. |
| 41 | + */ |
| 42 | + public static function validateCellOrCellRange($cellRange): string |
| 43 | + { |
| 44 | + if (is_string($cellRange) || is_numeric($cellRange)) { |
| 45 | + $cellRange = (string) $cellRange; |
| 46 | + // Convert a single column reference like 'A' to 'A:A' |
| 47 | + $cellRange = (string) preg_replace('/^([A-Z]+)$/', '${1}:${1}', $cellRange); |
| 48 | + // Convert a single row reference like '1' to '1:1' |
| 49 | + $cellRange = (string) preg_replace('/^(\d+)$/', '${1}:${1}', $cellRange); |
| 50 | + } elseif (is_object($cellRange) && $cellRange instanceof CellAddress) { |
| 51 | + $cellRange = new CellRange($cellRange, $cellRange); |
| 52 | + } |
| 53 | + |
| 54 | + return self::validateCellRange($cellRange); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Validate a cell range. |
| 59 | + * |
| 60 | + * @param AddressRange|array<int>|string $cellRange Coordinate of the cells as a string, eg: 'C5:F12'; |
| 61 | + * or as an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 12]), |
| 62 | + * or as an AddressRange object. |
| 63 | + */ |
| 64 | + public static function validateCellRange($cellRange): string |
| 65 | + { |
| 66 | + if (is_string($cellRange)) { |
| 67 | + [$worksheet, $addressRange] = Worksheet::extractSheetTitle($cellRange, true); |
| 68 | + |
| 69 | + // Convert Column ranges like 'A:C' to 'A1:C1048576' |
| 70 | + $addressRange = (string) preg_replace('/^([A-Z]+):([A-Z]+)$/', '${1}1:${2}1048576', $addressRange); |
| 71 | + // Convert Row ranges like '1:3' to 'A1:XFD3' |
| 72 | + $addressRange = (string) preg_replace('/^(\\d+):(\\d+)$/', 'A${1}:XFD${2}', $addressRange); |
| 73 | + |
| 74 | + return empty($worksheet) ? strtoupper($addressRange) : $worksheet . '!' . strtoupper($addressRange); |
| 75 | + } |
| 76 | + |
| 77 | + if (is_array($cellRange)) { |
| 78 | + [$from, $to] = array_chunk($cellRange, 2); |
| 79 | + $cellRange = new CellRange(CellAddress::fromColumnRowArray($from), CellAddress::fromColumnRowArray($to)); |
| 80 | + } |
| 81 | + |
| 82 | + return (string) $cellRange; |
| 83 | + } |
| 84 | + |
| 85 | + public static function definedNameToCoordinate(string $coordinate, Worksheet $worksheet): string |
| 86 | + { |
| 87 | + // Uppercase coordinate |
| 88 | + $testCoordinate = strtoupper($coordinate); |
| 89 | + // Eliminate leading equal sign |
| 90 | + $testCoordinate = (string) preg_replace('/^=/', '', $coordinate); |
| 91 | + $defined = $worksheet->getParent()->getDefinedName($testCoordinate, $worksheet); |
| 92 | + if ($defined !== null) { |
| 93 | + if ($defined->getWorksheet() === $worksheet && !$defined->isFormula()) { |
| 94 | + $coordinate = (string) preg_replace('/^=/', '', $defined->getValue()); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return $coordinate; |
| 99 | + } |
| 100 | +} |
0 commit comments