Skip to content

Commit 2b7e6f5

Browse files
committed
Ignore ignoredErrors when Not Applicable
Fix #4375. Do not set ignoredErrors when using readDataOnly, not when the cell to which it applies doesn't exist.
1 parent cd7386f commit 2b7e6f5

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

src/PhpSpreadsheet/Collection/Cells.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public function has(string $cellCoordinate): bool
8181
return ($cellCoordinate === $this->currentCoordinate) || isset($this->index[$cellCoordinate]);
8282
}
8383

84+
public function has2(string $cellCoordinate): bool
85+
{
86+
return isset($this->index[$cellCoordinate]);
87+
}
88+
8489
/**
8590
* Add or update a cell in the collection.
8691
*

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,9 +980,8 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
980980
}
981981
}
982982
$docSheet->setSelectedCells($holdSelectedCells);
983-
if ($xmlSheetNS && $xmlSheetNS->ignoredErrors) {
984-
foreach ($xmlSheetNS->ignoredErrors->ignoredError as $ignoredErrorx) {
985-
$ignoredError = self::testSimpleXml($ignoredErrorx);
983+
if (!$this->readDataOnly && $xmlSheetNS && $xmlSheetNS->ignoredErrors) {
984+
foreach ($xmlSheetNS->ignoredErrors->ignoredError as $ignoredError) {
986985
$this->processIgnoredErrors($ignoredError, $docSheet);
987986
}
988987
}
@@ -2375,6 +2374,7 @@ private static function extractPalette(?SimpleXMLElement $sxml): array
23752374

23762375
private function processIgnoredErrors(SimpleXMLElement $xml, Worksheet $sheet): void
23772376
{
2377+
$cellCollection = $sheet->getCellCollection();
23782378
$attributes = self::getAttributes($xml);
23792379
$sqref = (string) ($attributes['sqref'] ?? '');
23802380
$numberStoredAsText = (string) ($attributes['numberStoredAsText'] ?? '');
@@ -2399,6 +2399,9 @@ private function processIgnoredErrors(SimpleXMLElement $xml, Worksheet $sheet):
23992399
++$lastCol;
24002400
for ($row = $firstRow; $row <= $lastRow; ++$row) {
24012401
for ($col = $firstCol; $col !== $lastCol; ++$col) {
2402+
if (!$cellCollection->has2("$col$row")) {
2403+
continue;
2404+
}
24022405
if ($numberStoredAsText === '1') {
24032406
$sheet->getCell("$col$row")->getIgnoredErrors()->setNumberStoredAsText(true);
24042407
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
6+
7+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class Issue4375Test extends TestCase
11+
{
12+
private static string $file = 'tests/data/Reader/XLSX/issue.4375.small.xlsx';
13+
14+
public function testPreliminaries(): void
15+
{
16+
$file = 'zip://';
17+
$file .= self::$file;
18+
$file .= '#xl/worksheets/sheet1.xml';
19+
$data = file_get_contents($file) ?: '';
20+
$expected = '<ignoredErrors><ignoredError sqref="A2:B5 B1:F1" numberStoredAsText="1"/></ignoredErrors>';
21+
self::assertStringContainsString($expected, $data, 'neither fgColor nor bgColor');
22+
}
23+
24+
public function testDataOnly(): void
25+
{
26+
$file = self::$file;
27+
$reader = new XlsxReader();
28+
$reader->setReadDataOnly(true);
29+
$spreadsheet = $reader->load($file);
30+
$sheet = $spreadsheet->getActiveSheet();
31+
self::assertSame('0', $sheet->getCell('A2')->getValue());
32+
self::assertFalse(
33+
$sheet->getCell('A2')
34+
->getIgnoredErrors()
35+
->getNumberStoredAsText()
36+
);
37+
self::assertFalse($sheet->cellExists('A3'));
38+
$spreadsheet->disconnectWorksheets();
39+
}
40+
41+
public function testNormalRead(): void
42+
{
43+
$file = self::$file;
44+
$reader = new XlsxReader();
45+
$spreadsheet = $reader->load($file);
46+
$sheet = $spreadsheet->getActiveSheet();
47+
self::assertSame('0', $sheet->getCell('A2')->getValue());
48+
self::assertTrue(
49+
$sheet->getCell('A2')
50+
->getIgnoredErrors()
51+
->getNumberStoredAsText()
52+
);
53+
self::assertFalse($sheet->cellExists('A3'));
54+
$spreadsheet->disconnectWorksheets();
55+
}
56+
}
6.03 KB
Binary file not shown.

0 commit comments

Comments
 (0)