PHPExcel sets font / background color based on cell value

I have sheet-based stretch values

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

What I want to do is set the color of these values ​​if the value is less than 12. I still want to display values ​​greater than 12 and less than 72, so the code I 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 get a fatal error

"Calling the applyFromArray () member function for a non-object"

Pretty new PHP material for this, so any help in getting this work is really appreciated.

thank

+4
source share
3 answers

applyFromArray() is the method of the PHPExcel_Cell object, not the cell value (this is a simple PHP data type if the cell does not contain rich text) .... so you need to call it against the cell

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

, , . . 4.6.23 ( " " ) 08conditionalformatting.php

$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);
+6

, :

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

lil bit:

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

, , $slaHours . , $slaHours 12, . script . , , , .

+1

I eventually resolved this by putting the color 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 answers, really appreciate the help

0
source

Source: https://habr.com/ru/post/1529785/


All Articles