Skip to content

Commit

Permalink
Update Fork / Add Header Support / Start working in width for columns (
Browse files Browse the repository at this point in the history
  • Loading branch information
eliurkis authored Jul 26, 2023
1 parent c6d2b8b commit fd73d22
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
fail-fast: true
matrix:
os: [ ubuntu-latest ]
php: [ 8.1, 8.2 ]
php: [ 8.2 ]
stability: [ lowest, highest ]

steps:
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<img src="https://user-images.githubusercontent.com/36028424/40173202-9a03d68a-5a03-11e8-9968-6b7e3b4f8a1b.png">
</p>

[![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.
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "rap2hpoutre/fast-excel",
"name": "smart145/fast-excel",
"type": "library",
"keywords": [
"laravel",
Expand All @@ -21,21 +21,21 @@
},
"autoload": {
"psr-4": {
"Rap2hpoutre\\FastExcel\\": "src/"
"Smart145\\FastExcel\\": "src/"
},
"files": [
"src/functions/fastexcel.php"
]
},
"autoload-dev": {
"psr-4": {
"Rap2hpoutre\\FastExcel\\Tests\\": "tests/"
"Smart145\\FastExcel\\Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
"Rap2hpoutre\\FastExcel\\Providers\\FastExcelServiceProvider"
"Smart145\\FastExcel\\Providers\\FastExcelServiceProvider"
]
}
},
Expand Down
33 changes: 31 additions & 2 deletions src/Exportable.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace Rap2hpoutre\FastExcel;
namespace Smart145\FastExcel;

use Generator;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use InvalidArgumentException;
Expand All @@ -20,6 +21,12 @@
*/
trait Exportable
{

/**
* @var array
*/
private $columns_width;

/**
* @var Style
*/
Expand Down Expand Up @@ -235,11 +242,22 @@ private function writeHeader($writer, $first_row)
return;
}

$keys = array_keys(is_array($first_row) ? $first_row : $first_row->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.
*/
Expand Down Expand Up @@ -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;
}
}
12 changes: 6 additions & 6 deletions src/Facades/FastExcel.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?php

namespace Rap2hpoutre\FastExcel\Facades;
namespace Smart145\FastExcel\Facades;

use Illuminate\Support\Facades\Facade;

/**
* Class FastExcel.
*
* @method static \Rap2hpoutre\FastExcel\FastExcel data($data)
* @method static \Smart145\FastExcel\FastExcel data($data)
* @method static \Illuminate\Support\Collection import($path, callable $callback = null)
* @method static string export($path, callable $callback = null)
* @method static \Illuminate\Support\Collection importSheets($path, callable $callback = null)
* @method static \Rap2hpoutre\FastExcel\FastExcel configureCsv($delimiter = ',', $enclosure = '"', $encoding = 'UTF-8', $bom = false)
* @method static \Rap2hpoutre\FastExcel\FastExcel configureReaderUsing(?callable $callback = null)
* @method static \Rap2hpoutre\FastExcel\FastExcel configureWriterUsing(?callable $callback = null)
* @method static \Smart145\FastExcel\FastExcel configureCsv($delimiter = ',', $enclosure = '"', $encoding = 'UTF-8', $bom = false)
* @method static \Smart145\FastExcel\FastExcel configureReaderUsing(?callable $callback = null)
* @method static \Smart145\FastExcel\FastExcel configureWriterUsing(?callable $callback = null)
*
* @see \Rap2hpoutre\FastExcel\FastExcel
* @see \Smart145\FastExcel\FastExcel
*/
class FastExcel extends Facade
{
Expand Down
2 changes: 1 addition & 1 deletion src/FastExcel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rap2hpoutre\FastExcel;
namespace Smart145\FastExcel;

use Generator;
use Illuminate\Support\Collection;
Expand Down
2 changes: 1 addition & 1 deletion src/Importable.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rap2hpoutre\FastExcel;
namespace Smart145\FastExcel;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;
Expand Down
4 changes: 2 additions & 2 deletions src/Providers/FastExcelServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rap2hpoutre\FastExcel\Providers;
namespace Smart145\FastExcel\Providers;

use Illuminate\Support\ServiceProvider;

Expand Down Expand Up @@ -30,7 +30,7 @@ public function register()
$data = collect($data);
}

return new \Rap2hpoutre\FastExcel\FastExcel($data);
return new \Smart145\FastExcel\FastExcel($data);
});
}
}
2 changes: 1 addition & 1 deletion src/SheetCollection.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rap2hpoutre\FastExcel;
namespace Smart145\FastExcel;

use Illuminate\Support\Collection;

Expand Down
4 changes: 2 additions & 2 deletions src/functions/fastexcel.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

use Rap2hpoutre\FastExcel\SheetCollection;
use Smart145\FastExcel\SheetCollection;

if (!function_exists('fastexcel')) {
/**
* Return app instance of FastExcel.
*
* @return Rap2hpoutre\FastExcel\FastExcel
* @return Smart145\FastExcel\FastExcel
*/
function fastexcel($data = null)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/ChunkTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Rap2hpoutre\FastExcel\Tests;
namespace Smart145\FastExcel\Tests;

use Rap2hpoutre\FastExcel\FastExcel;
use Smart145\FastExcel\FastExcel;

/**
* Class ChunkTest.
Expand Down
2 changes: 1 addition & 1 deletion tests/Dumb.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rap2hpoutre\FastExcel\Tests;
namespace Smart145\FastExcel\Tests;

use Illuminate\Database\Eloquent\Model;

Expand Down
6 changes: 3 additions & 3 deletions tests/FastExcelTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Rap2hpoutre\FastExcel\Tests;
namespace Smart145\FastExcel\Tests;

use OpenSpout\Common\Entity\Style\Color;
use OpenSpout\Common\Entity\Style\Style;
use Rap2hpoutre\FastExcel\FastExcel;
use Rap2hpoutre\FastExcel\SheetCollection;
use Smart145\FastExcel\FastExcel;
use Smart145\FastExcel\SheetCollection;

/**
* Class FastExcelTest.
Expand Down
6 changes: 3 additions & 3 deletions tests/IssuesTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Rap2hpoutre\FastExcel\Tests;
namespace Smart145\FastExcel\Tests;

use Illuminate\Support\Collection;
use Rap2hpoutre\FastExcel\FastExcel;
use Rap2hpoutre\FastExcel\SheetCollection;
use Smart145\FastExcel\FastExcel;
use Smart145\FastExcel\SheetCollection;

/**
* Class IssuesTest.
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rap2hpoutre\FastExcel\Tests;
namespace Smart145\FastExcel\Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;

Expand Down

0 comments on commit fd73d22

Please sign in to comment.