diff --git a/src/PhpSpreadsheet/Cell/Coordinate.php b/src/PhpSpreadsheet/Cell/Coordinate.php index b2dd65c4..fac3c629 100644 --- a/src/PhpSpreadsheet/Cell/Coordinate.php +++ b/src/PhpSpreadsheet/Cell/Coordinate.php @@ -47,15 +47,17 @@ abstract class Coordinate * * @param string $coordinates eg: 'A1', '$B$12' * - * @return array{0: int, 1: int} Array containing column index and row index (indexes 0 and 1) + * @return array{0: int, 1: int, 2: string} Array containing column and row index, and column string */ public static function indexesFromString(string $coordinates): array { - [$col, $row] = self::coordinateFromString($coordinates); + [$column, $row] = self::coordinateFromString($coordinates); + $column = ltrim($column, '$'); return [ - self::columnIndexFromString(ltrim($col, '$')), + self::columnIndexFromString($column), (int) ltrim($row, '$'), + $column, ]; } diff --git a/src/PhpSpreadsheet/Collection/Cells.php b/src/PhpSpreadsheet/Collection/Cells.php index 8555d09b..20fccf48 100644 --- a/src/PhpSpreadsheet/Collection/Cells.php +++ b/src/PhpSpreadsheet/Collection/Cells.php @@ -96,12 +96,7 @@ class Cells */ public function has($cellCoordinate) { - if ($cellCoordinate === $this->currentCoordinate) { - return true; - } - - // Check if the requested entry exists in the index - return isset($this->index[$cellCoordinate]); + return ($cellCoordinate === $this->currentCoordinate) || isset($this->index[$cellCoordinate]); } /** @@ -237,7 +232,7 @@ class Cells $toRow = $row * self::MAX_COLUMN_ID; $fromRow = --$row * self::MAX_COLUMN_ID; foreach ($this->index as $coordinate) { - if ($coordinate < $fromRow || $coordinate > $toRow) { + if ($coordinate < $fromRow || $coordinate >= $toRow) { continue; } $column = $coordinate % self::MAX_COLUMN_ID; diff --git a/src/PhpSpreadsheet/Worksheet/Worksheet.php b/src/PhpSpreadsheet/Worksheet/Worksheet.php index cb2cd06d..4c0237f5 100644 --- a/src/PhpSpreadsheet/Worksheet/Worksheet.php +++ b/src/PhpSpreadsheet/Worksheet/Worksheet.php @@ -181,13 +181,6 @@ class Worksheet implements IComparable */ private $conditionalStylesCollection = []; - /** - * Is the current cell collection sorted already? - * - * @var bool - */ - private $cellCollectionIsSorted = false; - /** * Collection of breaks. * @@ -1336,24 +1329,22 @@ class Worksheet implements IComparable */ public function createNewCell($coordinate) { - [$column, $row] = Coordinate::coordinateFromString($coordinate); + [$column, $row, $columnString] = Coordinate::indexesFromString($coordinate); $cell = new Cell(null, DataType::TYPE_NULL, $this); $this->cellCollection->add($coordinate, $cell); - $this->cellCollectionIsSorted = false; // Coordinates - $aIndexes = Coordinate::indexesFromString($coordinate); - if ($this->cachedHighestColumn < $aIndexes[0]) { - $this->cachedHighestColumn = $aIndexes[0]; + if ($column > $this->cachedHighestColumn) { + $this->cachedHighestColumn = $column; } - if ($aIndexes[1] > $this->cachedHighestRow) { - $this->cachedHighestRow = $aIndexes[1]; + if ($row > $this->cachedHighestRow) { + $this->cachedHighestRow = $row; } // Cell needs appropriate xfIndex from dimensions records // but don't create dimension records if they don't already exist $rowDimension = $this->rowDimensions[$row] ?? null; - $columnDimension = $this->columnDimensions[$column] ?? null; + $columnDimension = $this->columnDimensions[$columnString] ?? null; if ($rowDimension !== null && $rowDimension->getXfIndex() > 0) { // then there is a row dimension with explicit style, assign it to the cell diff --git a/tests/data/Cell/IndexesFromString.php b/tests/data/Cell/IndexesFromString.php index c2fe1c09..0a79e47f 100644 --- a/tests/data/Cell/IndexesFromString.php +++ b/tests/data/Cell/IndexesFromString.php @@ -5,6 +5,7 @@ return [ [ 1, 1, + 'A', ], 'A1', ], @@ -12,6 +13,7 @@ return [ [ 1, 12, + 'A', ], 'A12', ], @@ -19,6 +21,7 @@ return [ [ 10, 1, + 'J', ], 'J1', ], @@ -26,6 +29,7 @@ return [ [ 10, 20, + 'J', ], 'J20', ], @@ -33,6 +37,7 @@ return [ [ 35, 1, + 'AI', ], 'AI1', ], @@ -40,6 +45,7 @@ return [ [ 35, 2012, + 'AI', ], 'AI2012', ], @@ -47,6 +53,7 @@ return [ [ 2, 3, + 'B', ], 'B3', ], @@ -54,6 +61,7 @@ return [ [ 2, 3, + 'B', ], '$B3', ], @@ -61,6 +69,7 @@ return [ [ 2, 3, + 'B', ], 'B$3', ], @@ -68,6 +77,7 @@ return [ [ 2, 3, + 'B', ], '$B$3', ],