From 8e87c392a5cbf76a73b3fc4d065b8ab02069fe58 Mon Sep 17 00:00:00 2001 From: Piotr Nguyen Ha Date: Mon, 16 May 2022 11:07:17 +0200 Subject: [PATCH 1/5] allows to pass format as array for each column --- composer.json | 2 +- .../Writer/Common/Creator/WriterEntityFactory.php | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 8bdd2659..b3781a26 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ } ], "require": { - "php": ">=7.3.0", + "php": ">=8.0", "ext-zip": "*", "ext-xmlreader": "*", "ext-dom": "*" diff --git a/src/Spout/Writer/Common/Creator/WriterEntityFactory.php b/src/Spout/Writer/Common/Creator/WriterEntityFactory.php index f07e5f01..e6333f93 100644 --- a/src/Spout/Writer/Common/Creator/WriterEntityFactory.php +++ b/src/Spout/Writer/Common/Creator/WriterEntityFactory.php @@ -7,6 +7,7 @@ use Box\Spout\Common\Entity\Style\Style; use Box\Spout\Common\Exception\UnsupportedTypeException; use Box\Spout\Common\Type; +use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; use Box\Spout\Writer\WriterInterface; /** @@ -101,9 +102,12 @@ public static function createRow(array $cells = [], Style $rowStyle = null) */ public static function createRowFromArray(array $cellValues = [], Style $rowStyle = null) { - $cells = \array_map(function ($cellValue) { - return new Cell($cellValue); - }, $cellValues); + $format = $rowStyle?->getFormat(); + + $cells = \array_map(function ($k, $cellValue) { + $cellStyle = (new StyleBuilder())->setFormat($format[$k] ?? '@')->build(); + return new Cell($cellValue, $cellStyle); + }, array_keys($cellValues), $cellValues); return new Row($cells, $rowStyle); } From c6b2498091352b9f4ecdd0c7e3c4d873410711ec Mon Sep 17 00:00:00 2001 From: Piotr Nguyen Ha Date: Mon, 16 May 2022 11:16:40 +0200 Subject: [PATCH 2/5] optimize for large exports --- .../Writer/Common/Creator/WriterEntityFactory.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Spout/Writer/Common/Creator/WriterEntityFactory.php b/src/Spout/Writer/Common/Creator/WriterEntityFactory.php index e6333f93..6dcebbf1 100644 --- a/src/Spout/Writer/Common/Creator/WriterEntityFactory.php +++ b/src/Spout/Writer/Common/Creator/WriterEntityFactory.php @@ -103,10 +103,15 @@ public static function createRow(array $cells = [], Style $rowStyle = null) public static function createRowFromArray(array $cellValues = [], Style $rowStyle = null) { $format = $rowStyle?->getFormat(); + $cellStyles = []; - $cells = \array_map(function ($k, $cellValue) { - $cellStyle = (new StyleBuilder())->setFormat($format[$k] ?? '@')->build(); - return new Cell($cellValue, $cellStyle); + if (is_array($format)) { + foreach ($format as $k => $f) + $cellStyles[$k] = (new StyleBuilder())->setFormat($f ?? '@')->build(); + } + + $cells = \array_map(function ($k, $cellValue) use ($cellStyles) { + return new Cell($cellValue, $cellStyles[$k] ?? null); }, array_keys($cellValues), $cellValues); return new Row($cells, $rowStyle); From cd4887138e178c767431b96df3dbc2defcc44d85 Mon Sep 17 00:00:00 2001 From: Piotr Nguyen Ha Date: Mon, 16 May 2022 14:30:53 +0200 Subject: [PATCH 3/5] fix for asoc array --- src/Spout/Writer/Common/Creator/WriterEntityFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Spout/Writer/Common/Creator/WriterEntityFactory.php b/src/Spout/Writer/Common/Creator/WriterEntityFactory.php index 6dcebbf1..d54b9324 100644 --- a/src/Spout/Writer/Common/Creator/WriterEntityFactory.php +++ b/src/Spout/Writer/Common/Creator/WriterEntityFactory.php @@ -112,7 +112,7 @@ public static function createRowFromArray(array $cellValues = [], Style $rowStyl $cells = \array_map(function ($k, $cellValue) use ($cellStyles) { return new Cell($cellValue, $cellStyles[$k] ?? null); - }, array_keys($cellValues), $cellValues); + }, array_keys(array_values($cellValues)), $cellValues); return new Row($cells, $rowStyle); } From 16e34095e02e07635cea04d125fc6b5dda049965 Mon Sep 17 00:00:00 2001 From: Piotr Nguyen Ha Date: Mon, 16 May 2022 14:33:47 +0200 Subject: [PATCH 4/5] prevent for setting row formats as array --- src/Spout/Writer/XLSX/Manager/Style/StyleRegistry.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Spout/Writer/XLSX/Manager/Style/StyleRegistry.php b/src/Spout/Writer/XLSX/Manager/Style/StyleRegistry.php index 14eb9862..01a852c4 100644 --- a/src/Spout/Writer/XLSX/Manager/Style/StyleRegistry.php +++ b/src/Spout/Writer/XLSX/Manager/Style/StyleRegistry.php @@ -141,7 +141,7 @@ protected function registerFormat(Style $style) $styleId = $style->getId(); $format = $style->getFormat(); - if ($format) { + if (!is_array($format)) { $isFormatRegistered = isset($this->registeredFormats[$format]); // We need to track the already registered format definitions From afc047221d3cfdad8c2c824ab08ee3f1efb81352 Mon Sep 17 00:00:00 2001 From: Piotr Nguyen Ha Date: Wed, 18 May 2022 08:59:44 +0200 Subject: [PATCH 5/5] If format is passed as array then skip it from add it to rowstyles --- src/Spout/Writer/XLSX/Manager/Style/StyleRegistry.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Spout/Writer/XLSX/Manager/Style/StyleRegistry.php b/src/Spout/Writer/XLSX/Manager/Style/StyleRegistry.php index 01a852c4..3452e47e 100644 --- a/src/Spout/Writer/XLSX/Manager/Style/StyleRegistry.php +++ b/src/Spout/Writer/XLSX/Manager/Style/StyleRegistry.php @@ -141,6 +141,8 @@ protected function registerFormat(Style $style) $styleId = $style->getId(); $format = $style->getFormat(); + + // If format is passed as array then skip it from add it to rowstyles if (!is_array($format)) { $isFormatRegistered = isset($this->registeredFormats[$format]);