Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel conditional formatting when cell equals string

Tags:

php

phpexcel

The following:

$objConditional1 = new PHPExcel_Style_Conditional();
$objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT)
                ->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_CONTAINSTEXT)
                ->addCondition("Bla bla");
$objConditional1->getStyle()->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getEndColor()->setARGB(PHPExcel_Style_Color::COLOR_GREEN);   


$conditionalStyles = $sheet->getStyle('I2')->getConditionalStyles();
array_push($conditionalStyles, $objConditional1);               
$sheet->getStyle('I$2:I$10000')->setConditionalStyles($conditionalStyles);

Generates an excel document and says that the conditional formatting has issues and that it will remove it...

The correct result i'm looking for is when column equals "Bla bla" fill the box in green

like image 875
Jimmyt1988 Avatar asked Jul 09 '14 13:07

Jimmyt1988


1 Answers

The change is instead of addCondition, you have to call setText... Which is hard to infer, but whatever... cool I got it to work.

$objConditional1 = new PHPExcel_Style_Conditional();
$objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT)
                ->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_CONTAINSTEXT)
                ->setText("Bla bla");
$objConditional1->getStyle()->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getEndColor()->setARGB(PHPExcel_Style_Color::COLOR_GREEN);   


$conditionalStyles = $sheet->getStyle('I2')->getConditionalStyles();
array_push($conditionalStyles, $objConditional1);               
$sheet->getStyle('I$2:I$10000')->setConditionalStyles($conditionalStyles);
like image 75
Jimmyt1988 Avatar answered Nov 13 '22 14:11

Jimmyt1988