Setting background color always makes cell black

Topics: Developer Forum, User Forum
May 6, 2010 at 4:14 PM

Hi. I am writing some conditional formatting rules. I got the background fill to work but in the end it always generates a black background cell rather than the color I would like. I've tried to do this in a number of ways but it keeps becoming black. Below is my code. If anyone could help that'd be appreciated!

 

    $objConditional1 = new PHPExcel_Style_Conditional();
    $objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS);
    $objConditional1->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_GREATERTHANOREQUAL);
    $objConditional1->addCondition('.9');
    $objConditional1->getStyle()->getFill()->applyFromArray(
            array(
            'type'       => PHPExcel_Style_Fill::FILL_SOLID,
            'startcolor' => array('rgb' => 'E9E9E9'),
            )
    );
    $objConditional1->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE);

Another variation I tried:

 

    $objConditional1 = new PHPExcel_Style_Conditional();
    $objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS);
    $objConditional1->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_GREATERTHANOREQUAL);
    $objConditional1->addCondition('.9');
    $objConditional1->getStyle()->applyFromArray(
            array('fill' 	=> array(
                    'type'		=> PHPExcel_Style_Fill::FILL_SOLID,
                    'color'		=> array('rgb' => '00FF00')
            ),
            )
    );
    $objConditional1->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE);

and yet another

    $objConditional1 = new PHPExcel_Style_Conditional();
    $objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS);
    $objConditional1->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_GREATERTHANOREQUAL);
    $objConditional1->addCondition('.9');
    $objConditional1->getStyle()->getFill()->getStartColor()->setRGB('00FF00');

    $objConditional1->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE);

Any help would be much appreciated!

 

Thanks

May 7, 2010 at 2:29 AM

Color code must have 8 digits

You can see color code in the document

 

good luck

May 7, 2010 at 2:55 AM
'startcolor' => array('rgb','E9E9E9')   


change to...

'startcolor' => array('argb','E9E9E9')  
 

 

 

 

May 7, 2010 at 2:21 PM

Hey I ended up solving it. You only need 6 characters for rgb (vs argb) but I found the problem was with FILL_SOLID you still need an end color and it simply has to match the start color

 

    $objConditional1 = new PHPExcel_Style_Conditional();
    $objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS);
    $objConditional1->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_GREATERTHANOREQUAL);
    $objConditional1->addCondition('.9');
    $objConditional1->getStyle()->getFill()->applyFromArray(
            array(
            'type'       => PHPExcel_Style_Fill::FILL_SOLID,
            'startcolor' => array('rgb' => '00FF00'),
            'endcolor' => array('rgb' => '00FF00')

            )
    );

    $objConditional1->getStyle()->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE);

Above is my final code