Laravel-excel: registerEvents > AfterSheet $event->sheet doesn't allow to transform to array

Created on 30 Oct 2018  路  1Comment  路  Source: Maatwebsite/Laravel-Excel

Prerequisites

  • [x ] Able to reproduce the behaviour outside of your code, the problem is isolated to Laravel Excel.
  • [x ] Checked that your issue isn't already filed.
  • [ x] Checked if no PR was submitted that fixes this problem.

Versions

  • PHP version: 7.1.23
  • Laravel version: 5.6
  • Package version: 3.1.2

Description

When i ran composer install the new version of Laravel Excel was installed (3.1.2) this broke the ->toArray() method that was available to get rows using the AfterSheet Event.

Steps to Reproduce

<?php

namespace App\Http\Exports;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Origin\Models\User;
use Origin\Services\Utilities\CollectionUtils;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\Fill;

class AnalyticsReport implements FromView, ShouldAutoSize, withEvents
{

    public function __construct($collection){
        $this->collection = $collection;
    }

    public function description()
    {
        return 'Analytics Report';
    }

    private function getNameFromNumber($num) {
        $numeric = ($num - 1) % 26;
        $letter = chr(65 + $numeric);
        $num2 = intval(($num - 1) / 26);

        if ($num2 > 0) {
            return $this->getNameFromNumber($num2) . $letter;
        } else {
            return $letter;
        }
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function(AfterSheet $event) {
                $rows = $event->sheet->toArray();
                $breakdown = false;

                # Styles Here
                $event->sheet->getParent()->getDefaultStyle()->applyFromArray([
                    'font' => [
                        'name' => 'Arial',
                        'size' => 12
                    ]
                ]);

                foreach ($rows as $k => $v) {
                    $lastLetter = $this->getNameFromNumber(count($v));

                    if($k === 0){
                        $event->sheet->getStyle('A1:'.$lastLetter.'1')->applyFromArray([
                            'font' => [
                                'name' => 'Arial',
                                'size' => 14,
                                'bold' => true,
                                'color' => [
                                    'argb' => 'FFFFFFFF'
                                 ]
                            ],
                            'fill' => [
                                'fillType' => Fill::FILL_SOLID,
                                'color' => [
                                    'argb' => 'FF37474f'
                                ]
                            ]
                        ]);
                    }else if ($k === 1){
                        $event->sheet->getStyle('A2:'.$lastLetter.'2')->applyFromArray([
                            'font' => [
                                'name' => 'Arial',
                                'size' => 12,
                                'bold' => true,
                                'color' => [
                                    'argb' => 'FFFFFFFF'
                                ]
                            ],
                            'fill' => [
                                'fillType' => Fill::FILL_SOLID,
                                'color' => [
                                    'argb' => 'FF3949ab'
                                ]
                            ]
                        ]);
                    }else{
                        if(!is_numeric($v[0])){
                            $event->sheet->getStyle('A'.($k + 1))->applyFromArray([
                                'font' => [
                                    'name' => 'Arial',
                                    'size' => 12,
                                    'bold' => true
                                ]
                            ]);
                        }

                        if($v[0] === 'Hover and Click Breakdown'){
                            $breakdown = true;

                            $event->sheet->getStyle('A'.($k+1).':'.$lastLetter.($k+1))->applyFromArray([
                                'fill' => [
                                    'fillType' => Fill::FILL_SOLID,
                                    'color' => [
                                        'argb' => 'FF3949ab'
                                    ]
                                ],
                                'font' => [
                                    'color' => [
                                        'argb' => 'FFFFFFFF'
                                    ]
                                ]
                            ]);
                        }

                        if(!$breakdown){
                            if(!empty($v[0])){
                                $event->sheet->getStyle('A'.($k+1).':'.$lastLetter.($k+1))->applyFromArray([
                                    'font' => [
                                        'name' => 'Arial',
                                        'size' => 12,
                                        'bold' => true
                                    ],
                                    'fill' => [
                                        'fillType' => Fill::FILL_SOLID,
                                        'color' => [
                                            'argb' => 'FFcfd8dc'
                                        ]
                                    ]
                                ]);
                            }
                        }else{
                            if($v[1] === 'Component ID'){
                                $event->sheet->getStyle('A'.($k+1).':'.$lastLetter.($k+1))->applyFromArray([
                                    'fill' => [
                                        'fillType' => Fill::FILL_SOLID,
                                        'color' => [
                                            'argb' => 'FFcfd8dc'
                                        ]
                                    ],
                                    'font' => [
                                        'bold' => true
                                    ]
                                ]);
                            }

                            $event->sheet->getStyle('C'.($k+1))->applyFromArray([
                                'font' => [
                                    'bold' => true
                                ]
                            ]);
                        }
                    }
                }
            }
        ];
    }

    public function view(): View
    {

        return view('exports.analytics', [
            'data' => $this->collection->toArray()
        ]);
    }
}

Expected behavior:

Sheet to array in order to get rows.

Actual behavior:

Exception: Too few arguments to function Maatwebsite\Excel\Sheet::toArray(), 0 passed in ...\app\Http\Exports\AnalyticsReport.php on line 42 and at least 1 expected

Additional Information

Everything works on version: 3.0.9

Most helpful comment

Hey @EM-LilianaIturribarria sheet->toArray() was intended at being an internal method until the release of 3.1 (Imports). However that toArray() method is enriched with some 3.1 goodies.

You could use the native toArray method by getting it via the delegate. (Before you got it via a magic call to the delegate)

$rows = $event->sheet->getDelegate()->toArray();

>All comments

Hey @EM-LilianaIturribarria sheet->toArray() was intended at being an internal method until the release of 3.1 (Imports). However that toArray() method is enriched with some 3.1 goodies.

You could use the native toArray method by getting it via the delegate. (Before you got it via a magic call to the delegate)

$rows = $event->sheet->getDelegate()->toArray();
Was this page helpful?
0 / 5 - 0 ratings

Related issues

amine8ghandi8amine picture amine8ghandi8amine  路  3Comments

vandolphreyes picture vandolphreyes  路  3Comments

dr3ads picture dr3ads  路  3Comments

kurianic picture kurianic  路  3Comments

contifico picture contifico  路  3Comments