2
2
3
3
namespace PhpOffice \PhpSpreadsheet \Reader \Xlsx ;
4
4
5
+ use PhpOffice \PhpSpreadsheet \Style \Color ;
5
6
use PhpOffice \PhpSpreadsheet \Style \Conditional ;
7
+ use PhpOffice \PhpSpreadsheet \Style \ConditionalFormatting \ConditionalDataBar ;
8
+ use PhpOffice \PhpSpreadsheet \Style \ConditionalFormatting \ConditionalFormattingRuleExtension ;
6
9
use PhpOffice \PhpSpreadsheet \Worksheet \Worksheet ;
7
10
use SimpleXMLElement ;
8
11
@@ -25,7 +28,8 @@ public function load(): void
25
28
{
26
29
$ this ->setConditionalStyles (
27
30
$ this ->worksheet ,
28
- $ this ->readConditionalStyles ($ this ->worksheetXml )
31
+ $ this ->readConditionalStyles ($ this ->worksheetXml ),
32
+ $ this ->worksheetXml ->extLst
29
33
);
30
34
}
31
35
@@ -36,26 +40,28 @@ private function readConditionalStyles($xmlSheet)
36
40
foreach ($ conditional ->cfRule as $ cfRule ) {
37
41
if (
38
42
((string ) $ cfRule ['type ' ] == Conditional::CONDITION_NONE
39
- || (string ) $ cfRule ['type ' ] == Conditional::CONDITION_CELLIS
40
- || (string ) $ cfRule ['type ' ] == Conditional::CONDITION_CONTAINSTEXT
41
- || (string ) $ cfRule ['type ' ] == Conditional::CONDITION_CONTAINSBLANKS
42
- || (string ) $ cfRule ['type ' ] == Conditional::CONDITION_NOTCONTAINSBLANKS
43
- || (string ) $ cfRule ['type ' ] == Conditional::CONDITION_EXPRESSION )
43
+ || (string ) $ cfRule ['type ' ] == Conditional::CONDITION_CELLIS
44
+ || (string ) $ cfRule ['type ' ] == Conditional::CONDITION_CONTAINSTEXT
45
+ || (string ) $ cfRule ['type ' ] == Conditional::CONDITION_CONTAINSBLANKS
46
+ || (string ) $ cfRule ['type ' ] == Conditional::CONDITION_NOTCONTAINSBLANKS
47
+ || (string ) $ cfRule ['type ' ] == Conditional::CONDITION_EXPRESSION )
44
48
&& isset ($ this ->dxfs [(int ) ($ cfRule ['dxfId ' ])])
45
49
) {
46
50
$ conditionals [(string ) $ conditional ['sqref ' ]][(int ) ($ cfRule ['priority ' ])] = $ cfRule ;
51
+ } elseif ((string ) $ cfRule ['type ' ] == Conditional::CONDITION_DATABAR ) {
52
+ $ conditionals [(string ) $ conditional ['sqref ' ]][(int ) ($ cfRule ['priority ' ])] = $ cfRule ;
47
53
}
48
54
}
49
55
}
50
56
51
57
return $ conditionals ;
52
58
}
53
59
54
- private function setConditionalStyles (Worksheet $ worksheet , array $ conditionals ): void
60
+ private function setConditionalStyles (Worksheet $ worksheet , array $ conditionals, $ xmlExtLst ): void
55
61
{
56
62
foreach ($ conditionals as $ ref => $ cfRules ) {
57
63
ksort ($ cfRules );
58
- $ conditionalStyles = $ this ->readStyleRules ($ cfRules );
64
+ $ conditionalStyles = $ this ->readStyleRules ($ cfRules, $ xmlExtLst );
59
65
60
66
// Extract all cell references in $ref
61
67
$ cellBlocks = explode (' ' , str_replace ('$ ' , '' , strtoupper ($ ref )));
@@ -65,8 +71,9 @@ private function setConditionalStyles(Worksheet $worksheet, array $conditionals)
65
71
}
66
72
}
67
73
68
- private function readStyleRules ($ cfRules )
74
+ private function readStyleRules ($ cfRules, $ extLst )
69
75
{
76
+ $ conditionalFormattingRuleExtensions = ConditionalFormattingRuleExtension::parseExtLstXml ($ extLst );
70
77
$ conditionalStyles = [];
71
78
foreach ($ cfRules as $ cfRule ) {
72
79
$ objConditional = new Conditional ();
@@ -88,10 +95,49 @@ private function readStyleRules($cfRules)
88
95
} else {
89
96
$ objConditional ->addCondition ((string ) $ cfRule ->formula );
90
97
}
91
- $ objConditional ->setStyle (clone $ this ->dxfs [(int ) ($ cfRule ['dxfId ' ])]);
98
+
99
+ if (isset ($ cfRule ->dataBar )) {
100
+ $ objConditional ->setDataBar ($ this ->readDataBarOfConditionalRule ($ cfRule , $ conditionalFormattingRuleExtensions ));
101
+ } else {
102
+ $ objConditional ->setStyle (clone $ this ->dxfs [(int ) ($ cfRule ['dxfId ' ])]);
103
+ }
104
+
92
105
$ conditionalStyles [] = $ objConditional ;
93
106
}
94
107
95
108
return $ conditionalStyles ;
96
109
}
110
+
111
+ private function readDataBarOfConditionalRule ($ cfRule , $ conditionalFormattingRuleExtensions ): ConditionalDataBar
112
+ {
113
+ $ dataBar = new ConditionalDataBar ();
114
+ //dataBar attribute
115
+ if (isset ($ cfRule ->dataBar ['showValue ' ])) {
116
+ $ dataBar ->setShowValue ((int ) $ cfRule ->dataBar ['showValue ' ]);
117
+ }
118
+
119
+ //dataBar children
120
+ //conditionalFormatValueObjects
121
+ $ cfvoXml = $ cfRule ->dataBar ->cfvo ;
122
+ foreach ((count ($ cfvoXml ) > 1 ? $ cfvoXml : [$ cfvoXml ]) as $ cfvo ) {
123
+ $ dataBar ->addConditionalFormatValueObject ((string ) $ cfvo ['type ' ], (string ) $ cfvo ['val ' ]);
124
+ }
125
+
126
+ //color
127
+ if (isset ($ cfRule ->dataBar ->color )) {
128
+ $ dataBar ->setColor (new Color ((string ) $ cfRule ->dataBar ->color ['rgb ' ]));
129
+ }
130
+ //extLst
131
+ if (isset ($ cfRule ->extLst )) {
132
+ $ ns = $ cfRule ->extLst ->getNamespaces (true );
133
+ foreach ((count ($ cfRule ->extLst ) > 0 ? $ cfRule ->extLst ->ext : [$ cfRule ->extLst ->ext ]) as $ ext ) {
134
+ $ extId = (string ) $ ext ->children ($ ns ['x14 ' ])->id ;
135
+ if (isset ($ conditionalFormattingRuleExtensions [$ extId ]) && (string ) $ ext ['uri ' ] === '{B025F937-C7B1-47D3-B67F-A62EFF666E3E} ' ) {
136
+ $ dataBar ->addConditionalFormattingRuleExtList ($ conditionalFormattingRuleExtensions [$ extId ]);
137
+ }
138
+ }
139
+ }
140
+
141
+ return $ dataBar ;
142
+ }
97
143
}
0 commit comments