Skip to content

Commit

Permalink
General Style Specified in Uppercase in Input Xlsx
Browse files Browse the repository at this point in the history
Fix PHPOffice#2450. Treat input style GENERAL as if it were expected upper/lowercase.
  • Loading branch information
oleibman committed Dec 17, 2021
1 parent aa91abc commit 7b2e10e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/PhpSpreadsheet/Reader/Xlsx/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private function readNumberFormat(NumberFormat $numfmtStyle, SimpleXMLElement $n
}
$numfmt = Xlsx::getAttributes($numfmtStyleXml);
if ($numfmt->count() > 0 && isset($numfmt['formatCode'])) {
$numfmtStyle->setFormatCode((string) $numfmt['formatCode']);
$numfmtStyle->setFormatCode(self::formatGeneral((string) $numfmt['formatCode']));
}
}

Expand Down Expand Up @@ -183,6 +183,15 @@ public function readAlignmentStyle(Alignment $alignment, SimpleXMLElement $align
);
}

private function formatGeneral(string $formatString): string
{
if ($formatString === 'GENERAL') {
$formatString = NumberFormat::FORMAT_GENERAL;
}

return $formatString;
}

/**
* Read style.
*
Expand All @@ -193,7 +202,7 @@ public function readStyle(Style $docStyle, $style): void
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)) {
Expand Down
34 changes: 34 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/Issue2450Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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();
$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 added tests/data/Reader/XLSX/issue.2450.xlsx
Binary file not shown.

0 comments on commit 7b2e10e

Please sign in to comment.