Skip to content

Commit

Permalink
Apache OpenOffice Creates Xls Using Wrong Case for Number Format Gene…
Browse files Browse the repository at this point in the history
…ral (#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.
  • Loading branch information
oleibman authored Aug 8, 2021
1 parent de230fa commit d7ac702
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/PhpSpreadsheet/Reader/Xls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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']);

Expand Down
34 changes: 34 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xls/NumberFormatGeneralTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls;

use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;

class NumberFormatGeneralTest extends AbstractFunctional
{
public function testGeneral(): void
{
$filename = 'tests/data/Reader/XLS/issue2239.xls';
$contents = file_get_contents($filename) ?: '';
self::assertStringContainsString('GENERAL', $contents);

$reader = new Xls();
$spreadsheet = $reader->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();
}
}
Binary file added tests/data/Reader/XLS/issue2239.xls
Binary file not shown.

0 comments on commit d7ac702

Please sign in to comment.