Begin optimize xlsx write

This commit is contained in:
Antoine Lamirault 2020-08-25 14:14:37 +02:00 committed by Adrien Loison
parent 91f756be0b
commit 57b6e87a65
4 changed files with 35 additions and 8 deletions

View File

@ -0,0 +1,7 @@
<?php
namespace Box\Spout\Common\Entity\Style;
class EmptyStyle extends Style
{
}

View File

@ -36,7 +36,7 @@ class StyleRegistry
{
$serializedStyle = $this->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]);
}

View File

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

View File

@ -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
if ($cell->getStyle() instanceof EmptyStyle) {
$cell->setStyle($rowStyle);
} else {
$mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle);
$cell->setStyle($mergedCellAndRowStyle);
$newCellStyle = $this->styleManager->applyExtraStylesIfNeeded($cell);
}
$newCellStyle = $this->styleManager->applyExtraStylesIfNeeded($cell);
$registeredStyle = $this->styleManager->registerStyle($newCellStyle);
return $this->getCellXML($rowIndexOneBased, $columnIndexZeroBased, $cell, $registeredStyle->getId());