diff --git a/src/PhpSpreadsheet/Reader/Xlsx/Styles.php b/src/PhpSpreadsheet/Reader/Xlsx/Styles.php index c98780b0..40106258 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx/Styles.php +++ b/src/PhpSpreadsheet/Reader/Xlsx/Styles.php @@ -8,6 +8,7 @@ use PhpOffice\PhpSpreadsheet\Style\Borders; use PhpOffice\PhpSpreadsheet\Style\Color; use PhpOffice\PhpSpreadsheet\Style\Fill; use PhpOffice\PhpSpreadsheet\Style\Font; +use PhpOffice\PhpSpreadsheet\Style\NumberFormat; use PhpOffice\PhpSpreadsheet\Style\Protection; use PhpOffice\PhpSpreadsheet\Style\Style; @@ -71,6 +72,17 @@ class Styles extends BaseParserClass } } + private static function readNumberFormat(NumberFormat $numfmtStyle, \SimpleXMLElement $numfmtStyleXml) + { + if ($numfmtStyleXml->count() === 0) { + return; + } + $numfmt = $numfmtStyleXml->attributes(); + if ($numfmt->count() > 0 && isset($numfmt['formatCode'])) { + $numfmtStyle->setFormatCode((string) $numfmt['formatCode']); + } + } + private static function readFillStyle(Fill $fillStyle, \SimpleXMLElement $fillStyleXml) { if ($fillStyleXml->gradientFill) { @@ -149,7 +161,11 @@ class Styles extends BaseParserClass private function readStyle(Style $docStyle, $style) { - $docStyle->getNumberFormat()->setFormatCode($style->numFmt); + if ($style->numFmt instanceof \SimpleXMLElement) { + self::readNumberFormat($docStyle->getNumberFormat(), $style->numFmt); + } else { + $docStyle->getNumberFormat()->setFormatCode($style->numFmt); + } if (isset($style->font)) { self::readFontStyle($docStyle->getFont(), $style->font); diff --git a/src/PhpSpreadsheet/Style/NumberFormat.php b/src/PhpSpreadsheet/Style/NumberFormat.php index 7efa759e..5ea30b88 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat.php +++ b/src/PhpSpreadsheet/Style/NumberFormat.php @@ -367,7 +367,7 @@ class NumberFormat extends Supervisor self::fillBuiltInFormatCodes(); // Lookup format code - if (isset(self::$flippedBuiltInFormats[$formatCode])) { + if (array_key_exists($formatCode, self::$flippedBuiltInFormats)) { return self::$flippedBuiltInFormats[$formatCode]; } diff --git a/tests/PhpSpreadsheetTests/Reader/CondNumFmtTest.php b/tests/PhpSpreadsheetTests/Reader/CondNumFmtTest.php new file mode 100644 index 00000000..ca69d23b --- /dev/null +++ b/tests/PhpSpreadsheetTests/Reader/CondNumFmtTest.php @@ -0,0 +1,39 @@ +load($filename); + + $worksheet = $spreadsheet->getActiveSheet(); + // NumberFormat explicitly set in following conditional style + $conditionalStyle = $worksheet->getConditionalStyles('A1:A3'); + self::assertNotEmpty($conditionalStyle); + $conditionalRule = $conditionalStyle[0]; + $conditions = $conditionalRule->getConditions(); + self::assertNotEmpty($conditions); + self::assertEquals(Conditional::CONDITION_EXPRESSION, $conditionalRule->getConditionType()); + self::assertEquals('MONTH(A1)=10', $conditions[0]); + $numfmt = $conditionalRule->getStyle()->getNumberFormat()->getFormatCode(); + self::assertEquals('yyyy/mm/dd', $numfmt); + // NumberFormat not set in following conditional style + $conditionalStyle = $worksheet->getConditionalStyles('B1'); + self::assertNotEmpty($conditionalStyle); + $conditionalRule = $conditionalStyle[0]; + $conditions = $conditionalRule->getConditions(); + self::assertNotEmpty($conditions); + self::assertEquals(Conditional::CONDITION_EXPRESSION, $conditionalRule->getConditionType()); + self::assertEquals('AND(B1>=2000,B1<3000)', $conditions[0]); + $numfmt = $conditionalRule->getStyle()->getNumberFormat()->getFormatCode(); + self::assertEquals('', $numfmt); + } +} diff --git a/tests/data/Reader/XLSX/condfmtnum.xlsx b/tests/data/Reader/XLSX/condfmtnum.xlsx new file mode 100644 index 00000000..9254f87f Binary files /dev/null and b/tests/data/Reader/XLSX/condfmtnum.xlsx differ