Hello,
I am trying to apply styling to my sheet. However, I get this error;
BadMethodCallException : Method Maatwebsite\Excel\Sheet::styleCells does not exist.
Below is my Exports file
namespace App\Exports;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\BeforeExport;
use Maatwebsite\Excel\Events\AfterSheet;
class MitieReport implements FromView, ShouldAutoSize, WithEvents
{
use Exportable;
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$event->sheet->styleCells(
'B2:G8',
[
'borders' => [
'outline' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
'color' => ['argb' => 'FFFF0000'],
],
],
'font' => [
'bold' => true,
]
]
);
},
];
}
public function view(): View
{
return view('reports.weeklyReport');
}
}
Thanks for submitting the ticket. Unfortunately the information you provided is incomplete. We need to know which version you use and how to reproduce it. Please include code examples. Before we can pick it up, please check (https://github.com/Maatwebsite/Laravel-Excel/blob/3.0/.github/ISSUE_TEMPLATE.md) and add the missing information. To make processing of this ticket a lot easier, please make sure to check (https://laravel-excel.maatwebsite.nl/docs/3.0/getting-started/contributing) and double-check if you have filled in the issue template correctly. This will allow us to pick up your ticket more efficiently. Issues that follow the guidelines correctly will get priority over other issues.
Hi Gareth,
Did you see/check the macro
Use \Maatwebsite\Excel\Sheet;
Sheet::macro('styleCells', function (Sheet $sheet, string $cellRange, array style) {
$sheet->getDelegate()->getStyle($cellRange)->applyFromArray($style);
});
on https://laravel-excel.maatwebsite.nl/docs/3.0/export/extending#docs
I did see this, but I couldn't figure out how to implement it @m-bosch
Hi @Gareth-3aaa
You can put your 'styleCells' macro definition as @m-bosch pointed out in App\Providers\AppServiceProvider boot()
Or you can make your own provider using
php artisan make:provider YourMacroServiceProvider
and register it in config/app.php and then you can put your all macro definitions inside the boot method of your new provider.
You might want to read more about Laravel macros here or here
Good luck!
Thanks for the extra info @m-bosch and @RoyIrwan as I was having the same issue as @Gareth-3aaa
For anyone else running into this, it looks like there is a typo in the sample code for styleCells (third parameter "array style" should be "array $style"):
use \Maatwebsite\Excel\Sheet;
Sheet::macro('styleCells', function (Sheet $sheet, string $cellRange, array style) {
$sheet->getDelegate()->getStyle($cellRange)->applyFromArray($style);
});
Should be:
use \Maatwebsite\Excel\Sheet;
Sheet::macro('styleCells', function (Sheet $sheet, string $cellRange, array $style) {
$sheet->getDelegate()->getStyle($cellRange)->applyFromArray($style);
});
Most helpful comment
Hi @Gareth-3aaa
You can put your 'styleCells' macro definition as @m-bosch pointed out in
App\Providers\AppServiceProvider boot()Or you can make your own provider using
php artisan make:provider YourMacroServiceProviderand register it in
config/app.phpand then you can put your all macro definitions inside the boot method of your new provider.You might want to read more about Laravel macros here or here
Good luck!