Laravel-excel: [QUESTION] How to enable AutoFilter for header row?

Created on 20 Mar 2019  路  2Comments  路  Source: Maatwebsite/Laravel-Excel

Versions

  • PHP version: 7.2.14
  • Laravel version: 5.6.39
  • Package version: 3.1.10

Description

I'am migrating the laravel-excel package from 2.1 to 3.1.10 but i have many questions about this, the first is

  • How i can enable header autofilter?, in the 2.1 version we enabled with setAutoFilterByColumnAndRow() or setAutoFilter(), but in this case i don't see any of this in the documentation, example:
    /**
     * @return array
     * Version 3.1.10
     */
    public function headings(): array
    {
        if ( sizeof($this->data) > 0 ) {
            return array_keys($this->data[0]);
        }

        return [];
    }
    /**
     * Version 2.1
     */
    $sheet->row(1)->setAutoFilterByColumnAndRow(sizeof(array_values($data[0])) - 1,1);
question

Most helpful comment

Thanks @patrickbrouwers it work's fine

I implement the interface WithEvents

And my registerEvents method was this:

    /**
     * @return array
     */
    public function registerEvents(): array
    {
        return array(
            AfterSheet::class => function(AfterSheet $event) {
                $event->sheet->getDelegate()->setAutoFilter('A1:'.$event->sheet->getDelegate()->getHighestColumn().'1');
            }
        );
    }

Thanks!

All 2 comments

You need to use the PhpSpreadsheet method in an extending event.

See extending documentation: https://docs.laravel-excel.com/3.1/exports/extending.html#events

And their docs about the auto filter functionality: https://phpspreadsheet.readthedocs.io/en/stable/topics/autofilters/

Thanks @patrickbrouwers it work's fine

I implement the interface WithEvents

And my registerEvents method was this:

    /**
     * @return array
     */
    public function registerEvents(): array
    {
        return array(
            AfterSheet::class => function(AfterSheet $event) {
                $event->sheet->getDelegate()->setAutoFilter('A1:'.$event->sheet->getDelegate()->getHighestColumn().'1');
            }
        );
    }

Thanks!

Was this page helpful?
0 / 5 - 0 ratings