Minor performance tweaks

This commit is contained in:
MarkBaker 2022-05-01 14:44:09 +02:00
parent 3db129dc52
commit 4f7da29332
4 changed files with 23 additions and 25 deletions

View File

@ -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,
];
}

View File

@ -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;

View File

@ -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

View File

@ -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',
],