Laravel 9 - maatwebsite/excel conditional formatting

259 Views Asked by At

Using Laravel 9 and maatwebsite excel version 3.1.

When exporting a file from an array, on each line I need to check: If column C is empty, make column A bold.

I read the documentation and saw some examples but I can't understand how to do that.

I have this function in my export class:

public function registerEvents(): array
    {        
        return [
            AfterSheet::class => function(AfterSheet $event) {
        
            //If cell C ('day') is empty, make cell A bold
            $cellRange = 'C';
            $conditional1 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
            $conditional1->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS);
            $conditional1->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional:: CONDITION_CONTAINSBLANKS);
            $conditional1->addCondition('0');                
            $conditional1->getStyle()->getFont()->setBold(true);

            $conditionalStyles = $event->sheet->getDelegate()->getStyle('C')->getConditionalStyles();
            $conditionalStyles[] = $conditional1;
            
            $event->sheet->getDelegate()->getStyle('A')->setConditionalStyles($conditionalStyles);
            },
        ];
    }

Then when I try to open the exported file, I get this message:

We found a problem with some content. Do you want us to recover as much as we can? If you trust the source of this workbook, click Yes.

I open the file anyway, and the column that is supposed to be bold is not bold.

It is clear I don't fully understand the documentation of it all.

Can someone please help?

1

There are 1 best solutions below

0
Sigal Zahavi On

I got an answer on Laracast: Laracast answer