From 190e4f4ca0544ef9affb2a723957f2ad720a411d Mon Sep 17 00:00:00 2001 From: Elminson De Oleo Baez Date: Fri, 10 Apr 2020 19:50:26 -0400 Subject: [PATCH 1/6] Adding capability to replace the normal header for custom ones --- src/Exportable.php | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/Exportable.php b/src/Exportable.php index e023f85..2d3a0fb 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -21,6 +21,16 @@ trait Exportable */ private $header_style; + /** + * @var bool with Custom Header + */ + private $withCustomHeader; + + /** + * @var array header Fields + */ + private $headerFields; + /** * @param string $path * @@ -173,7 +183,12 @@ private function writeHeader($writer, $first_row) return; } - $keys = array_keys(is_array($first_row) ? $first_row : $first_row->toArray()); + if ($this->withCustomHeader) { + $keys = array_values(is_array($this->headerFields) ? $this->headerFields : $this->headerFields->toArray()); + } else { + $keys = array_keys(is_array($first_row) ? $first_row : $first_row->toArray()); + } + if ($this->header_style) { $writer->addRowWithStyle($keys, $this->header_style); } else { @@ -234,6 +249,32 @@ public function headerStyle(Style $style) { $this->header_style = $style; + return $this; + } + + /** + * @param $headerFields + * + * @return Exportable + */ + public function setCustomHeader($headerFields) + { + if($this->customHeader){ + $this->headerFields = $headerFields; + } + + return $this; + } + + /** + * @param bool $withCustomHeader + * + * @return Exportable + */ + public function withCustomHeader(bool $withCustomHeader) + { + $this->withCustomHeader = $withCustomHeader; + return $this; } } From 2ba70ee1318c1649cbed234668286c1b5f75188b Mon Sep 17 00:00:00 2001 From: Elminson De Oleo Baez Date: Fri, 10 Apr 2020 20:34:10 -0400 Subject: [PATCH 2/6] adding this withCustomHeader setCustomHeader --- src/Exportable.php | 8 +++++++- tests/FastExcelTest.php | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Exportable.php b/src/Exportable.php index 2d3a0fb..dc409f1 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -184,7 +184,13 @@ private function writeHeader($writer, $first_row) } if ($this->withCustomHeader) { + $keys = array_values(is_array($this->headerFields) ? $this->headerFields : $this->headerFields->toArray()); + if(count($first_row) != count($keys) ){ + throw new InvalidArgumentException('setCustomHeader fields need to match with the number of rows'); + } + + } else { $keys = array_keys(is_array($first_row) ? $first_row : $first_row->toArray()); } @@ -259,7 +265,7 @@ public function headerStyle(Style $style) */ public function setCustomHeader($headerFields) { - if($this->customHeader){ + if($this->withCustomHeader){ $this->headerFields = $headerFields; } diff --git a/tests/FastExcelTest.php b/tests/FastExcelTest.php index 3a98303..2ef1a29 100644 --- a/tests/FastExcelTest.php +++ b/tests/FastExcelTest.php @@ -190,4 +190,46 @@ public function testExportWithHeaderStyle() unlink($file); } + /** + * @throws \Box\Spout\Common\Exception\IOException + * @throws \Box\Spout\Common\Exception\InvalidArgumentException + * @throws \Box\Spout\Common\Exception\UnsupportedTypeException + * @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException + * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException + */ + + public function testExportWithCustomHeader() + { + $collectionsOriginal = [ + collect([ + ['col1' => 'row1 col1', 'col2' => 'row1 col2', 'col3' => 'row1 col3'], + ['col1' => 'row2 col1', 'col2' => 'row2 col2', 'col3' => 'row2 col3'], + ['col1' => 'row3 col1', 'col2' => 'row3 col2', 'col3' => 'row3 col3'], + ]) + ]; + $collectionsExpected = [ + collect([ + ['Id' => 'row1 col1', 'Nombre' => 'row1 col2', 'Telefono' => 'row1 col3'], + ['Id' => 'row2 col1', 'Nombre' => 'row2 col2', 'Telefono' => 'row2 col3'], + ['Id' => 'row3 col1', 'Nombre' => 'row3 col2', 'Telefono' => 'row3 col3'], + ]) + ]; + $file = __DIR__.'/test_custom_header.xlsx'; + $sheets = new SheetCollection($collectionsOriginal); + (new FastExcel($sheets))->withCustomHeader(true) + ->setCustomHeader([ + 'Id', + 'Nombre', + 'Telefono' + ])->export($file); + + $sheets = (new FastExcel())->importSheets($file); + $this->assertInstanceOf(SheetCollection::class, $sheets); + + $this->assertEquals($collectionsExpected[0][0], $sheets->all()[0][0]); + $this->assertEquals($collectionsExpected[0][1], $sheets->all()[0][1]); + $this->assertEquals($collectionsExpected[0][2], $sheets->all()[0][2]); + + unlink($file); + } } From 9ed06d04ebc4146ab824d7eca2dd9c3b1c587ebe Mon Sep 17 00:00:00 2001 From: Elminson De Oleo Baez Date: Fri, 10 Apr 2020 20:39:09 -0400 Subject: [PATCH 3/6] updating readme.md --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index c94a179..1b8abf2 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,31 @@ You can also import a specific sheet by its number: $users = (new FastExcel)->sheet(3)->import('file.xlsx'); ``` +### Export large collections with chunk +#### ->withCustomHeader +#### ->setCustomHeader + +- combine this 2 methods and you can set header on collection before export + +```php + +$collectionsOriginal = [ + collect([ + ['col1' => 'row1 col1', 'col2' => 'row1 col2', 'col3' => 'row1 col3'], + ['col1' => 'row2 col1', 'col2' => 'row2 col2', 'col3' => 'row2 col3'], + ['col1' => 'row3 col1', 'col2' => 'row3 col2', 'col3' => 'row3 col3'], + ]) + ]; + + $file = __DIR__.'/test_custom_header.xlsx'; + $sheets = new SheetCollection($collectionsOriginal); + (new FastExcel($sheets))->withCustomHeader(true) + ->setCustomHeader([ + 'Id', + 'Nombre', + 'Telefono' + ])->export($file); +``` ### Export large collections with chunk Export rows one by one to avoid `memory_limit` issues [using `yield`](https://www.php.net/manual/en/language.generators.syntax.php): From 68a3eb5ae578275547cbab0912e3d1940121ea95 Mon Sep 17 00:00:00 2001 From: Elminson De Oleo Baez Date: Fri, 10 Apr 2020 20:55:59 -0400 Subject: [PATCH 4/6] fixing styleci/pr --- src/Exportable.php | 58 +++++++++++++++++----------------- tests/FastExcelTest.php | 69 +++++++++++++++++++++-------------------- 2 files changed, 65 insertions(+), 62 deletions(-) diff --git a/src/Exportable.php b/src/Exportable.php index dc409f1..02f95d5 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -179,21 +179,21 @@ private function writeRowsFromArray($writer, array $array, ?callable $callback = private function writeHeader($writer, $first_row) { + if ($first_row === null) { return; } - if ($this->withCustomHeader) { - - $keys = array_values(is_array($this->headerFields) ? $this->headerFields : $this->headerFields->toArray()); - if(count($first_row) != count($keys) ){ - throw new InvalidArgumentException('setCustomHeader fields need to match with the number of rows'); - } + if ($this->withCustomHeader) { + $keys = array_values(is_array($this->headerFields) ? $this->headerFields : $this->headerFields->toArray()); + if (count($first_row) != count($keys)) { + throw new InvalidArgumentException('setCustomHeader fields need to match with the number of rows'); + } - } else { - $keys = array_keys(is_array($first_row) ? $first_row : $first_row->toArray()); - } + } else { + $keys = array_keys(is_array($first_row) ? $first_row : $first_row->toArray()); + } if ($this->header_style) { $writer->addRowWithStyle($keys, $this->header_style); @@ -258,27 +258,29 @@ public function headerStyle(Style $style) return $this; } - /** - * @param $headerFields - * - * @return Exportable - */ - public function setCustomHeader($headerFields) - { - if($this->withCustomHeader){ - $this->headerFields = $headerFields; - } - - return $this; - } - - /** - * @param bool $withCustomHeader - * - * @return Exportable - */ + /** + * @param $headerFields + * + * @return Exportable + */ + public function setCustomHeader($headerFields) + { + + if ($this->withCustomHeader) { + $this->headerFields = $headerFields; + } + + return $this; + } + + /** + * @param bool $withCustomHeader + * + * @return Exportable + */ public function withCustomHeader(bool $withCustomHeader) { + $this->withCustomHeader = $withCustomHeader; return $this; diff --git a/tests/FastExcelTest.php b/tests/FastExcelTest.php index 2ef1a29..5258f79 100644 --- a/tests/FastExcelTest.php +++ b/tests/FastExcelTest.php @@ -198,38 +198,39 @@ public function testExportWithHeaderStyle() * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException */ - public function testExportWithCustomHeader() - { - $collectionsOriginal = [ - collect([ - ['col1' => 'row1 col1', 'col2' => 'row1 col2', 'col3' => 'row1 col3'], - ['col1' => 'row2 col1', 'col2' => 'row2 col2', 'col3' => 'row2 col3'], - ['col1' => 'row3 col1', 'col2' => 'row3 col2', 'col3' => 'row3 col3'], - ]) - ]; - $collectionsExpected = [ - collect([ - ['Id' => 'row1 col1', 'Nombre' => 'row1 col2', 'Telefono' => 'row1 col3'], - ['Id' => 'row2 col1', 'Nombre' => 'row2 col2', 'Telefono' => 'row2 col3'], - ['Id' => 'row3 col1', 'Nombre' => 'row3 col2', 'Telefono' => 'row3 col3'], - ]) - ]; - $file = __DIR__.'/test_custom_header.xlsx'; - $sheets = new SheetCollection($collectionsOriginal); - (new FastExcel($sheets))->withCustomHeader(true) - ->setCustomHeader([ - 'Id', - 'Nombre', - 'Telefono' - ])->export($file); - - $sheets = (new FastExcel())->importSheets($file); - $this->assertInstanceOf(SheetCollection::class, $sheets); - - $this->assertEquals($collectionsExpected[0][0], $sheets->all()[0][0]); - $this->assertEquals($collectionsExpected[0][1], $sheets->all()[0][1]); - $this->assertEquals($collectionsExpected[0][2], $sheets->all()[0][2]); - - unlink($file); - } + public function testExportWithCustomHeader() + { + + $collectionsOriginal = [ + collect([ + ['col1' => 'row1 col1', 'col2' => 'row1 col2', 'col3' => 'row1 col3'], + ['col1' => 'row2 col1', 'col2' => 'row2 col2', 'col3' => 'row2 col3'], + ['col1' => 'row3 col1', 'col2' => 'row3 col2', 'col3' => 'row3 col3'], + ]) + ]; + $collectionsExpected = [ + collect([ + ['Id' => 'row1 col1', 'Nombre' => 'row1 col2', 'Telefono' => 'row1 col3'], + ['Id' => 'row2 col1', 'Nombre' => 'row2 col2', 'Telefono' => 'row2 col3'], + ['Id' => 'row3 col1', 'Nombre' => 'row3 col2', 'Telefono' => 'row3 col3'], + ]) + ]; + $file = __DIR__ . '/test_custom_header.xlsx'; + $sheets = new SheetCollection($collectionsOriginal); + (new FastExcel($sheets))->withCustomHeader(true) + ->setCustomHeader([ + 'Id', + 'Nombre', + 'Telefono' + ])->export($file); + + $sheets = (new FastExcel())->importSheets($file); + $this->assertInstanceOf(SheetCollection::class, $sheets); + + $this->assertEquals($collectionsExpected[0][0], $sheets->all()[0][0]); + $this->assertEquals($collectionsExpected[0][1], $sheets->all()[0][1]); + $this->assertEquals($collectionsExpected[0][2], $sheets->all()[0][2]); + + unlink($file); + } } From 53a7fe95b5aca40fbb8114f6886b7f6fa5b77908 Mon Sep 17 00:00:00 2001 From: Elminson De Oleo Baez Date: Fri, 10 Apr 2020 20:59:33 -0400 Subject: [PATCH 5/6] fixing styleci/pr --- src/Exportable.php | 4 ++-- tests/FastExcelTest.php | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Exportable.php b/src/Exportable.php index 02f95d5..3f61ae6 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -22,12 +22,12 @@ trait Exportable private $header_style; /** - * @var bool with Custom Header + * @var bool with Custom Header */ private $withCustomHeader; /** - * @var array header Fields + * @var array header Fields */ private $headerFields; diff --git a/tests/FastExcelTest.php b/tests/FastExcelTest.php index 5258f79..acfc212 100644 --- a/tests/FastExcelTest.php +++ b/tests/FastExcelTest.php @@ -190,13 +190,14 @@ public function testExportWithHeaderStyle() unlink($file); } - /** - * @throws \Box\Spout\Common\Exception\IOException - * @throws \Box\Spout\Common\Exception\InvalidArgumentException - * @throws \Box\Spout\Common\Exception\UnsupportedTypeException - * @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException - * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException - */ + + /** + * @throws \Box\Spout\Common\Exception\IOException + * @throws \Box\Spout\Common\Exception\InvalidArgumentException + * @throws \Box\Spout\Common\Exception\UnsupportedTypeException + * @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException + * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException + */ public function testExportWithCustomHeader() { From c2f597290c2d70250ee20b4ad9980354d7bea1f8 Mon Sep 17 00:00:00 2001 From: Elminson De Oleo Baez Date: Fri, 10 Apr 2020 21:02:18 -0400 Subject: [PATCH 6/6] fixing styleci/pr --- src/Exportable.php | 5 ----- tests/FastExcelTest.php | 10 ++++------ 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/Exportable.php b/src/Exportable.php index 3f61ae6..3b3a532 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -179,18 +179,15 @@ private function writeRowsFromArray($writer, array $array, ?callable $callback = private function writeHeader($writer, $first_row) { - if ($first_row === null) { return; } if ($this->withCustomHeader) { - $keys = array_values(is_array($this->headerFields) ? $this->headerFields : $this->headerFields->toArray()); if (count($first_row) != count($keys)) { throw new InvalidArgumentException('setCustomHeader fields need to match with the number of rows'); } - } else { $keys = array_keys(is_array($first_row) ? $first_row : $first_row->toArray()); } @@ -265,7 +262,6 @@ public function headerStyle(Style $style) */ public function setCustomHeader($headerFields) { - if ($this->withCustomHeader) { $this->headerFields = $headerFields; } @@ -280,7 +276,6 @@ public function setCustomHeader($headerFields) */ public function withCustomHeader(bool $withCustomHeader) { - $this->withCustomHeader = $withCustomHeader; return $this; diff --git a/tests/FastExcelTest.php b/tests/FastExcelTest.php index acfc212..7325180 100644 --- a/tests/FastExcelTest.php +++ b/tests/FastExcelTest.php @@ -198,31 +198,29 @@ public function testExportWithHeaderStyle() * @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException */ - public function testExportWithCustomHeader() { - $collectionsOriginal = [ collect([ ['col1' => 'row1 col1', 'col2' => 'row1 col2', 'col3' => 'row1 col3'], ['col1' => 'row2 col1', 'col2' => 'row2 col2', 'col3' => 'row2 col3'], ['col1' => 'row3 col1', 'col2' => 'row3 col2', 'col3' => 'row3 col3'], - ]) + ]), ]; $collectionsExpected = [ collect([ ['Id' => 'row1 col1', 'Nombre' => 'row1 col2', 'Telefono' => 'row1 col3'], ['Id' => 'row2 col1', 'Nombre' => 'row2 col2', 'Telefono' => 'row2 col3'], ['Id' => 'row3 col1', 'Nombre' => 'row3 col2', 'Telefono' => 'row3 col3'], - ]) + ]), ]; - $file = __DIR__ . '/test_custom_header.xlsx'; + $file = __DIR__.'/test_custom_header.xlsx'; $sheets = new SheetCollection($collectionsOriginal); (new FastExcel($sheets))->withCustomHeader(true) ->setCustomHeader([ 'Id', 'Nombre', - 'Telefono' + 'Telefono', ])->export($file); $sheets = (new FastExcel())->importSheets($file);