Skip to content

Commit 0f515ee

Browse files
committed
Xls Conditional Format Improvements
While researching another problem, I noticed that font color was not working as expected for Xls Conditional Formats, at least not when a "non-standard" color is used. In such cases, the color might wind up being rendered as black. The reason is as follows. Xls Writer includes a color palette which is dynamically generated from the (non-Conditional) styles used in the workbook. Any colors used in the workbook are indexes to this dynamic palette. However, Conditional colors use a static palette found in class ColorMap to determine the index, so the determination of index will often not find a match, and, if a match is found, it is not necessarily correct. (Also, the ColorMap method was case-sensitive and needs to be insensitive.) In order to correct this, the `addColor` method in Xls Writer Workbook needs to be accessible to the Conditional logic which is found in Xls Writer Worksheet. This is accomplished by passing the Workbook in the Worksheet's constructor, and changing the method to public, and changing Conditional Font to use this method rather than ColorMap. The logic for Conditional Fill colors is similarly changed. Although Xls Conditional Fill has appeared to just not work, I was finally able to figure out the problem. Excel Xls Conditional Fill with fill type Solid requires that the fill color be specified as startColor, and that endColor be omitted. Our conditional samples used endColor, and are now changed to use startColor instead; the same is true for our online documentation, and for some tests. Xlsx continues to work as expected, and now Xls does at least some of the time. If the condition is one that Excel Xls does not recognize (e.g. cell contains), it will, of course, not work. A surprising situation that also doesn't work is the use of ISODD or ISEVEN in formulas. Those are "add-in functions" which are handled differently than other functions, and I'm not sure how to support them. I will document this in issue #3403. Samples 08_Conditional_Formatting(_2) had produced corrupt Xls versions. This turned out to be because the code was using hash codes to avoid having to write out duplicate conditionals; this is often a good idea, but not in this case. Allowing the duplicates fixes the corruption problem. Conditional Border colors also ought to figure in this change, but the current code does not support Border colors, and I have not yet been able to figure out how to implement it (BIFF format can be very messy to figure out). With this change, I could delete ColorMap altogether. However, it is a public class with a static public method, so maybe someone is using it for a purpose I'm not familiar with. I will just deprecate it.
1 parent 563de5f commit 0f515ee

22 files changed

+144
-63
lines changed

docs/topics/conditional-formatting.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ $conditional->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERA
4343
$conditional->addCondition(80);
4444
$conditional->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_DARKGREEN);
4545
$conditional->getStyle()->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
46-
$conditional->getStyle()->getFill()->getEndColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
46+
$conditional->getStyle()->getFill()->getStartColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
4747

4848
$conditionalStyles = $spreadsheet->getActiveSheet()->getStyle('A1:A10')->getConditionalStyles();
4949
$conditionalStyles[] = $conditional;
@@ -63,7 +63,7 @@ $wizard = $wizardFactory->newRule(\PhpOffice\PhpSpreadsheet\Style\ConditionalFor
6363
$wizard->greaterThan(80);
6464
$wizard->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_DARKGREEN);
6565
$wizard->getStyle()->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
66-
$wizard->getStyle()->getFill()->getEndColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
66+
$wizard->getStyle()->getFill()->getStartColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
6767

6868
$conditional = $wizard->getConditional();
6969
```
@@ -84,7 +84,7 @@ $conditional2->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPER
8484
$conditional2->addCondition(10);
8585
$conditional2->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_DARKRED);
8686
$conditional2->getStyle()->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
87-
$conditional2->getStyle()->getFill()->getEndColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
87+
$conditional2->getStyle()->getFill()->getStartColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
8888

8989
$conditionalStyles = $spreadsheet->getActiveSheet()->getStyle('A1:A10')->getConditionalStyles();
9090
$conditionalStyles[] = $conditional2;
@@ -98,7 +98,7 @@ $wizard = $wizardFactory->newRule(\PhpOffice\PhpSpreadsheet\Style\ConditionalFor
9898
$wizard->lessThan(10);
9999
$wizard->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_DARKGREEN);
100100
$wizard->getStyle()->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
101-
$wizard->getStyle()->getFill()->getEndColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
101+
$wizard->getStyle()->getFill()->getStartColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_GREEN);
102102

103103
$conditional = $wizard->getConditional();
104104
```

