Skip to content

Commit 7e89d33

Browse files
author
MarkBaker
committed
Resolve problems with writing the CF Header for each group of rules
1 parent 762300d commit 7e89d33

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

src/PhpSpreadsheet/Writer/Xls/Worksheet.php

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -539,25 +539,32 @@ public function close(): void
539539
$this->writeSheetProtection();
540540
$this->writeRangeProtection();
541541

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();
543551
if (!empty($arrConditionalStyles)) {
544552
$arrConditional = [];
545553

546-
$cfHeaderWritten = false;
547554
// Write ConditionalFormattingTable records
548555
foreach ($arrConditionalStyles as $cellCoordinate => $conditionalStyles) {
556+
$cfHeaderWritten = false;
549557
foreach ($conditionalStyles as $conditional) {
550558
/** @var Conditional $conditional */
551559
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
554562
) {
555563
// Write CFHEADER record (only if there are Conditional Styles that we are able to write)
556564
if ($cfHeaderWritten === false) {
557-
$this->writeCFHeader();
558-
$cfHeaderWritten = true;
565+
$cfHeaderWritten = $this->writeCFHeader($cellCoordinate, $conditionalStyles);
559566
}
560-
if (!isset($arrConditional[$conditional->getHashCode()])) {
567+
if ($cfHeaderWritten === true && !isset($arrConditional[$conditional->getHashCode()])) {
561568
// This hash code has been handled
562569
$arrConditional[$conditional->getHashCode()] = true;
563570

@@ -568,8 +575,6 @@ public function close(): void
568575
}
569576
}
570577
}
571-
572-
$this->storeEof();
573578
}
574579

575580
/**
@@ -3127,8 +3132,10 @@ private function writeCFRule(Conditional $conditional): void
31273132

31283133
/**
31293134
* Write CFHeader record.
3135+
*
3136+
* @param Conditional[] $conditionalStyles
31303137
*/
3131-
private function writeCFHeader(): void
3138+
private function writeCFHeader(string $cellCoordinate, array $conditionalStyles): bool
31323139
{
31333140
$record = 0x01B0; // Record identifier
31343141
$length = 0x0016; // Bytes to follow
@@ -3137,33 +3144,32 @@ private function writeCFHeader(): void
31373144
$numColumnMax = null;
31383145
$numRowMin = null;
31393146
$numRowMax = null;
3147+
31403148
$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];
31653163
}
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;
31663171
}
3172+
31673173
$needRedraw = 1;
31683174
$cellRange = pack('vvvv', $numRowMin - 1, $numRowMax - 1, $numColumnMin - 1, $numColumnMax - 1);
31693175

@@ -3173,6 +3179,8 @@ private function writeCFHeader(): void
31733179
$data .= pack('v', 0x0001);
31743180
$data .= $cellRange;
31753181
$this->append($header . $data);
3182+
3183+
return true;
31763184
}
31773185

31783186
private function getDataBlockProtection(Conditional $conditional): int

0 commit comments

Comments
 (0)