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