Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel set font/background colour based on cell value

Tags:

php

phpexcel

I have a sheet pulling values based on

if ($slaHours >= 0 & $slaHours <= 72) 

What I want to do is set the colour of those values if the value is less than 12. I still want to display the values greater than 12 and less than 72 so the code I've tried is

$styleArray = array(
'font'  => array(
    'bold'  => true,
    'color' => array('rgb' => 'FF0000'),
));

if ($slaHours >= 0 & $slaHours <= 72)
    $slaHours = $objWorksheet->getCellByColumnAndRow(3, $row)->getValue();
    if($slaHours <=12) {    
        $slaHours = $objWorksheet->getCellByColumnAndRow(3, $row)->getValue()->applyFromArray($styleArray); 

However, I'm getting a fatal error of

"Call to a member function applyFromArray() on a non-object "

Pretty new to this PHP stuff so any help in getting this working is greatly appreciated.

Thanks

like image 658
nixxrite Avatar asked Mar 03 '14 11:03

nixxrite


People also ask

How do I merge cells in Excel using PhpExcel?

$sheet = $workbook->getActiveSheet(); $sheet->setCellValue('A1','A pretty long sentence that deserves to be in a merged cell'); $sheet->mergeCells('A1:C1');


2 Answers

applyFromArray() is a method of the PHPExcel_Cell object, not of the cell value (which is a simple PHP datatype unless the cell contains rich text).... so you need to call it against the cell

$objWorksheet->getCellByColumnAndRow(3, $row)->applyFromArray;

but if you need a style based on the value of the cell, then you should use conditional styles. See section 4.6.23 of the developer documentation ("Conditional formatting a cell") for details, and look at 08conditionalformatting.php in the Examples

EDIT

$objConditionalStyle = new PHPExcel_Style_Conditional();
$objConditionalStyle->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS)
    ->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_LESSTHAN)
    ->addCondition('12');
$objConditionalStyle->getStyle()->getFont()->getColor()->setRGB('FF0000');
$objConditionalStyle->getStyle()->getFont()->setBold(true);

$conditionalStyles = $objWorksheet->getStyle('A3')
    ->getConditionalStyles();
array_push($conditionalStyles, $objConditionalStyle);
$objPHPExcel->getActiveSheet()->getStyle('A3')
    ->setConditionalStyles($conditionalStyles);
like image 85
Mark Baker Avatar answered Nov 15 '22 01:11

Mark Baker


You probably want to call this:

$slaHours = $objWorksheet->getCellByColumnAndRow(3,$row)->applyFromArray($styleArray);

EDIT

I've changed your code a lil bit:

$cell = $objWorksheet->getCellByColumnAndRow(3, $row);
if($slaHours >= 0 && $slaHours <= 72)
    $slaHours = $cell->getValue();
if($slaHours <=12) {    
    $cell->applyFromArray($styleArray);

So since the first condition is true, your $slaHours is set. After that if $slaHours is smaller or equals 12 the color is changed. The script also caches your cell. If you have this in a loop this will probably save you some memory.

like image 39
Tobias Golbs Avatar answered Nov 15 '22 01:11

Tobias Golbs