rap2hpoutre / fast-excel

🦉 Fast Excel import/export for Laravel
MIT License
2.09k stars 246 forks source link

How to apply different columns #297

Open bkmorse opened 1 year ago

bkmorse commented 1 year ago

I am trying to export to an xls file, there is multiple tabs in this export, but it uses the same set of columns, even though each sheet within the export does not contain the same columns

below is an example SheetCollection, is there a way using the FastExcel ->export to set what columns each array within that SheetCollection?

$sheets = new SheetCollection([
    'colors' => [
        'favorite' => 'green',
        'favorite' => 'blue',
        'favorite' => 'orange',
    ],
    'food' => [
        'type'  => 'italian',
        'type'  => 'french',
    ],
    'dogs' => [
        'name' => 'ruffus',
        'name' => 'marley'
    ]
]);

Trying to find a way so all 3 tabs in the spreadsheet use the appropriate columns per tab.

Expected outcome:

3 tabs colors tab would have one column: favorite food tab would have one column: type dogs tab would have one column: name

afiqiqmal commented 1 year ago

Im also trouble to styling it. If we look at the codes, there is no way we can customize each of rows/cells style individually. Even method configureWriterUsing are only works for Options

What we can do, we need to overwrite writeRowsFromCollection in Exportable traits

from this

$all_rows = $collection->map(function ($value) {
           return WriterEntityFactory::createRowFromArray($value);
        })->toArray();

to

$all_rows = $collection->map(function ($value) {
            if ($value instanceof Row) {
                return $value;
            }

            return WriterEntityFactory::createRowFromArray($value);
        })->toArray();

so that, we can use like this

SheetCollection::make([
            new Row(
                [
                    new Cell(
                        'Year',
                        (new Style())->setCellAlignment('center')
                    )
                ],
                null
            ),
            [
                'Jan',
                'Feb',
                'March',
                'April',
            ],
            [
                100,
                299,
                493,
                4944,
            ],
        ])