fast-excel
                                
                                 fast-excel copied to clipboard
                                
                                    fast-excel copied to clipboard
                            
                            
                            
                        callback function not working
function myGenerator() {
    $users = get_users_as_you_want();
    foreach($users as $user) {
        yield $user;
    }
}
(new FastExcel(myGenerator()))->export('test.xlsx', function ($user) {
return [
    'name' => $user->name
]
});
when i using generator the callback function not working . the export file has all of the column.
I'm having the same issue, the final file contains all columns
(new FastExcel($this->rowGenerator($this->sector_number)))
            ->export(storage_path('app/public/exports/' . $this->file_name), function($row) {
                return [
                    'id INEGI' => $row->inegi_id,
                    'Sector económico' => $row->codigo_act,
                    'Nombre comercial' => $row->nom_estab,
                    'Razón social' => $row->raz_social,
                    'PaÃs' => 'MÉXICO',
                    'Estado' => $row->entidad,
                    'Municipio alcaldÃa' => $row->municipio,
                    'Colonia localidad' => $row->nomb_asent . ', ' . $row->localidad,
                    'Calle y número' => $row->nom_vial . ' ' . $row->numero_ext . ' ' . $row->numero_int,
                    'Código postal' => $row->cod_postal,
                    'Latitud' => $row->latitud,
                    'Longitud' => $row->longitud,
                    'Correo electrónico' => $row->correoelec,
                    'Web' => $row->www,
                    'Teléfono oficina' => $row->telefono,
                ];
            });
Hi, thank you for your contribution. Callbacks are not implemented with generators. I think there can be avoided by directly building the right format in the generator itself.
@ehsanhoushmand code could be:
function myGenerator() {
    $users = get_users_as_you_want();
    foreach($users as $user) {
        yield ['name' => $user->name];
    }
}
(new FastExcel(myGenerator()))->export('test.xlsx');
And @juanhuerta code could be:
function rowGenerator() {
    // ...
    yield [
        'id INEGI' => $row->inegi_id,
        'Sector económico' => $row->codigo_act,
        'Nombre comercial' => $row->nom_estab,
        'Razón social' => $row->raz_social,
        'PaÃs' => 'MÉXICO',
        'Estado' => $row->entidad,
        // ...
    ];
}
(new FastExcel(rowGenerator($this->sector_number)))
    ->export(storage_path('app/public/exports/' . $this->file_name));
I agree there is a problem: this lib silently ignore the callback. So now we have (at least) two solutions:
- accept callbacks
- throw an exception if callback is used with generator
What are your thoughts @ehsanhoushmand & @juanhuerta?
@rap2hpoutre if we can modify exportOrDownload function this way:
    private function exportOrDownload($path, $function, callable $beforeCallback = null, callable $afterCallback = null, callable $callback = null)
    {
        $writer = WriterFactory::create($this->getType($path));
        $this->setOptions($writer);
        /* @var \Box\Spout\Writer\WriterInterface $writer */
        $writer->$function($path);
        if ($beforeCallback) $beforeCallback($writer);
        ... other stuff
        if ($afterCallback) $afterCallback($writer);
        $writer->close();
    }
It will also be good to be able to obtain the $writer instance. This would save you a lot of stress from people who want to play with the $writer instance.