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:
parent
67bf45d700
commit
3a6558625d
|
|
@ -94,7 +94,7 @@ class Styles extends BaseParserClass
|
||||||
}
|
}
|
||||||
$numfmt = Xlsx::getAttributes($numfmtStyleXml);
|
$numfmt = Xlsx::getAttributes($numfmtStyleXml);
|
||||||
if ($numfmt->count() > 0 && isset($numfmt['formatCode'])) {
|
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.
|
* Read style.
|
||||||
*
|
*
|
||||||
|
|
@ -193,7 +202,7 @@ class Styles extends BaseParserClass
|
||||||
if ($style->numFmt instanceof SimpleXMLElement) {
|
if ($style->numFmt instanceof SimpleXMLElement) {
|
||||||
$this->readNumberFormat($docStyle->getNumberFormat(), $style->numFmt);
|
$this->readNumberFormat($docStyle->getNumberFormat(), $style->numFmt);
|
||||||
} else {
|
} else {
|
||||||
$docStyle->getNumberFormat()->setFormatCode($style->numFmt);
|
$docStyle->getNumberFormat()->setFormatCode(self::formatGeneral((string) $style->numFmt));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($style->font)) {
|
if (isset($style->font)) {
|
||||||
|
|
|
||||||
|
|
@ -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.
Loading…
Reference in New Issue