samples/ConditionalFormatting/01_Basic_Comparisons.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,16 @@
7474
$yellowStyle->getFill()
7575
->setFillType(Fill::FILL_SOLID)
7676
->getStartColor()->setARGB(Color::COLOR_YELLOW);
77-
$yellowStyle->getFill()
78-
->getEndColor()->setARGB(Color::COLOR_YELLOW);
7977
$yellowStyle->getFont()->setColor(new Color(Color::COLOR_BLUE));
8078
$greenStyle = new Style(false, true);
8179
$greenStyle->getFill()
8280
->setFillType(Fill::FILL_SOLID)
8381
->getStartColor()->setARGB(Color::COLOR_GREEN);
84-
$greenStyle->getFill()
85-
->getEndColor()->setARGB(Color::COLOR_GREEN);
8682
$greenStyle->getFont()->setColor(new Color(Color::COLOR_DARKRED));
8783
$redStyle = new Style(false, true);
8884
$redStyle->getFill()
8985
->setFillType(Fill::FILL_SOLID)
9086
->getStartColor()->setARGB(Color::COLOR_RED);
91-
$redStyle->getFill()
92-
->getEndColor()->setARGB(Color::COLOR_RED);
9387
$redStyle->getFont()->setColor(new Color(Color::COLOR_GREEN));
9488

9589
// Set conditional formatting rules and styles

samples/ConditionalFormatting/02_Text_Comparisons.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,17 @@
7373
$yellowStyle = new Style(false, true);
7474
$yellowStyle->getFill()
7575
->setFillType(Fill::FILL_SOLID)
76-
->getEndColor()->setARGB(Color::COLOR_YELLOW);
76+
->getStartColor()->setARGB(Color::COLOR_YELLOW);
7777
$yellowStyle->getFont()->setColor(new Color(Color::COLOR_BLUE));
7878
$greenStyle = new Style(false, true);
7979
$greenStyle->getFill()
8080
->setFillType(Fill::FILL_SOLID)
81-
->getEndColor()->setARGB(Color::COLOR_GREEN);
81+
->getStartColor()->setARGB(Color::COLOR_GREEN);
8282
$greenStyle->getFont()->setColor(new Color(Color::COLOR_DARKRED));
8383
$redStyle = new Style(false, true);
8484
$redStyle->getFill()
8585
->setFillType(Fill::FILL_SOLID)
86-
->getEndColor()->setARGB(Color::COLOR_RED);
86+
->getStartColor()->setARGB(Color::COLOR_RED);
8787
$redStyle->getFont()->setColor(new Color(Color::COLOR_GREEN));
8888

8989
// Set conditional formatting rules and styles

samples/ConditionalFormatting/03_Blank_Comparisons.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@
4545
$greenStyle = new Style(false, true);
4646
$greenStyle->getFill()
4747
->setFillType(Fill::FILL_SOLID)
48-
->getEndColor()->setARGB(Color::COLOR_GREEN);
48+
->getStartColor()->setARGB(Color::COLOR_GREEN);
4949
$greenStyle->getFont()->setColor(new Color(Color::COLOR_DARKRED));
5050
$redStyle = new Style(false, true);
5151
$redStyle->getFill()
5252
->setFillType(Fill::FILL_SOLID)
53-
->getEndColor()->setARGB(Color::COLOR_RED);
53+
->getStartColor()->setARGB(Color::COLOR_RED);
5454
$redStyle->getFont()->setColor(new Color(Color::COLOR_GREEN));
5555

5656
// Set conditional formatting rules and styles

samples/ConditionalFormatting/04_Error_Comparisons.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@
4848
$greenStyle = new Style(false, true);
4949
$greenStyle->getFill()
5050
->setFillType(Fill::FILL_SOLID)
51-
->getEndColor()->setARGB(Color::COLOR_GREEN);
51+
->getStartColor()->setARGB(Color::COLOR_GREEN);
5252
$greenStyle->getFont()->setColor(new Color(Color::COLOR_DARKRED));
5353
$redStyle = new Style(false, true);
5454
$redStyle->getFill()
5555
->setFillType(Fill::FILL_SOLID)
56-
->getEndColor()->setARGB(Color::COLOR_RED);
56+
->getStartColor()->setARGB(Color::COLOR_RED);
5757
$redStyle->getFont()->setColor(new Color(Color::COLOR_GREEN));
5858

5959
// Set conditional formatting rules and styles

