PHPOffice / PHPExcel

ARCHIVED
Other
11.46k stars 4.19k forks source link

Conditional Formatting Excel2007 does not work for versions earlier than Excel 2016 #1326

Closed juan-nunez closed 6 years ago

juan-nunez commented 6 years ago

Hello all,

I am running into an issue with conditional formatting. I can apply conditional formatting to a file but unfortunately, only certain versions of excel are able to properly open the file. The ones that cannot open the file correctly allow me to "repair" them. When I hit repair, the conditional formatting is stripped from the file. I have posted the versions that work/do not work down under. Does anyone have any idea as to why this is happening or let me know what I can do to fix the problem?

Here is the code. Note: For testing purposes, I simplified the code to just include a simple expression such as "=A1=1" and it still does not work so it just seems to be expressions in general and not just ones that are more complex. I am using the 2007 writer.

protected function applyConditionalHighlighting(PHPExcel_Worksheet $sheet, $column)
    {
        $startCell = self::STARTCELL;
        $color = self::HIGHLIGHTCOLOR;
        $maxColumn = $sheet->getHighestColumn();
        $maxRow = $sheet->getHighestRow();
        $connectSheetName = self::CONNECT_SHEET;

        $conditional = new PHPExcel_Style_Conditional();
        $conditional->setConditionType(PHPExcel_Style_Conditional::CONDITION_EXPRESSION);
        $condition = '=MATCH($' . "$startCell,'$connectSheetName'!$" . $column . ':$' . $column . ",0)>0";
        $conditional->addCondition($condition);
        $conditional->getStyle()->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getEndColor()->setARGB($color);
        $conditionalStyles = $sheet->getStyle($startCell)->getConditionalStyles();
        $conditionalStyles[] = $conditional;
        $sheet->getStyle("$startCell:$maxColumn$maxRow")->setConditionalStyles($conditionalStyles);
    }

Version results:

Windows: Excel 2016: Works Excel 2013: Does not work Excel 2010: Does not work

Mac: Excel 2016: Works Excel 2011: Does not work OpenOffice latest stable version: Works LibreOffice latest stable version: Works

Browser: Microsoft Excel Online: Works

juan-nunez commented 6 years ago

I figured out the problem. If you look at my code, I have the following line:

$condition = '=MATCH($' . "$startCell,'$connectSheetName'!$" . $column . ':$' . $column . ",0)>0";

The equal sign is not needed.