3
3
namespace PhpOffice \PhpSpreadsheet \Reader \Xlsx ;
4
4
5
5
use PhpOffice \PhpSpreadsheet \Reader \Xlsx \Styles as StyleReader ;
6
+ use PhpOffice \PhpSpreadsheet \Style \Color ;
6
7
use PhpOffice \PhpSpreadsheet \Style \Conditional ;
8
+ use PhpOffice \PhpSpreadsheet \Style \ConditionalFormatting \ConditionalColorScale ;
7
9
use PhpOffice \PhpSpreadsheet \Style \ConditionalFormatting \ConditionalDataBar ;
8
10
use PhpOffice \PhpSpreadsheet \Style \ConditionalFormatting \ConditionalFormattingRuleExtension ;
9
11
use PhpOffice \PhpSpreadsheet \Style \ConditionalFormatting \ConditionalFormatValueObject ;
@@ -25,11 +27,14 @@ class ConditionalStyles
25
27
26
28
private array $ dxfs ;
27
29
28
- public function __construct (Worksheet $ workSheet , SimpleXMLElement $ worksheetXml , array $ dxfs = [])
30
+ private StyleReader $ styleReader ;
31
+
32
+ public function __construct (Worksheet $ workSheet , SimpleXMLElement $ worksheetXml , array $ dxfs , StyleReader $ styleReader )
29
33
{
30
34
$ this ->worksheet = $ workSheet ;
31
35
$ this ->worksheetXml = $ worksheetXml ;
32
36
$ this ->dxfs = $ dxfs ;
37
+ $ this ->styleReader = $ styleReader ;
33
38
}
34
39
35
40
public function load (): void
@@ -45,13 +50,13 @@ public function load(): void
45
50
$ this ->worksheet ->setSelectedCells ($ selectedCells );
46
51
}
47
52
48
- public function loadFromExt (StyleReader $ styleReader ): void
53
+ public function loadFromExt (): void
49
54
{
50
55
$ selectedCells = $ this ->worksheet ->getSelectedCells ();
51
56
52
57
$ this ->ns = $ this ->worksheetXml ->getNamespaces (true );
53
58
$ this ->setConditionalsFromExt (
54
- $ this ->readConditionalsFromExt ($ this ->worksheetXml ->extLst , $ styleReader )
59
+ $ this ->readConditionalsFromExt ($ this ->worksheetXml ->extLst )
55
60
);
56
61
57
62
$ this ->worksheet ->setSelectedCells ($ selectedCells );
@@ -68,7 +73,7 @@ private function setConditionalsFromExt(array $conditionals): void
68
73
}
69
74
}
70
75
71
- private function readConditionalsFromExt (SimpleXMLElement $ extLst, StyleReader $ styleReader ): array
76
+ private function readConditionalsFromExt (SimpleXMLElement $ extLst ): array
72
77
{
73
78
$ conditionals = [];
74
79
if (!isset ($ extLst ->ext )) {
@@ -110,7 +115,7 @@ private function readConditionalsFromExt(SimpleXMLElement $extLst, StyleReader $
110
115
$ priority = (int ) $ attributes ->priority ;
111
116
112
117
$ conditional = $ this ->readConditionalRuleFromExt ($ extCfRuleXml , $ attributes );
113
- $ cfStyle = $ this ->readStyleFromExt ($ extCfRuleXml, $ styleReader );
118
+ $ cfStyle = $ this ->readStyleFromExt ($ extCfRuleXml );
114
119
$ conditional ->setStyle ($ cfStyle );
115
120
$ conditionals [$ sqref ][$ priority ] = $ conditional ;
116
121
}
@@ -146,17 +151,17 @@ private function readConditionalRuleFromExt(SimpleXMLElement $cfRuleXml, SimpleX
146
151
return $ conditional ;
147
152
}
148
153
149
- private function readStyleFromExt (SimpleXMLElement $ extCfRuleXml, StyleReader $ styleReader ): Style
154
+ private function readStyleFromExt (SimpleXMLElement $ extCfRuleXml ): Style
150
155
{
151
156
$ cfStyle = new Style (false , true );
152
157
if ($ extCfRuleXml ->dxf ) {
153
158
$ styleXML = $ extCfRuleXml ->dxf ->children ();
154
159
155
160
if ($ styleXML ->borders ) {
156
- $ styleReader ->readBorderStyle ($ cfStyle ->getBorders (), $ styleXML ->borders );
161
+ $ this -> styleReader ->readBorderStyle ($ cfStyle ->getBorders (), $ styleXML ->borders );
157
162
}
158
163
if ($ styleXML ->fill ) {
159
- $ styleReader ->readFillStyle ($ cfStyle ->getFill (), $ styleXML ->fill );
164
+ $ this -> styleReader ->readFillStyle ($ cfStyle ->getFill (), $ styleXML ->fill );
160
165
}
161
166
}
162
167
@@ -198,6 +203,7 @@ private function readStyleRules(array $cfRules, SimpleXMLElement $extLst): array
198
203
$ conditionalFormattingRuleExtensions = ConditionalFormattingRuleExtension::parseExtLstXml ($ extLst );
199
204
$ conditionalStyles = [];
200
205
206
+ /** @var SimpleXMLElement $cfRule */
201
207
foreach ($ cfRules as $ cfRule ) {
202
208
$ objConditional = new Conditional ();
203
209
$ objConditional ->setConditionType ((string ) $ cfRule ['type ' ]);
@@ -231,7 +237,11 @@ private function readStyleRules(array $cfRules, SimpleXMLElement $extLst): array
231
237
232
238
if (isset ($ cfRule ->dataBar )) {
233
239
$ objConditional ->setDataBar (
234
- $ this ->readDataBarOfConditionalRule ($ cfRule , $ conditionalFormattingRuleExtensions ) // @phpstan-ignore-line
240
+ $ this ->readDataBarOfConditionalRule ($ cfRule , $ conditionalFormattingRuleExtensions )
241
+ );
242
+ } elseif (isset ($ cfRule ->colorScale )) {
243
+ $ objConditional ->setColorScale (
244
+ $ this ->readColorScale ($ cfRule )
235
245
);
236
246
} elseif (isset ($ cfRule ['dxfId ' ])) {
237
247
$ objConditional ->setStyle (clone $ this ->dxfs [(int ) ($ cfRule ['dxfId ' ])]);
@@ -270,14 +280,48 @@ private function readDataBarOfConditionalRule($cfRule, array $conditionalFormatt
270
280
271
281
//color
272
282
if (isset ($ cfRule ->dataBar ->color )) {
273
- $ dataBar ->setColor (( string ) $ cfRule ->dataBar ->color [ ' rgb ' ] );
283
+ $ dataBar ->setColor ($ this -> styleReader -> readColor ( $ cfRule ->dataBar ->color ) );
274
284
}
275
285
//extLst
276
286
$ this ->readDataBarExtLstOfConditionalRule ($ dataBar , $ cfRule , $ conditionalFormattingRuleExtensions );
277
287
278
288
return $ dataBar ;
279
289
}
280
290
291
+ private function readColorScale (simpleXMLElement |stdClass $ cfRule ): ConditionalColorScale
292
+ {
293
+ $ colorScale = new ConditionalColorScale ();
294
+ $ types = [];
295
+ foreach ($ cfRule ->colorScale ->cfvo as $ cfvoXml ) {
296
+ $ attr = $ cfvoXml ->attributes () ?? [];
297
+ $ type = (string ) ($ attr ['type ' ] ?? '' );
298
+ $ types [] = $ type ;
299
+ $ val = $ attr ['val ' ] ?? null ;
300
+ if ($ type === 'min ' ) {
301
+ $ colorScale ->setMinimumConditionalFormatValueObject (new ConditionalFormatValueObject ($ type , $ val ));
302
+ } elseif ($ type === 'percentile ' ) {
303
+ $ colorScale ->setMidpointConditionalFormatValueObject (new ConditionalFormatValueObject ($ type , $ val ));
304
+ } elseif ($ type === 'max ' ) {
305
+ $ colorScale ->setMaximumConditionalFormatValueObject (new ConditionalFormatValueObject ($ type , $ val ));
306
+ }
307
+ }
308
+ $ idx = 0 ;
309
+ foreach ($ cfRule ->colorScale ->color as $ color ) {
310
+ $ type = $ types [$ idx ];
311
+ $ rgb = $ this ->styleReader ->readColor ($ color );
312
+ if ($ type === 'min ' ) {
313
+ $ colorScale ->setMinimumColor (new Color ($ rgb ));
314
+ } elseif ($ type === 'percentile ' ) {
315
+ $ colorScale ->setMidpointColor (new Color ($ rgb ));
316
+ } elseif ($ type === 'max ' ) {
317
+ $ colorScale ->setMaximumColor (new Color ($ rgb ));
318
+ }
319
+ ++$ idx ;
320
+ }
321
+
322
+ return $ colorScale ;
323
+ }
324
+
281
325
/**
282
326
* @param SimpleXMLElement|stdClass $cfRule
283
327
*/
0 commit comments