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.
<?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
Everything works on version: 3.0.9
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();
Most helpful comment
Hey @EM-LilianaIturribarria
sheet->toArray()was intended at being an internal method until the release of 3.1 (Imports). However thattoArray()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)