General Style Specified in Uppercase in Input Xlsx (#2451)

* General Style Specified in Uppercase in Input Xlsx

Fix #2450. Treat input style GENERAL as if it were expected upper/lowercase.

* Declare Method as Static

Surprised neither Phpstan nor Scrutinizer flagged this.

* Remove Duplicated Statement

Don't know why Scrutinizer didn't flag this the first time.
This commit is contained in:
oleibman 2021-12-18 09:25:08 -08:00 committed by GitHub
parent 67bf45d700
commit 3a6558625d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 2 deletions

View File

@ -94,7 +94,7 @@ class Styles extends BaseParserClass
}
$numfmt = Xlsx::getAttributes($numfmtStyleXml);
if ($numfmt->count() > 0 && isset($numfmt['formatCode'])) {
$numfmtStyle->setFormatCode((string) $numfmt['formatCode']);
$numfmtStyle->setFormatCode(self::formatGeneral((string) $numfmt['formatCode']));
}
}
@ -183,6 +183,15 @@ class Styles extends BaseParserClass
);
}
private static function formatGeneral(string $formatString): string
{
if ($formatString === 'GENERAL') {
$formatString = NumberFormat::FORMAT_GENERAL;
}
return $formatString;
}
/**
* Read style.
*
@ -193,7 +202,7 @@ class Styles extends BaseParserClass
if ($style->numFmt instanceof SimpleXMLElement) {
$this->readNumberFormat($docStyle->getNumberFormat(), $style->numFmt);
} else {
$docStyle->getNumberFormat()->setFormatCode($style->numFmt);
$docStyle->getNumberFormat()->setFormatCode(self::formatGeneral((string) $style->numFmt));
}
if (isset($style->font)) {

View File

@ -0,0 +1,33 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PHPUnit\Framework\TestCase;
class Issue2450Test extends TestCase
{
public function testIssue2450(): void
{
// Style specified as GENERAL rather than General.
$filename = 'tests/data/Reader/XLSX/issue.2450.xlsx';
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
$birthYears = [
'C2' => $sheet->getCell('C2')->getFormattedValue(),
'C3' => $sheet->getCell('C3')->getFormattedValue(),
'C4' => $sheet->getCell('C4')->getFormattedValue(),
];
self::assertSame(
[
'C2' => '1932',
'C3' => '1964',
'C4' => '1988',
],
$birthYears
);
$spreadsheet->disconnectWorksheets();
}
}

Binary file not shown.