@@ -539,25 +539,32 @@ public function close(): void
539
539
$ this ->writeSheetProtection ();
540
540
$ this ->writeRangeProtection ();
541
541
542
- $ arrConditionalStyles = $ phpSheet ->getConditionalStylesCollection ();
542
+ // Write Conditional Formatting Rules and Styles
543
+ $ this ->writeConditionalFormatting ();
544
+
545
+ $ this ->storeEof ();
546
+ }
547
+
548
+ private function writeConditionalFormatting (): void
549
+ {
550
+ $ arrConditionalStyles = $ this ->phpSheet ->getConditionalStylesCollection ();
543
551
if (!empty ($ arrConditionalStyles )) {
544
552
$ arrConditional = [];
545
553
546
- $ cfHeaderWritten = false ;
547
554
// Write ConditionalFormattingTable records
548
555
foreach ($ arrConditionalStyles as $ cellCoordinate => $ conditionalStyles ) {
556
+ $ cfHeaderWritten = false ;
549
557
foreach ($ conditionalStyles as $ conditional ) {
550
558
/** @var Conditional $conditional */
551
559
if (
552
- $ conditional ->getConditionType () == Conditional::CONDITION_EXPRESSION ||
553
- $ conditional ->getConditionType () == Conditional::CONDITION_CELLIS
560
+ $ conditional ->getConditionType () === Conditional::CONDITION_EXPRESSION ||
561
+ $ conditional ->getConditionType () === Conditional::CONDITION_CELLIS
554
562
) {
555
563
// Write CFHEADER record (only if there are Conditional Styles that we are able to write)
556
564
if ($ cfHeaderWritten === false ) {
557
- $ this ->writeCFHeader ();
558
- $ cfHeaderWritten = true ;
565
+ $ cfHeaderWritten = $ this ->writeCFHeader ($ cellCoordinate , $ conditionalStyles );
559
566
}
560
- if (!isset ($ arrConditional [$ conditional ->getHashCode ()])) {
567
+ if ($ cfHeaderWritten === true && !isset ($ arrConditional [$ conditional ->getHashCode ()])) {
561
568
// This hash code has been handled
562
569
$ arrConditional [$ conditional ->getHashCode ()] = true ;
563
570
@@ -568,8 +575,6 @@ public function close(): void
568
575
}
569
576
}
570
577
}
571
-
572
- $ this ->storeEof ();
573
578
}
574
579
575
580
/**
@@ -3127,8 +3132,10 @@ private function writeCFRule(Conditional $conditional): void
3127
3132
3128
3133
/**
3129
3134
* Write CFHeader record.
3135
+ *
3136
+ * @param Conditional[] $conditionalStyles
3130
3137
*/
3131
- private function writeCFHeader (): void
3138
+ private function writeCFHeader (string $ cellCoordinate , array $ conditionalStyles ): bool
3132
3139
{
3133
3140
$ record = 0x01B0 ; // Record identifier
3134
3141
$ length = 0x0016 ; // Bytes to follow
@@ -3137,33 +3144,32 @@ private function writeCFHeader(): void
3137
3144
$ numColumnMax = null ;
3138
3145
$ numRowMin = null ;
3139
3146
$ numRowMax = null ;
3147
+
3140
3148
$ arrConditional = [];
3141
- foreach ($ this ->phpSheet ->getConditionalStylesCollection () as $ cellCoordinate => $ conditionalStyles ) {
3142
- foreach ($ conditionalStyles as $ conditional ) {
3143
- if (
3144
- $ conditional ->getConditionType () == Conditional::CONDITION_EXPRESSION ||
3145
- $ conditional ->getConditionType () == Conditional::CONDITION_CELLIS
3146
- ) {
3147
- if (!in_array ($ conditional ->getHashCode (), $ arrConditional )) {
3148
- $ arrConditional [] = $ conditional ->getHashCode ();
3149
- }
3150
- // Cells
3151
- $ rangeCoordinates = Coordinate::rangeBoundaries ($ cellCoordinate );
3152
- if ($ numColumnMin === null || ($ numColumnMin > $ rangeCoordinates [0 ][0 ])) {
3153
- $ numColumnMin = $ rangeCoordinates [0 ][0 ];
3154
- }
3155
- if ($ numColumnMax === null || ($ numColumnMax < $ rangeCoordinates [1 ][0 ])) {
3156
- $ numColumnMax = $ rangeCoordinates [1 ][0 ];
3157
- }
3158
- if ($ numRowMin === null || ($ numRowMin > $ rangeCoordinates [0 ][1 ])) {
3159
- $ numRowMin = (int ) $ rangeCoordinates [0 ][1 ];
3160
- }
3161
- if ($ numRowMax === null || ($ numRowMax < $ rangeCoordinates [1 ][1 ])) {
3162
- $ numRowMax = (int ) $ rangeCoordinates [1 ][1 ];
3163
- }
3164
- }
3149
+ foreach ($ conditionalStyles as $ conditional ) {
3150
+ if (!in_array ($ conditional ->getHashCode (), $ arrConditional )) {
3151
+ $ arrConditional [] = $ conditional ->getHashCode ();
3152
+ }
3153
+ // Cells
3154
+ $ rangeCoordinates = Coordinate::rangeBoundaries ($ cellCoordinate );
3155
+ if ($ numColumnMin === null || ($ numColumnMin > $ rangeCoordinates [0 ][0 ])) {
3156
+ $ numColumnMin = $ rangeCoordinates [0 ][0 ];
3157
+ }
3158
+ if ($ numColumnMax === null || ($ numColumnMax < $ rangeCoordinates [1 ][0 ])) {
3159
+ $ numColumnMax = $ rangeCoordinates [1 ][0 ];
3160
+ }
3161
+ if ($ numRowMin === null || ($ numRowMin > $ rangeCoordinates [0 ][1 ])) {
3162
+ $ numRowMin = (int ) $ rangeCoordinates [0 ][1 ];
3165
3163
}
3164
+ if ($ numRowMax === null || ($ numRowMax < $ rangeCoordinates [1 ][1 ])) {
3165
+ $ numRowMax = (int ) $ rangeCoordinates [1 ][1 ];
3166
+ }
3167
+ }
3168
+
3169
+ if (count ($ arrConditional ) === 0 ) {
3170
+ return false ;
3166
3171
}
3172
+
3167
3173
$ needRedraw = 1 ;
3168
3174
$ cellRange = pack ('vvvv ' , $ numRowMin - 1 , $ numRowMax - 1 , $ numColumnMin - 1 , $ numColumnMax - 1 );
3169
3175
@@ -3173,6 +3179,8 @@ private function writeCFHeader(): void
3173
3179
$ data .= pack ('v ' , 0x0001 );
3174
3180
$ data .= $ cellRange ;
3175
3181
$ this ->append ($ header . $ data );
3182
+
3183
+ return true ;
3176
3184
}
3177
3185
3178
3186
private function getDataBlockProtection (Conditional $ conditional ): int
0 commit comments