Skip to content

Commit

Permalink
Correct Xlsx Parsing of quotePrefix="0" (#3438)
Browse files Browse the repository at this point in the history
* Correct Xlsx Parsing of quotePrefix="0"

Fix #3435. Mis-parsed attribute is not normally generated by Excel or PhpSpreadsheet, but some 3rd-party software (correctly) generates it.

* Update Issue3435Test.php
  • Loading branch information
oleibman authored Mar 7, 2023
1 parent cba1f07 commit 73f880f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
$numFmt = NumberFormat::builtInFormatCode((int) $xf['numFmtId']);
}
}
$quotePrefix = (bool) ($xf['quotePrefix'] ?? false);
$quotePrefix = (bool) (string) ($xf['quotePrefix'] ?? '');

$style = (object) [
'numFmt' => $numFmt ?? NumberFormat::FORMAT_GENERAL,
Expand Down Expand Up @@ -653,7 +653,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
}
}

$quotePrefix = (bool) ($xf['quotePrefix'] ?? false);
$quotePrefix = (bool) (string) ($xf['quotePrefix'] ?? '');

$cellStyle = (object) [
'numFmt' => $numFmt,
Expand Down
44 changes: 44 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3435Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx;

class Issue3435Test extends \PHPUnit\Framework\TestCase
{
/**
* @var string
*/
private static $testbook = 'tests/data/Reader/XLSX/issue.3435.xlsx';

public function testPreliminaries(): void
{
$file = 'zip://';
$file .= self::$testbook;
$file .= '#xl/styles.xml';
$data = file_get_contents($file);
// confirm that file contains expected namespaced xml tag
if ($data === false) {
self::fail('Unable to read file');
} else {
$expected = <<<EOF
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" quotePrefix="1"/>
<xf numFmtId="0" fontId="1" fillId="2" borderId="1" xfId="0" quotePrefix="0" applyFont="1" applyFill="1" applyBorder="1"/>
EOF;
self::assertStringContainsString($expected, $data);
}
}

public function testQuotePrefix(): void
{
// Parsing of quotePrefix="0" was incorrect, now corrected.
$reader = new Xlsx();
$spreadsheet = $reader->load(self::$testbook);
$sheet = $spreadsheet->getActiveSheet();
self::assertTrue($sheet->getStyle('A1')->getQuotePrefix());
self::assertFalse($sheet->getStyle('A2')->getQuotePrefix());
self::assertFalse($sheet->getStyle('A3')->getQuotePrefix());
$spreadsheet->disconnectWorksheets();
}
}
Binary file added tests/data/Reader/XLSX/issue.3435.xlsx
Binary file not shown.

0 comments on commit 73f880f

Please sign in to comment.