Skip to content

Commit 3a65586

Browse files
authored
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.
1 parent 67bf45d commit 3a65586

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/PhpSpreadsheet/Reader/Xlsx/Styles.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private function readNumberFormat(NumberFormat $numfmtStyle, SimpleXMLElement $n
9494
}
9595
$numfmt = Xlsx::getAttributes($numfmtStyleXml);
9696
if ($numfmt->count() > 0 && isset($numfmt['formatCode'])) {
97-
$numfmtStyle->setFormatCode((string) $numfmt['formatCode']);
97+
$numfmtStyle->setFormatCode(self::formatGeneral((string) $numfmt['formatCode']));
9898
}
9999
}
100100

@@ -183,6 +183,15 @@ public function readAlignmentStyle(Alignment $alignment, SimpleXMLElement $align
183183
);
184184
}
185185

186+
private static function formatGeneral(string $formatString): string
187+
{
188+
if ($formatString === 'GENERAL') {
189+
$formatString = NumberFormat::FORMAT_GENERAL;
190+
}
191+
192+
return $formatString;
193+
}
194+
186195
/**
187196
* Read style.
188197
*
@@ -193,7 +202,7 @@ public function readStyle(Style $docStyle, $style): void
193202
if ($style->numFmt instanceof SimpleXMLElement) {
194203
$this->readNumberFormat($docStyle->getNumberFormat(), $style->numFmt);
195204
} else {
196-
$docStyle->getNumberFormat()->setFormatCode($style->numFmt);
205+
$docStyle->getNumberFormat()->setFormatCode(self::formatGeneral((string) $style->numFmt));
197206
}
198207

199208
if (isset($style->font)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
4+
5+
use PhpOffice\PhpSpreadsheet\IOFactory;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class Issue2450Test extends TestCase
9+
{
10+
public function testIssue2450(): void
11+
{
12+
// Style specified as GENERAL rather than General.
13+
$filename = 'tests/data/Reader/XLSX/issue.2450.xlsx';
14+
$reader = IOFactory::createReader('Xlsx');
15+
$spreadsheet = $reader->load($filename);
16+
$sheet = $spreadsheet->getActiveSheet();
17+
$birthYears = [
18+
'C2' => $sheet->getCell('C2')->getFormattedValue(),
19+
'C3' => $sheet->getCell('C3')->getFormattedValue(),
20+
'C4' => $sheet->getCell('C4')->getFormattedValue(),
21+
];
22+
self::assertSame(
23+
[
24+
'C2' => '1932',
25+
'C3' => '1964',
26+
'C4' => '1988',
27+
],
28+
$birthYears
29+
);
30+
31+
$spreadsheet->disconnectWorksheets();
32+
}
33+
}
4.58 KB
Binary file not shown.

0 commit comments

Comments
 (0)