From 57b6e87a65cf4a8b73565139f53abb0c74e30672 Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Tue, 25 Aug 2020 14:14:37 +0200 Subject: [PATCH] Begin optimize xlsx write --- src/Spout/Common/Entity/Style/EmptyStyle.php | 7 +++++++ .../Writer/Common/Manager/Style/StyleRegistry.php | 13 ++++++++++++- src/Spout/Writer/ODS/Manager/WorksheetManager.php | 11 ++++++++--- src/Spout/Writer/XLSX/Manager/WorksheetManager.php | 12 ++++++++---- 4 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 src/Spout/Common/Entity/Style/EmptyStyle.php diff --git a/src/Spout/Common/Entity/Style/EmptyStyle.php b/src/Spout/Common/Entity/Style/EmptyStyle.php new file mode 100644 index 0000000..56fa034 --- /dev/null +++ b/src/Spout/Common/Entity/Style/EmptyStyle.php @@ -0,0 +1,7 @@ +serialize($style); - if (!$this->hasStyleAlreadyBeenRegistered($style)) { + if (!$this->hasSerializedStyleAlreadyBeenRegistered($serializedStyle)) { $nextStyleId = \count($this->serializedStyleToStyleIdMappingTable); $style->setId($nextStyleId); @@ -57,6 +57,17 @@ class StyleRegistry { $serializedStyle = $this->serialize($style); + return $this->hasSerializedStyleAlreadyBeenRegistered($serializedStyle); + } + + /** + * Returns whether the serialized style has already been registered. + * + * @param string $serializedStyle The serialized style + * @return bool + */ + protected function hasSerializedStyleAlreadyBeenRegistered(string $serializedStyle) + { // Using isset here because it is way faster than array_key_exists... return isset($this->serializedStyleToStyleIdMappingTable[$serializedStyle]); } diff --git a/src/Spout/Writer/ODS/Manager/WorksheetManager.php b/src/Spout/Writer/ODS/Manager/WorksheetManager.php index 169e4e6..970cace 100644 --- a/src/Spout/Writer/ODS/Manager/WorksheetManager.php +++ b/src/Spout/Writer/ODS/Manager/WorksheetManager.php @@ -4,6 +4,7 @@ namespace Box\Spout\Writer\ODS\Manager; use Box\Spout\Common\Entity\Cell; use Box\Spout\Common\Entity\Row; +use Box\Spout\Common\Entity\Style\EmptyStyle; use Box\Spout\Common\Entity\Style\Style; use Box\Spout\Common\Exception\InvalidArgumentException; use Box\Spout\Common\Exception\IOException; @@ -157,9 +158,13 @@ class WorksheetManager implements WorksheetManagerInterface */ private function applyStyleAndGetCellXML(Cell $cell, Style $rowStyle, $currentCellIndex, $nextCellIndex) { - // Apply row and extra styles - $mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle); - $cell->setStyle($mergedCellAndRowStyle); + if ($cell->getStyle() instanceof EmptyStyle) { + $cell->setStyle($rowStyle); + } else { + $mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle); + $cell->setStyle($mergedCellAndRowStyle); + } + $newCellStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); $registeredStyle = $this->styleManager->registerStyle($newCellStyle); diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index 741d0aa..e49c1d8 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -4,6 +4,7 @@ namespace Box\Spout\Writer\XLSX\Manager; use Box\Spout\Common\Entity\Cell; use Box\Spout\Common\Entity\Row; +use Box\Spout\Common\Entity\Style\EmptyStyle; use Box\Spout\Common\Entity\Style\Style; use Box\Spout\Common\Exception\InvalidArgumentException; use Box\Spout\Common\Exception\IOException; @@ -185,11 +186,14 @@ EOD; */ private function applyStyleAndGetCellXML(Cell $cell, Style $rowStyle, $rowIndexOneBased, $columnIndexZeroBased) { - // Apply row and extra styles - $mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle); - $cell->setStyle($mergedCellAndRowStyle); - $newCellStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); + if ($cell->getStyle() instanceof EmptyStyle) { + $cell->setStyle($rowStyle); + } else { + $mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle); + $cell->setStyle($mergedCellAndRowStyle); + } + $newCellStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); $registeredStyle = $this->styleManager->registerStyle($newCellStyle); return $this->getCellXML($rowIndexOneBased, $columnIndexZeroBased, $cell, $registeredStyle->getId());