Skip to content

feat: fast-excel export cell styling via exportFormat #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/Services/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use Maatwebsite\Excel\ExcelServiceProvider;
use OpenSpout\Common\Entity\Style\Style;
use Rap2hpoutre\FastExcel\FastExcel;
use Yajra\DataTables\Contracts\DataTableButtons;
use Yajra\DataTables\Contracts\DataTableScope;
Expand Down Expand Up @@ -674,11 +675,11 @@ public function fastExcelCallback(): Closure
return function ($row) {
$mapped = [];

$this->exportColumns()->each(function (Column $column) use (&$mapped, $row) {
if ($column['exportable']) {
$mapped[$column['title']] = $row[$column['data']];
}
});
$this->exportColumns()
->reject(fn (Column $column) => $column->exportable === false)
->each(function (Column $column) use (&$mapped, $row) {
$mapped[$column->title] = $row[$column->data];
});

return $mapped;
};
Expand All @@ -705,16 +706,23 @@ protected function buildFastExcelFile(): FastExcel
$dataTable = app()->call([$this, 'dataTable'], compact('query'));
$dataTable->skipPaging();

$styles = [];
$this->exportColumns()
->reject(fn (Column $column) => $column->exportable === false || ! $column->exportFormat)
->each(function (Column $column) use (&$styles) {
$styles[$column->title] = (new Style)->setFormat($column->exportFormat);
});

if ($dataTable instanceof QueryDataTable) {
$queryGenerator = function ($dataTable): Generator {
foreach ($dataTable->getFilteredQuery()->cursor() as $row) {
yield $row;
}
};

return new FastExcel($queryGenerator($dataTable));
return (new FastExcel($queryGenerator($dataTable)))->setColumnStyles($styles);
}

return new FastExcel($dataTable->toArray()['data']);
return (new FastExcel($dataTable->toArray()['data']))->setColumnStyles($styles);
}
}
Loading