Search code examples
phplaravelconditional-formattingphpspreadsheetmaatwebsite-excel

phpspreadsheet conditional format column A if value of column B is less than 700


Currently, I am exporting an excel sheet using maatwebsite 3.1 export in laravel 5.6. I am using phpspreadsheet concepts for conditional formatting


$conditional_fico = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
$conditional_fico->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS)
->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_LESSTHAN)
                            ->addCondition('700');
$conditional_fico->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
$conditionalStyles = $event->sheet->getStyle('G:G')->getConditionalStyles();
$conditionalStyles[] = $conditional_fico;
$event->sheet->getStyle('G:G')->setConditionalStyles($conditionalStyles);

Now i want to Change the color of A:A if respective G:G is less than 700. Example :

make A1 red if G1 < 700; make A2 red if G2 <700 and so on.

Please share the corresponding conditional formatting rule


Solution

  • I figured it out,

      $styleArray = array(
                'font'  => array(
                  //  'bold'  => true,
                    'color' => array('rgb' => 'FF0000'),
                ));
    
       $highestRow = $event->sheet->getHighestRow();
       $highestColumn = $event->sheet->getHighestColumn(); 
       $highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); 
      for ($row = 3; $row <= $highestRow; ++$row) {
                $slaHours = $event->sheet->getCellByColumnAndRow(7, $row)->getValue();
                if( $slaHours < 700) {    
                $event->sheet->getCellByColumnAndRow(2, $row)->getStyle()->applyFromArray($styleArray);
           }
       }