diff --git a/src/PhpSpreadsheet/Reader/Xlsx/ConditionalStyles.php b/src/PhpSpreadsheet/Reader/Xlsx/ConditionalStyles.php index 1c624e1a3c..5c1a9186c4 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx/ConditionalStyles.php +++ b/src/PhpSpreadsheet/Reader/Xlsx/ConditionalStyles.php @@ -97,37 +97,7 @@ private function readStyleRules($cfRules, $extLst) } if (isset($cfRule->dataBar)) { - $dataBar = new ConditionalDataBar(); - //dataBar attribute - if (isset($cfRule->dataBar['showValue'])) { - $dataBar->setShowValue((int) $cfRule->dataBar['showValue']); - } - - //dataBar children - //conditionalFormatValueObjects - $cfvoXml = $cfRule->dataBar->cfvo; - foreach ((count($cfvoXml) > 1 ? $cfvoXml : [$cfvoXml]) as $cfvo) { - $dataBar->addConditionalFormatValueObject((string) $cfvo['type'], (string) $cfvo['val']); - } - - //color - if (isset($cfRule->dataBar->color)) { - $dataBar->setColor(new Color((string) $cfRule->dataBar->color['rgb'])); - } - //extLst - if (isset($cfRule->extLst)) { - $ns = $cfRule->extLst->getNamespaces(true); - foreach ((count($cfRule->extLst) > 0 ? $cfRule->extLst->ext : [$cfRule->extLst->ext]) as $ext) { - if ((string) $ext['uri'] === '{B025F937-C7B1-47D3-B67F-A62EFF666E3E}') { - $extId = (string) $ext->children($ns['x14'])->id; - if (isset($conditionalFormattingRuleExtensions[$extId])) { - $dataBar->addConditionalFormattingRuleExtList($conditionalFormattingRuleExtensions[$extId]); - } - } - } - } - - $objConditional->setDataBar($dataBar); + $objConditional->setDataBar($this->readDataBarOfConditionalRule($cfRule, $conditionalFormattingRuleExtensions)); } else { $objConditional->setStyle(clone $this->dxfs[(int) ($cfRule['dxfId'])]); } @@ -137,4 +107,37 @@ private function readStyleRules($cfRules, $extLst) return $conditionalStyles; } + + private function readDataBarOfConditionalRule($cfRule, $conditionalFormattingRuleExtensions): ConditionalDataBar + { + $dataBar = new ConditionalDataBar(); + //dataBar attribute + if (isset($cfRule->dataBar['showValue'])) { + $dataBar->setShowValue((int) $cfRule->dataBar['showValue']); + } + + //dataBar children + //conditionalFormatValueObjects + $cfvoXml = $cfRule->dataBar->cfvo; + foreach ((count($cfvoXml) > 1 ? $cfvoXml : [$cfvoXml]) as $cfvo) { + $dataBar->addConditionalFormatValueObject((string) $cfvo['type'], (string) $cfvo['val']); + } + + //color + if (isset($cfRule->dataBar->color)) { + $dataBar->setColor(new Color((string) $cfRule->dataBar->color['rgb'])); + } + //extLst + if (isset($cfRule->extLst)) { + $ns = $cfRule->extLst->getNamespaces(true); + foreach ((count($cfRule->extLst) > 0 ? $cfRule->extLst->ext : [$cfRule->extLst->ext]) as $ext) { + $extId = (string) $ext->children($ns['x14'])->id; + if (isset($conditionalFormattingRuleExtensions[$extId]) && (string) $ext['uri'] === '{B025F937-C7B1-47D3-B67F-A62EFF666E3E}') { + $dataBar->addConditionalFormattingRuleExtList($conditionalFormattingRuleExtensions[$extId]); + } + } + } + + return $dataBar; + } } diff --git a/src/PhpSpreadsheet/Style/ConditionalFormatting/ConditionalFormattingRuleExtension.php b/src/PhpSpreadsheet/Style/ConditionalFormatting/ConditionalFormattingRuleExtension.php index 3a25bfab3f..ce50a120f7 100644 --- a/src/PhpSpreadsheet/Style/ConditionalFormatting/ConditionalFormattingRuleExtension.php +++ b/src/PhpSpreadsheet/Style/ConditionalFormatting/ConditionalFormattingRuleExtension.php @@ -59,49 +59,8 @@ public static function parseExtLstXml($extLstXml) $extFormattingRuleObj->setDataBar($extDataBarObj); $dataBarXml = $extCfRuleXml->dataBar; - $dataBarAttribute = $dataBarXml->attributes(); - - //attributes - if ($dataBarAttribute->minLength) { - $extDataBarObj->setMinLength((int) $dataBarAttribute->minLength); - } - if ($dataBarAttribute->maxLength) { - $extDataBarObj->setMaxLength((int) $dataBarAttribute->maxLength); - } - if ($dataBarAttribute->border) { - $extDataBarObj->setBorder((int) $dataBarAttribute->border); - } - if ($dataBarAttribute->gradient) { - $extDataBarObj->setGradient((int) $dataBarAttribute->gradient); - } - if ($dataBarAttribute->direction) { - $extDataBarObj->setDirection((string) $dataBarAttribute->direction); - } - if ($dataBarAttribute->negativeBarBorderColorSameAsPositive) { - $extDataBarObj->setNegativeBarBorderColorSameAsPositive((int) $dataBarAttribute->negativeBarBorderColorSameAsPositive); - } - if ($dataBarAttribute->axisPosition) { - $extDataBarObj->setAxisPosition((string) $dataBarAttribute->axisPosition); - } - - //children - if ($dataBarXml->borderColor) { - $extDataBarObj->setBorderColor((string) $dataBarXml->borderColor->attributes()['rgb']); - } - if ($dataBarXml->negativeFillColor) { - $extDataBarObj->setNegativeFillColor((string) $dataBarXml->negativeFillColor->attributes()['rgb']); - } - if ($dataBarXml->negativeBorderColor) { - $extDataBarObj->setNegativeBorderColor((string) $dataBarXml->negativeBorderColor->attributes()['rgb']); - } - if ($dataBarXml->axisColor) { - $axisColorAttr = $dataBarXml->axisColor->attributes(); - $extDataBarObj->setAxisColor((string) $axisColorAttr['rgb'], (string) $axisColorAttr['theme'], (string) $axisColorAttr['tint']); - } - foreach ($dataBarXml->cfvo as $cfvo) { - $f = (string) $cfvo->children($ns['xm'])->f; - $extDataBarObj->addConditionalFormatValueObject((string) $cfvo->attributes()['type'], null, (empty($f) ? null : $f)); - } + self::parseExtDataBarAttributesFromXml($extDataBarObj, $dataBarXml); + self::parseExtDataBarElementChildrenFromXml($extDataBarObj, $dataBarXml, $ns); } } } @@ -109,6 +68,53 @@ public static function parseExtLstXml($extLstXml) return $conditionalFormattingRuleExtensions; } + private static function parseExtDataBarAttributesFromXml(ConditionalDataBarExtension $extDataBarObj, SimpleXMLElement $dataBarXml): void + { + $dataBarAttribute = $dataBarXml->attributes(); + if ($dataBarAttribute->minLength) { + $extDataBarObj->setMinLength((int) $dataBarAttribute->minLength); + } + if ($dataBarAttribute->maxLength) { + $extDataBarObj->setMaxLength((int) $dataBarAttribute->maxLength); + } + if ($dataBarAttribute->border) { + $extDataBarObj->setBorder((int) $dataBarAttribute->border); + } + if ($dataBarAttribute->gradient) { + $extDataBarObj->setGradient((int) $dataBarAttribute->gradient); + } + if ($dataBarAttribute->direction) { + $extDataBarObj->setDirection((string) $dataBarAttribute->direction); + } + if ($dataBarAttribute->negativeBarBorderColorSameAsPositive) { + $extDataBarObj->setNegativeBarBorderColorSameAsPositive((int) $dataBarAttribute->negativeBarBorderColorSameAsPositive); + } + if ($dataBarAttribute->axisPosition) { + $extDataBarObj->setAxisPosition((string) $dataBarAttribute->axisPosition); + } + } + + private static function parseExtDataBarElementChildrenFromXml(ConditionalDataBarExtension $extDataBarObj, SimpleXMLElement $dataBarXml, $ns): void + { + if ($dataBarXml->borderColor) { + $extDataBarObj->setBorderColor((string) $dataBarXml->borderColor->attributes()['rgb']); + } + if ($dataBarXml->negativeFillColor) { + $extDataBarObj->setNegativeFillColor((string) $dataBarXml->negativeFillColor->attributes()['rgb']); + } + if ($dataBarXml->negativeBorderColor) { + $extDataBarObj->setNegativeBorderColor((string) $dataBarXml->negativeBorderColor->attributes()['rgb']); + } + if ($dataBarXml->axisColor) { + $axisColorAttr = $dataBarXml->axisColor->attributes(); + $extDataBarObj->setAxisColor((string) $axisColorAttr['rgb'], (string) $axisColorAttr['theme'], (string) $axisColorAttr['tint']); + } + foreach ($dataBarXml->cfvo as $cfvo) { + $f = (string) $cfvo->children($ns['xm'])->f; + $extDataBarObj->addConditionalFormatValueObject((string) $cfvo->attributes()['type'], null, (empty($f) ? null : $f)); + } + } + /** * @return mixed */ diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php index 8bf4b4ee9b..29d61a0e76 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php @@ -543,6 +543,39 @@ private static function writeExtConditionalFormattingElements(XMLWriter $objWrit $objWriter->endElement(); //end conditionalFormatting } + private function writeDataBarElements(XMLWriter $objWriter, $dataBar): void + { + if ($dataBar) { + $objWriter->startElement('dataBar'); + self::writeAttributeIf($objWriter, null !== $dataBar->getShowValue(), 'showValue', (string) $dataBar->getShowValue()); + foreach ($dataBar->getConditionalFormatValueObjects() as $cfvo) { + $objWriter->startElement('cfvo'); + self::writeAttributeIf($objWriter, $cfvo->getType(), 'type', (string) $cfvo->getType()); + self::writeAttributeIf($objWriter, $cfvo->getValue(), 'val', (string) $cfvo->getValue()); + $objWriter->endElement(); + } + if ($dataBar->getColor()) { + $objWriter->startElement('color'); + $objWriter->writeAttribute('rgb', $dataBar->getColor()->getARGB()); + $objWriter->endElement(); + } + $objWriter->endElement(); // end dataBar + + if (count($dataBar->getConditionalFormattingRuleExtList()) > 0) { + $objWriter->startElement('extLst'); + foreach ($dataBar->getConditionalFormattingRuleExtList() as $extension) { + $objWriter->startElement('ext'); + $objWriter->writeAttribute('uri', '{B025F937-C7B1-47D3-B67F-A62EFF666E3E}'); + $objWriter->startElementNs('x14', 'id', null); + $objWriter->text($extension->getId()); + $objWriter->endElement(); + $objWriter->endElement(); + } + $objWriter->endElement(); //end extLst + } + } + } + /** * Write ConditionalFormatting. * @@ -594,36 +627,7 @@ private function writeConditionalFormatting(XMLWriter $objWriter, Phpspreadsheet } // - $dataBar = $conditional->getDataBar(); - if ($dataBar) { - $objWriter->startElement('dataBar'); - self::writeAttributeIf($objWriter, null !== $dataBar->getShowValue(), 'showValue', (string) $dataBar->getShowValue()); - foreach ($dataBar->getConditionalFormatValueObjects() as $cfvo) { - $objWriter->startElement('cfvo'); - self::writeAttributeIf($objWriter, $cfvo->getType(), 'type', (string) $cfvo->getType()); - self::writeAttributeIf($objWriter, $cfvo->getValue(), 'val', (string) $cfvo->getValue()); - $objWriter->endElement(); - } - if ($dataBar->getColor()) { - $objWriter->startElement('color'); - $objWriter->writeAttribute('rgb', $dataBar->getColor()->getARGB()); - $objWriter->endElement(); - } - $objWriter->endElement(); // end dataBar - - if (count($dataBar->getConditionalFormattingRuleExtList()) > 0) { - $objWriter->startElement('extLst'); - foreach ($dataBar->getConditionalFormattingRuleExtList() as $extension) { - $objWriter->startElement('ext'); - $objWriter->writeAttribute('uri', '{B025F937-C7B1-47D3-B67F-A62EFF666E3E}'); - $objWriter->startElementNs('x14', 'id', null); - $objWriter->text($extension->getId()); - $objWriter->endElement(); - $objWriter->endElement(); - } - $objWriter->endElement(); //end extLst - } - } + self::writeDataBarElements($objWriter, $conditional->getDataBar()); $objWriter->endElement(); //end cfRule