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.
This commit is contained in:
oleibman 2021-08-08 08:24:03 -07:00 committed by GitHub
parent de230fa899
commit d7ac7021c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 1 deletions

View File

@ -2125,6 +2125,10 @@ class Xls extends BaseReader
}
$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 @@ class Xls extends BaseReader
$numberFormat = ['formatCode' => $code];
} else {
// we set the general format code
$numberFormat = ['formatCode' => 'General'];
$numberFormat = ['formatCode' => NumberFormat::FORMAT_GENERAL];
}
$objStyle->getNumberFormat()->setFormatCode($numberFormat['formatCode']);

View File

@ -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 not shown.