Performance tweaks to cell address validation
This commit is contained in:
parent
eb34e02085
commit
677054dd0a
|
|
@ -118,17 +118,17 @@ class AddressHelper
|
|||
throw new Exception('Invalid A1-format Cell Reference');
|
||||
}
|
||||
|
||||
$columnId = Coordinate::columnIndexFromString($cellReference['col_ref']);
|
||||
if ($cellReference['absolute_col'] === '$') {
|
||||
if ($cellReference['col'][0] === '$') {
|
||||
// Column must be absolute address
|
||||
$currentColumnNumber = null;
|
||||
}
|
||||
$columnId = Coordinate::columnIndexFromString(ltrim($cellReference['col'], '$'));
|
||||
|
||||
$rowId = (int) $cellReference['row_ref'];
|
||||
if ($cellReference['absolute_row'] === '$') {
|
||||
if ($cellReference['row'][0] === '$') {
|
||||
// Row must be absolute address
|
||||
$currentRowNumber = null;
|
||||
}
|
||||
$rowId = (int) ltrim($cellReference['row'], '$');
|
||||
|
||||
if ($currentRowNumber !== null) {
|
||||
if ($rowId === $currentRowNumber) {
|
||||
|
|
|
|||
|
|
@ -35,9 +35,7 @@ class CellAddress
|
|||
public function __construct(string $cellAddress, ?Worksheet $worksheet = null)
|
||||
{
|
||||
$this->cellAddress = str_replace('$', '', $cellAddress);
|
||||
[$this->columnName, $rowId] = Coordinate::coordinateFromString($cellAddress);
|
||||
$this->rowId = (int) $rowId;
|
||||
$this->columnId = Coordinate::columnIndexFromString($this->columnName);
|
||||
[$this->columnId, $this->rowId, $this->columnName] = Coordinate::indexesFromString($this->cellAddress);
|
||||
$this->worksheet = $worksheet;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|||
*/
|
||||
abstract class Coordinate
|
||||
{
|
||||
public const A1_COORDINATE_REGEX = '/^(?<absolute_col>\$?)(?<col_ref>[A-Z]{1,3})(?<absolute_row>\$?)(?<row_ref>\d{1,7})$/i';
|
||||
public const A1_COORDINATE_REGEX = '/^(?<col>\$?[A-Z]{1,3})(?<row>\$?\d{1,7})$/i';
|
||||
|
||||
/**
|
||||
* Default range variable constant.
|
||||
|
|
@ -32,7 +32,7 @@ abstract class Coordinate
|
|||
public static function coordinateFromString($cellAddress)
|
||||
{
|
||||
if (preg_match(self::A1_COORDINATE_REGEX, $cellAddress, $matches)) {
|
||||
return [$matches['absolute_col'] . $matches['col_ref'], $matches['absolute_row'] . $matches['row_ref']];
|
||||
return [$matches['col'], $matches['row']];
|
||||
} elseif (self::coordinateIsRange($cellAddress)) {
|
||||
throw new Exception('Cell coordinate string can not be a range of cells');
|
||||
} elseif ($cellAddress == '') {
|
||||
|
|
|
|||
|
|
@ -42,9 +42,8 @@ class Validations
|
|||
public static function validateCellOrCellRange($cellRange): string
|
||||
{
|
||||
if (is_string($cellRange) || is_numeric($cellRange)) {
|
||||
$cellRange = (string) $cellRange;
|
||||
// Convert a single column reference like 'A' to 'A:A'
|
||||
$cellRange = (string) preg_replace('/^([A-Z]+)$/', '${1}:${1}', $cellRange);
|
||||
$cellRange = (string) preg_replace('/^([A-Z]+)$/', '${1}:${1}', (string) $cellRange);
|
||||
// Convert a single row reference like '1' to '1:1'
|
||||
$cellRange = (string) preg_replace('/^(\d+)$/', '${1}:${1}', $cellRange);
|
||||
} elseif (is_object($cellRange) && $cellRange instanceof CellAddress) {
|
||||
|
|
@ -85,7 +84,7 @@ class Validations
|
|||
public static function definedNameToCoordinate(string $coordinate, Worksheet $worksheet): string
|
||||
{
|
||||
// Uppercase coordinate
|
||||
$testCoordinate = strtoupper($coordinate);
|
||||
$coordinate = strtoupper($coordinate);
|
||||
// Eliminate leading equal sign
|
||||
$testCoordinate = (string) preg_replace('/^=/', '', $coordinate);
|
||||
$defined = $worksheet->getParent()->getDefinedName($testCoordinate, $worksheet);
|
||||
|
|
|
|||
Loading…
Reference in New Issue