Skip to content

Commit 05d08fe

Browse files
authored
Merge pull request #2818 from PHPOffice/Performance-Minor-Tweaks
Performance tweaks to cell address validation
2 parents eb34e02 + 677054d commit 05d08fe

File tree

4 files changed

+9
-12
lines changed

4 files changed

+9
-12
lines changed

src/PhpSpreadsheet/Cell/AddressHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,17 @@ public static function convertToR1C1(
118118
throw new Exception('Invalid A1-format Cell Reference');
119119
}
120120

121-
$columnId = Coordinate::columnIndexFromString($cellReference['col_ref']);
122-
if ($cellReference['absolute_col'] === '$') {
121+
if ($cellReference['col'][0] === '$') {
123122
// Column must be absolute address
124123
$currentColumnNumber = null;
125124
}
125+
$columnId = Coordinate::columnIndexFromString(ltrim($cellReference['col'], '$'));
126126

127-
$rowId = (int) $cellReference['row_ref'];
128-
if ($cellReference['absolute_row'] === '$') {
127+
if ($cellReference['row'][0] === '$') {
129128
// Row must be absolute address
130129
$currentRowNumber = null;
131130
}
131+
$rowId = (int) ltrim($cellReference['row'], '$');
132132

133133
if ($currentRowNumber !== null) {
134134
if ($rowId === $currentRowNumber) {

src/PhpSpreadsheet/Cell/CellAddress.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ class CellAddress
3535
public function __construct(string $cellAddress, ?Worksheet $worksheet = null)
3636
{
3737
$this->cellAddress = str_replace('$', '', $cellAddress);
38-
[$this->columnName, $rowId] = Coordinate::coordinateFromString($cellAddress);
39-
$this->rowId = (int) $rowId;
40-
$this->columnId = Coordinate::columnIndexFromString($this->columnName);
38+
[$this->columnId, $this->rowId, $this->columnName] = Coordinate::indexesFromString($this->cellAddress);
4139
$this->worksheet = $worksheet;
4240
}
4341

src/PhpSpreadsheet/Cell/Coordinate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
abstract class Coordinate
1515
{
16-
public const A1_COORDINATE_REGEX = '/^(?<absolute_col>\$?)(?<col_ref>[A-Z]{1,3})(?<absolute_row>\$?)(?<row_ref>\d{1,7})$/i';
16+
public const A1_COORDINATE_REGEX = '/^(?<col>\$?[A-Z]{1,3})(?<row>\$?\d{1,7})$/i';
1717

1818
/**
1919
* Default range variable constant.
@@ -32,7 +32,7 @@ abstract class Coordinate
3232
public static function coordinateFromString($cellAddress)
3333
{
3434
if (preg_match(self::A1_COORDINATE_REGEX, $cellAddress, $matches)) {
35-
return [$matches['absolute_col'] . $matches['col_ref'], $matches['absolute_row'] . $matches['row_ref']];
35+
return [$matches['col'], $matches['row']];
3636
} elseif (self::coordinateIsRange($cellAddress)) {
3737
throw new Exception('Cell coordinate string can not be a range of cells');
3838
} elseif ($cellAddress == '') {

src/PhpSpreadsheet/Worksheet/Validations.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ public static function validateCellAddress($cellAddress): string
4242
public static function validateCellOrCellRange($cellRange): string
4343
{
4444
if (is_string($cellRange) || is_numeric($cellRange)) {
45-
$cellRange = (string) $cellRange;
4645
// Convert a single column reference like 'A' to 'A:A'
47-
$cellRange = (string) preg_replace('/^([A-Z]+)$/', '${1}:${1}', $cellRange);
46+
$cellRange = (string) preg_replace('/^([A-Z]+)$/', '${1}:${1}', (string) $cellRange);
4847
// Convert a single row reference like '1' to '1:1'
4948
$cellRange = (string) preg_replace('/^(\d+)$/', '${1}:${1}', $cellRange);
5049
} elseif (is_object($cellRange) && $cellRange instanceof CellAddress) {
@@ -85,7 +84,7 @@ public static function validateCellRange($cellRange): string
8584
public static function definedNameToCoordinate(string $coordinate, Worksheet $worksheet): string
8685
{
8786
// Uppercase coordinate
88-
$testCoordinate = strtoupper($coordinate);
87+
$coordinate = strtoupper($coordinate);
8988
// Eliminate leading equal sign
9089
$testCoordinate = (string) preg_replace('/^=/', '', $coordinate);
9190
$defined = $worksheet->getParent()->getDefinedName($testCoordinate, $worksheet);

0 commit comments

Comments
 (0)