From d7ac7021c61818b7182a7325e4a7f6e89e79c2a7 Mon Sep 17 00:00:00 2001 From: oleibman Date: Sun, 8 Aug 2021 08:24:03 -0700 Subject: [PATCH] Apache OpenOffice Creates Xls Using Wrong Case for Number Format General (#2242) See issue #2239. Problem is dealt with at the source, by making sure that Reader Xls checks for use of 'GENERAL' rather than 'General'. There doesn't seem to be a reason to test in other places, or to test for other casing variants. --- src/PhpSpreadsheet/Reader/Xls.php | 6 +++- .../Reader/Xls/NumberFormatGeneralTest.php | 34 ++++++++++++++++++ tests/data/Reader/XLS/issue2239.xls | Bin 0 -> 10752 bytes 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/PhpSpreadsheetTests/Reader/Xls/NumberFormatGeneralTest.php create mode 100644 tests/data/Reader/XLS/issue2239.xls diff --git a/src/PhpSpreadsheet/Reader/Xls.php b/src/PhpSpreadsheet/Reader/Xls.php index 42f5fceedc..3825471808 100644 --- a/src/PhpSpreadsheet/Reader/Xls.php +++ b/src/PhpSpreadsheet/Reader/Xls.php @@ -2125,6 +2125,10 @@ private function readFormat(): void } $formatString = $string['value']; + // Apache Open Office sets wrong case writing to xls - issue 2239 + if ($formatString === 'GENERAL') { + $formatString = NumberFormat::FORMAT_GENERAL; + } $this->formats[$indexCode] = $formatString; } } @@ -2174,7 +2178,7 @@ private function readXf(): void $numberFormat = ['formatCode' => $code]; } else { // we set the general format code - $numberFormat = ['formatCode' => 'General']; + $numberFormat = ['formatCode' => NumberFormat::FORMAT_GENERAL]; } $objStyle->getNumberFormat()->setFormatCode($numberFormat['formatCode']); diff --git a/tests/PhpSpreadsheetTests/Reader/Xls/NumberFormatGeneralTest.php b/tests/PhpSpreadsheetTests/Reader/Xls/NumberFormatGeneralTest.php new file mode 100644 index 0000000000..a67fbb341f --- /dev/null +++ b/tests/PhpSpreadsheetTests/Reader/Xls/NumberFormatGeneralTest.php @@ -0,0 +1,34 @@ +load($filename); + $sheet = $spreadsheet->getSheetByName('Blad1'); + if ($sheet === null) { + self::fail('Expected to find sheet Blad1'); + } else { + $array = $sheet->toArray(); + self::assertSame('€ 2.95', $array[1][3]); + self::assertSame(2.95, $sheet->getCell('D2')->getValue()); + self::assertSame(2.95, $sheet->getCell('D2')->getCalculatedValue()); + self::assertSame('€ 2.95', $sheet->getCell('D2')->getFormattedValue()); + self::assertSame(21, $array[1][4]); + self::assertSame(21, $sheet->getCell('E2')->getValue()); + self::assertSame(21, $sheet->getCell('E2')->getCalculatedValue()); + self::assertSame('21', $sheet->getCell('E2')->getFormattedValue()); + } + $spreadsheet->disconnectWorksheets(); + } +} diff --git a/tests/data/Reader/XLS/issue2239.xls b/tests/data/Reader/XLS/issue2239.xls new file mode 100644 index 0000000000000000000000000000000000000000..bb94575bbe7f12fb349d80c3bfd64977447505c5 GIT binary patch literal 10752 zcmeI2U2Ggz701u)m)G&ftW(D{O-YBOah$}7cD>#>Q7KvHqiIpvn2?f(q@o*VV>YpO zt@VbuAizzU7oZ}jNC=43Rtgdb)PSe}sYOYO5InR{RVo$qp-BY~QIT*91uB}D|NqRa zXJ=-uu2B$5$vpvk8isyzHpa><=RZM#A@UQo)=fs2@w;| zlwF&dnW>8LxDak24_tL@4Wj`gh*3ngc@ttBu^Dj@Vhds`VjE&RBHNs?!+E~Nx!*ul zUoaIO*drzURm74VQpCM1kDw(R4_)XnwT^5gXYG8^i2euZEbv_qe2}mBzSjTEaZKc~ zs65y9B8R+={mT-PA4M+WD)J`c0U77m-+eq^w07kS2ghwg`#stq}VHeA3WbGH%0 zS`)&mbOzUUIj3))*X!NYRVgdhCg^ijdR?+=|KoaHq1Sd?4f&bo`J+lwa+~}H`ZHfT z&4moGs4<8!P>`IAitjBHuK(NTSwSCnd8Ad&at7YH(e)pJ7C&+BYIO#9_)=8*T!AzwR(yt%GCigs6fxY}#5 z?Gbc2m2+3%$e#!yF(os+C#J%B@0g0|eeqO8@3%}f>AiC*I`c4wnjxpKY50|v9+j=5 zxkD)muCT{p?VzBKg412DApKSBMee}gMI(+VviEU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