From fd73d22128a40d0bb8c26c1e612b49073db3a8e4 Mon Sep 17 00:00:00 2001 From: Eliurkis Diaz Date: Wed, 26 Jul 2023 12:27:36 -0400 Subject: [PATCH] Update Fork / Add Header Support / Start working in width for columns (#7) --- .github/workflows/tests.yml | 2 +- README.md | 14 ++++----- composer.json | 8 +++--- src/Exportable.php | 33 ++++++++++++++++++++-- src/Facades/FastExcel.php | 12 ++++---- src/FastExcel.php | 2 +- src/Importable.php | 2 +- src/Providers/FastExcelServiceProvider.php | 4 +-- src/SheetCollection.php | 2 +- src/functions/fastexcel.php | 4 +-- tests/ChunkTest.php | 4 +-- tests/Dumb.php | 2 +- tests/FastExcelTest.php | 6 ++-- tests/IssuesTest.php | 6 ++-- tests/TestCase.php | 2 +- 15 files changed, 66 insertions(+), 37 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7ee7d81..30ee12b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -15,7 +15,7 @@ jobs: fail-fast: true matrix: os: [ ubuntu-latest ] - php: [ 8.1, 8.2 ] + php: [ 8.2 ] stability: [ lowest, highest ] steps: diff --git a/README.md b/README.md index 415e0a3..c473c3f 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@

-[![Version](https://poser.pugx.org/rap2hpoutre/fast-excel/version?format=flat)](https://packagist.org/packages/rap2hpoutre/fast-excel) -[![License](https://poser.pugx.org/rap2hpoutre/fast-excel/license?format=flat)](https://packagist.org/packages/rap2hpoutre/fast-excel) +[![Version](https://poser.pugx.org/smart145/fast-excel/version?format=flat)](https://packagist.org/packages/smart145/fast-excel) +[![License](https://poser.pugx.org/smart145/fast-excel/license?format=flat)](https://packagist.org/packages/smart145/fast-excel) [![StyleCI](https://github.styleci.io/repos/128174809/shield?branch=master)](https://github.styleci.io/repos/128174809?branch=master) -[![Tests](https://github.com/rap2hpoutre/fast-excel/actions/workflows/tests.yml/badge.svg)](https://github.com/rap2hpoutre/fast-excel/actions/workflows/tests.yml) -[![Total Downloads](https://poser.pugx.org/rap2hpoutre/fast-excel/downloads)](https://packagist.org/packages/rap2hpoutre/fast-excel) +[![Tests](https://github.com/smart145/fast-excel/actions/workflows/tests.yml/badge.svg)](https://github.com/smart145/fast-excel/actions/workflows/tests.yml) +[![Total Downloads](https://poser.pugx.org/smart145/fast-excel/downloads)](https://packagist.org/packages/smart145/fast-excel) Fast Excel import/export for Laravel, thanks to [Spout](https://github.com/box/spout). See [benchmarks](#benchmarks) below. @@ -16,13 +16,13 @@ See [benchmarks](#benchmarks) below. Install via composer: ``` -composer require rap2hpoutre/fast-excel +composer require smart145/fast-excel ``` Export a Model to `.xlsx` file: ```php -use Rap2hpoutre\FastExcel\FastExcel; +use Smart145\FastExcel\FastExcel; use App\User; // Load users @@ -100,7 +100,7 @@ $users = (new FastExcel)->import('file.xlsx', function ($line) { You may use FastExcel with the optional Facade. Add the following line to ``config/app.php`` under the ``aliases`` key. ````php -'FastExcel' => Rap2hpoutre\FastExcel\Facades\FastExcel::class, +'FastExcel' => Smart145\FastExcel\Facades\FastExcel::class, ```` Using the Facade, you will not have access to the constructor. You may set your export data using the ``data`` method. diff --git a/composer.json b/composer.json index 8600612..e86b459 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "rap2hpoutre/fast-excel", + "name": "smart145/fast-excel", "type": "library", "keywords": [ "laravel", @@ -21,7 +21,7 @@ }, "autoload": { "psr-4": { - "Rap2hpoutre\\FastExcel\\": "src/" + "Smart145\\FastExcel\\": "src/" }, "files": [ "src/functions/fastexcel.php" @@ -29,13 +29,13 @@ }, "autoload-dev": { "psr-4": { - "Rap2hpoutre\\FastExcel\\Tests\\": "tests/" + "Smart145\\FastExcel\\Tests\\": "tests/" } }, "extra": { "laravel": { "providers": [ - "Rap2hpoutre\\FastExcel\\Providers\\FastExcelServiceProvider" + "Smart145\\FastExcel\\Providers\\FastExcelServiceProvider" ] } }, diff --git a/src/Exportable.php b/src/Exportable.php index 4ae7e44..3d5abdf 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -1,8 +1,9 @@ toArray()); + $keys = $this->hasColumnsHeader() + ? array_keys($this->columns_width) + : array_keys(is_array($first_row) ? $first_row : $first_row->toArray()); + $writer->addRow(Row::fromValues($keys, $this->header_style)); // $writer->addRow(WriterEntityFactory::createRowFromArray($keys, $this->header_style)); } + /** + * @return bool + */ + private function hasColumnsHeader() + { + return $this->columns_width && \count($this->columns_width) && is_string(array_keys($this->columns_width)[0]); + } + /** * Prepare collection by removing non string if required. */ @@ -307,4 +325,15 @@ public function rowsStyle(Style $style) return $this; } + + /** + * @param $widths + * @return $this + */ + public function setColumnsWidth($widths) + { + $this->columns_width = $widths; + + return $this; + } } diff --git a/src/Facades/FastExcel.php b/src/Facades/FastExcel.php index 3001fa3..865ab6c 100644 --- a/src/Facades/FastExcel.php +++ b/src/Facades/FastExcel.php @@ -1,21 +1,21 @@