From 4046d24fc1eba5ca8673305c77f1eb7d3eabbee6 Mon Sep 17 00:00:00 2001 From: Jorick Schram Date: Fri, 19 Apr 2024 15:08:25 +0200 Subject: [PATCH 1/6] Don't filter out DateTime values --- src/Exportable.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Exportable.php b/src/Exportable.php index 8b063ec..3d95a6d 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -2,6 +2,7 @@ namespace Rap2hpoutre\FastExcel; +use DateTimeInterface; use Generator; use Illuminate\Support\Collection; use Illuminate\Support\Str; @@ -280,7 +281,7 @@ private function transformRow($data) return collect($data)->map(function ($value) { return is_null($value) ? (string) $value : $value; })->filter(function ($value) { - return is_string($value) || is_int($value) || is_float($value); + return is_string($value) || is_int($value) || is_float($value) || $value instanceof DateTimeInterface; }); } From ff22c1486f5d6117d2b62551d1de7538a5eebf7e Mon Sep 17 00:00:00 2001 From: Jorick Schram Date: Thu, 25 Apr 2024 20:48:18 +0200 Subject: [PATCH 2/6] Implement per cell styling of openspout --- composer.json | 8 +++++++- src/Exportable.php | 21 ++++++++++++++++----- tests/FastExcelTest.php | 30 ++++++++++++++++++++++++++++-- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 2f6202f..40a3357 100644 --- a/composer.json +++ b/composer.json @@ -9,10 +9,16 @@ "xlsx" ], "description": "Fast Excel import/export for Laravel", + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/jschram/openspout.git" + } + ], "require": { "php": "^8.0", "illuminate/support": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0", - "openspout/openspout": "^4.1.1" + "openspout/openspout": "4.x-dev" }, "require-dev": { "illuminate/database": "^6.20.12 || ^7.30.4 || ^8.24.0 || ^9.0 || ^10.0 || ^11.0", diff --git a/src/Exportable.php b/src/Exportable.php index 3d95a6d..f7fb768 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -27,6 +27,9 @@ trait Exportable private $header_style; private $rows_style; + /** @var Style[] */ + private $column_styles = []; + /** * @param AbstractOptions $options * @@ -34,6 +37,14 @@ trait Exportable */ abstract protected function setOptions(&$options); + /** @param Style[] $styles */ + public function setColumnStyles($styles): static + { + $this->column_styles = $styles; + + return $this; + } + /** * @param string $path * @param callable|null $callback @@ -183,19 +194,19 @@ private function writeRowsFromCollection($writer, Collection $collection, ?calla $all_rows = $collection->map(function ($value) { return Row::fromValues($value); })->toArray(); - if ($this->rows_style) { - $this->addRowsWithStyle($writer, $all_rows, $this->rows_style); + if ($this->rows_style || count($this->column_styles)) { + $this->addRowsWithStyle($writer, $all_rows, $this->rows_style, $this->column_styles); } else { $writer->addRows($all_rows); } } - private function addRowsWithStyle($writer, $all_rows, $rows_style) + private function addRowsWithStyle($writer, $all_rows, $rows_style, $column_styles) { $styled_rows = []; // Style rows one by one foreach ($all_rows as $row) { - $styled_rows[] = Row::fromValues($row->toArray(), $rows_style); + $styled_rows[] = Row::fromValues($row->toArray(), $rows_style, $column_styles); } $writer->addRows($styled_rows); } @@ -216,7 +227,7 @@ private function writeRowsFromGenerator($writer, Generator $generator, ?callable $this->writeHeader($writer, $item); } // Write rows (one by one). - $writer->addRow(Row::fromValues($item->toArray(), $this->rows_style)); + $writer->addRow(Row::fromValues($item->toArray(), $this->rows_style, $this->column_styles)); } } diff --git a/tests/FastExcelTest.php b/tests/FastExcelTest.php index 4372fdc..6aef1de 100644 --- a/tests/FastExcelTest.php +++ b/tests/FastExcelTest.php @@ -4,6 +4,7 @@ use OpenSpout\Common\Entity\Style\Color; use OpenSpout\Common\Entity\Style\Style; +use OpenSpout\Common\Exception\IOException; use OpenSpout\Reader\XLSX\Options; use Rap2hpoutre\FastExcel\FastExcel; use Rap2hpoutre\FastExcel\SheetCollection; @@ -13,6 +14,31 @@ */ class FastExcelTest extends TestCase { + /** + * @throws IOException + * @throws \OpenSpout\Writer\Exception\WriterNotOpenedException + * @throws \OpenSpout\Reader\Exception\ReaderNotOpenedException + * @throws \OpenSpout\Common\Exception\UnsupportedTypeException + * @throws \OpenSpout\Common\Exception\InvalidArgumentException + */ + public function testExportXlsxWithDates() + { + $collection = collect([ + ['col1' => new \DateTimeImmutable('1980-09-18 00:00:00.000000')], + ['col1' => new \DateTimeImmutable('2018-07-02 00:00:00.000000')], + ]); + + $file = __DIR__ . '/test-dates-export.xlsx'; + (new FastExcel(clone $collection)) + ->setColumnStyles([ + 0 => (new Style())->setFormat('mm/dd/yyyy'), + ]) + ->export($file); + + $this->assertEquals($collection, (new FastExcel())->import($file)); + unlink($file); + } + /** * @throws \OpenSpout\Common\Exception\IOException * @throws \OpenSpout\Common\Exception\UnsupportedTypeException @@ -47,9 +73,9 @@ public function testImportCsv() * @throws \OpenSpout\Reader\Exception\ReaderNotOpenedException * @throws \OpenSpout\Writer\Exception\WriterNotOpenedException */ - private function export($file) + private function export($file, $override_collection = null) { - $original_collection = $this->collection(); + $original_collection = $override_collection ?? $this->collection(); (new FastExcel(clone $original_collection))->export($file); $this->assertEquals($original_collection, (new FastExcel())->import($file)); From 8c02a91da2ad60c2f3a8a2a2bf1091e28ad3b38a Mon Sep 17 00:00:00 2001 From: Jorick Schram Date: Thu, 25 Apr 2024 20:49:22 +0200 Subject: [PATCH 3/6] Remove obsolete parameter --- tests/FastExcelTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/FastExcelTest.php b/tests/FastExcelTest.php index 6aef1de..abfa248 100644 --- a/tests/FastExcelTest.php +++ b/tests/FastExcelTest.php @@ -73,9 +73,9 @@ public function testImportCsv() * @throws \OpenSpout\Reader\Exception\ReaderNotOpenedException * @throws \OpenSpout\Writer\Exception\WriterNotOpenedException */ - private function export($file, $override_collection = null) + private function export($file) { - $original_collection = $override_collection ?? $this->collection(); + $original_collection = $this->collection(); (new FastExcel(clone $original_collection))->export($file); $this->assertEquals($original_collection, (new FastExcel())->import($file)); From 1388b3054ff32a6a1a19a050576d875051d4b99a Mon Sep 17 00:00:00 2001 From: Jorick Schram Date: Thu, 25 Apr 2024 20:56:01 +0200 Subject: [PATCH 4/6] Fix styling --- tests/FastExcelTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/FastExcelTest.php b/tests/FastExcelTest.php index abfa248..71a88bd 100644 --- a/tests/FastExcelTest.php +++ b/tests/FastExcelTest.php @@ -28,7 +28,7 @@ public function testExportXlsxWithDates() ['col1' => new \DateTimeImmutable('2018-07-02 00:00:00.000000')], ]); - $file = __DIR__ . '/test-dates-export.xlsx'; + $file = __DIR__.'/test-dates-export.xlsx'; (new FastExcel(clone $collection)) ->setColumnStyles([ 0 => (new Style())->setFormat('mm/dd/yyyy'), From 37516bb90004f8585c233d484617f842111119ec Mon Sep 17 00:00:00 2001 From: Jorick Schram Date: Fri, 10 May 2024 11:19:47 +0200 Subject: [PATCH 5/6] Update to reflect openspout changes --- composer.json | 8 +------- src/Exportable.php | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index 40a3357..047df41 100644 --- a/composer.json +++ b/composer.json @@ -9,16 +9,10 @@ "xlsx" ], "description": "Fast Excel import/export for Laravel", - "repositories": [ - { - "type": "vcs", - "url": "https://github.com/jschram/openspout.git" - } - ], "require": { "php": "^8.0", "illuminate/support": "^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0", - "openspout/openspout": "4.x-dev" + "openspout/openspout": "^4.24" }, "require-dev": { "illuminate/database": "^6.20.12 || ^7.30.4 || ^8.24.0 || ^9.0 || ^10.0 || ^11.0", diff --git a/src/Exportable.php b/src/Exportable.php index f7fb768..6dc85d0 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -206,7 +206,7 @@ private function addRowsWithStyle($writer, $all_rows, $rows_style, $column_style $styled_rows = []; // Style rows one by one foreach ($all_rows as $row) { - $styled_rows[] = Row::fromValues($row->toArray(), $rows_style, $column_styles); + $styled_rows[] = $this->createRow($row->toArray(), $rows_style, $column_styles); } $writer->addRows($styled_rows); } @@ -227,7 +227,7 @@ private function writeRowsFromGenerator($writer, Generator $generator, ?callable $this->writeHeader($writer, $item); } // Write rows (one by one). - $writer->addRow(Row::fromValues($item->toArray(), $this->rows_style, $this->column_styles)); + $writer->addRow($this->createRow($item->toArray(), $this->rows_style, $this->column_styles)); } } @@ -248,7 +248,7 @@ private function writeHeader($writer, $first_row) } $keys = array_keys(is_array($first_row) ? $first_row : $first_row->toArray()); - $writer->addRow(Row::fromValues($keys, $this->header_style)); + $writer->addRow($this->createRow($keys, $this->header_style)); // $writer->addRow(WriterEntityFactory::createRowFromArray($keys, $this->header_style)); } @@ -319,4 +319,14 @@ public function rowsStyle(Style $style) return $this; } + + /** + * Create openspout row from values with optional row and cell styling + * + * @SuppressWarnings(PHPMD.StaticAccess) + */ + private function createRow(array $values = [], ?Style $rows_style = null, array $column_styles = []): Row + { + return Row::fromValuesWithStyles($values, $rows_style, $column_styles); + } } From f5e0e29492fc028e2fdce8952a89bcefda104741 Mon Sep 17 00:00:00 2001 From: Jorick Schram Date: Fri, 10 May 2024 11:20:50 +0200 Subject: [PATCH 6/6] Fix StyleCI linting --- src/Exportable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Exportable.php b/src/Exportable.php index 6dc85d0..7aada0f 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -321,7 +321,7 @@ public function rowsStyle(Style $style) } /** - * Create openspout row from values with optional row and cell styling + * Create openspout row from values with optional row and cell styling. * * @SuppressWarnings(PHPMD.StaticAccess) */