Potential improvements for insert/delete column/row for performance testing

This commit is contained in:
MarkBaker 2022-06-14 12:01:08 +02:00
parent 7b7d3bc33c
commit 11a94dabec
1 changed files with 21 additions and 17 deletions

View File

@ -367,7 +367,6 @@ class ReferenceHelper
Worksheet $worksheet Worksheet $worksheet
): void { ): void {
$remove = ($numberOfColumns < 0 || $numberOfRows < 0); $remove = ($numberOfColumns < 0 || $numberOfRows < 0);
$allCoordinates = $worksheet->getCoordinates();
if ( if (
$this->cellReferenceHelper === null || $this->cellReferenceHelper === null ||
@ -394,12 +393,13 @@ class ReferenceHelper
} }
// Find missing coordinates. This is important when inserting column before the last column // Find missing coordinates. This is important when inserting column before the last column
$cellCollection = $worksheet->getCellCollection();
$missingCoordinates = array_filter( $missingCoordinates = array_filter(
array_map(function ($row) use ($highestColumn) { array_map(function ($row) use ($highestColumn) {
return $highestColumn . $row; return $highestColumn . $row;
}, range(1, $highestRow)), }, range(1, $highestRow)),
function ($coordinate) use ($allCoordinates) { function ($coordinate) use ($cellCollection) {
return in_array($coordinate, $allCoordinates, true) === false; return $cellCollection->has($coordinate) === false;
} }
); );
@ -408,16 +408,15 @@ class ReferenceHelper
foreach ($missingCoordinates as $coordinate) { foreach ($missingCoordinates as $coordinate) {
$worksheet->createNewCell($coordinate); $worksheet->createNewCell($coordinate);
} }
// Refresh all coordinates
$allCoordinates = $worksheet->getCoordinates();
} }
// Loop through cells, bottom-up, and change cell coordinate $allCoordinates = $worksheet->getCoordinates();
if ($remove) { if ($remove) {
// It's faster to reverse and pop than to use unshift, especially with large cell collections // It's faster to reverse and pop than to use unshift, especially with large cell collections
$allCoordinates = array_reverse($allCoordinates); $allCoordinates = array_reverse($allCoordinates);
} }
// Loop through cells, bottom-up, and change cell coordinate
while ($coordinate = array_pop($allCoordinates)) { while ($coordinate = array_pop($allCoordinates)) {
$cell = $worksheet->getCell($coordinate); $cell = $worksheet->getCell($coordinate);
$cellIndex = Coordinate::columnIndexFromString($cell->getColumn()); $cellIndex = Coordinate::columnIndexFromString($cell->getColumn());
@ -927,11 +926,7 @@ class ReferenceHelper
for ($i = 1; $i <= $highestRow - 1; ++$i) { for ($i = 1; $i <= $highestRow - 1; ++$i) {
for ($j = $beforeColumn - 1 + $numberOfColumns; $j <= $beforeColumn - 2; ++$j) { for ($j = $beforeColumn - 1 + $numberOfColumns; $j <= $beforeColumn - 2; ++$j) {
$coordinate = Coordinate::stringFromColumnIndex($j + 1) . $i; $coordinate = Coordinate::stringFromColumnIndex($j + 1) . $i;
$worksheet->removeConditionalStyles($coordinate); $this->clearStripCell($worksheet, $coordinate);
if ($worksheet->cellExists($coordinate)) {
$worksheet->getCell($coordinate)->setValueExplicit(null, DataType::TYPE_NULL);
$worksheet->getCell($coordinate)->setXfIndex(0);
}
} }
} }
} }
@ -943,15 +938,24 @@ class ReferenceHelper
for ($i = $beforeColumn - 1; $i <= $lastColumnIndex; ++$i) { for ($i = $beforeColumn - 1; $i <= $lastColumnIndex; ++$i) {
for ($j = $beforeRow + $numberOfRows; $j <= $beforeRow - 1; ++$j) { for ($j = $beforeRow + $numberOfRows; $j <= $beforeRow - 1; ++$j) {
$coordinate = Coordinate::stringFromColumnIndex($i + 1) . $j; $coordinate = Coordinate::stringFromColumnIndex($i + 1) . $j;
$worksheet->removeConditionalStyles($coordinate); $this->clearStripCell($worksheet, $coordinate);
if ($worksheet->cellExists($coordinate)) {
$worksheet->getCell($coordinate)->setValueExplicit(null, DataType::TYPE_NULL);
$worksheet->getCell($coordinate)->setXfIndex(0);
}
} }
} }
} }
private function clearStripCell(Worksheet $worksheet, string $coordinate)
{
// TODO - Should also clear down comments, but wait until after comment removal PR-2875 is merged
$worksheet->removeConditionalStyles($coordinate);
$worksheet->setHyperlink($coordinate);
$worksheet->setDataValidation($coordinate);
if ($worksheet->cellExists($coordinate)) {
$worksheet->getCell($coordinate)->setValueExplicit(null, DataType::TYPE_NULL);
$worksheet->getCell($coordinate)->setXfIndex(0);
}
}
private function adjustAutoFilter(Worksheet $worksheet, string $beforeCellAddress, int $numberOfColumns): void private function adjustAutoFilter(Worksheet $worksheet, string $beforeCellAddress, int $numberOfColumns): void
{ {
$autoFilter = $worksheet->getAutoFilter(); $autoFilter = $worksheet->getAutoFilter();