samples/ConditionalFormatting/05_Date_Comparisons.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
$yellowStyle = new Style(false, true);
112112
$yellowStyle->getFill()
113113
->setFillType(Fill::FILL_SOLID)
114-
->getEndColor()->setARGB(Color::COLOR_YELLOW);
114+
->getStartColor()->setARGB(Color::COLOR_YELLOW);
115115
$yellowStyle->getFont()->setColor(new Color(Color::COLOR_BLUE));
116116

117117
// Set conditional formatting rules and styles

samples/ConditionalFormatting/06_Duplicate_Comparisons.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@
5454
$yellowStyle = new Style(false, true);
5555
$yellowStyle->getFill()
5656
->setFillType(Fill::FILL_SOLID)
57-
->getEndColor()->setARGB(Color::COLOR_YELLOW);
57+
->getStartColor()->setARGB(Color::COLOR_YELLOW);
5858
$yellowStyle->getFont()->setColor(new Color(Color::COLOR_BLUE));
5959
$greenStyle = new Style(false, true);
6060
$greenStyle->getFill()
6161
->setFillType(Fill::FILL_SOLID)
62-
->getEndColor()->setARGB(Color::COLOR_GREEN);
62+
->getStartColor()->setARGB(Color::COLOR_GREEN);
6363
$greenStyle->getFont()->setColor(new Color(Color::COLOR_DARKRED));
6464

6565
// Set conditional formatting rules and styles

samples/ConditionalFormatting/07_Expression_Comparisons.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@
7373
$yellowStyle = new Style(false, true);
7474
$yellowStyle->getFill()
7575
->setFillType(Fill::FILL_SOLID)
76-
->getEndColor()->setARGB(Color::COLOR_YELLOW);
76+
->getStartColor()->setARGB(Color::COLOR_YELLOW);
7777
$yellowStyle->getFont()->setColor(new Color(Color::COLOR_BLUE));
7878
$greenStyle = new Style(false, true);
7979
$greenStyle->getFill()
8080
->setFillType(Fill::FILL_SOLID)
81-
->getEndColor()->setARGB(Color::COLOR_GREEN);
81+
->getStartColor()->setARGB(Color::COLOR_GREEN);
8282
$greenStyle->getFont()->setColor(new Color(Color::COLOR_DARKRED));
8383

8484
$greenStyleMoney = clone $greenStyle;

src/PhpSpreadsheet/Reader/Xls.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7493,11 +7493,15 @@ private function getCFFillStyle(string $options, Style $style): void
74937493

74947494
// bit: 0-6; mask: 0x007F; type
74957495
$color1 = (0x007F & $fillColors) >> 0;
7496-
$style->getFill()->getStartColor()->setRGB(Xls\Color::map($color1, $this->palette, $this->version)['rgb']);
74977496

74987497
// bit: 7-13; mask: 0x3F80; type
74997498
$color2 = (0x3F80 & $fillColors) >> 7;
7500-
$style->getFill()->getEndColor()->setRGB(Xls\Color::map($color2, $this->palette, $this->version)['rgb']);
7499+
if ($fillPattern === Fill::FILL_SOLID) {
7500+
$style->getFill()->getStartColor()->setRGB(Xls\Color::map($color2, $this->palette, $this->version)['rgb']);
7501+
} else {
7502+
$style->getFill()->getStartColor()->setRGB(Xls\Color::map($color1, $this->palette, $this->version)['rgb']);
7503+
$style->getFill()->getEndColor()->setRGB(Xls\Color::map($color2, $this->palette, $this->version)['rgb']);
7504+
}
75017505
}
75027506
}
75037507

src/PhpSpreadsheet/Writer/Xls.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function save($filename, int $flags = 0): void
119119
// Initialise worksheet writers
120120
$countSheets = $this->spreadsheet->getSheetCount();
121121
for ($i = 0; $i < $countSheets; ++$i) {
122-
$this->writerWorksheets[$i] = new Worksheet($this->strTotal, $this->strUnique, $this->strTable, $this->colors, $this->parser, $this->preCalculateFormulas, $this->spreadsheet->getSheet($i));
122+
$this->writerWorksheets[$i] = new Worksheet($this->strTotal, $this->strUnique, $this->strTable, $this->colors, $this->parser, $this->preCalculateFormulas, $this->spreadsheet->getSheet($i), $this->writerWorkbook);
123123
}
124124

125125
// build Escher objects. Escher objects for workbooks needs to be build before Escher object for workbook.

0 commit comments

Comments
 (0)