Question

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

Was it helpful?

Solution

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);

OTHER TIPS

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.

I eventually resolved it with this by putting the colour in the css sheet and adding the following two lines of code:

if ($slaHours >= 0 & $slaHours <= 12)
{
    $slaHours = "<b id =\"colorsla\">" . $slaHours . "</b>";
}

Thanks for all the responses, really appreciate the help

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top