Skip to content

Commit 8c86caa

Browse files
authored
Merge pull request #4377 from oleibman/issue4375
Ignore ignoredErrors when Not Applicable
2 parents 0c5808e + 0153987 commit 8c86caa

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
3232
### Fixed
3333

3434
- Refactor Helper/Html. [PR #4359](https://github.com/PHPOffice/PhpSpreadsheet/pull/4359)
35+
- Ignore ignoredErrors when not applicable. [Issue #4375](https://github.com/PHPOffice/PhpSpreadsheet/issues/4375) [PR #4377](https://github.com/PHPOffice/PhpSpreadsheet/pull/4377)
3536
- Better handling of defined names on sheets whose titles include apostrophes. [Issue #4356](https://github.com/PHPOffice/PhpSpreadsheet/issues/4356) [Issue #4362](https://github.com/PHPOffice/PhpSpreadsheet/issues/4362) [Issue #4376](https://github.com/PHPOffice/PhpSpreadsheet/issues/4376) [PR #4360](https://github.com/PHPOffice/PhpSpreadsheet/pull/4360)
3637

3738
## 2025-02-08 - 4.0.0

src/PhpSpreadsheet/Collection/Cells.php

+5
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

+6-3
Original file line numberDiff line numberDiff line change
@@ -982,9 +982,8 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
982982
}
983983
}
984984
$docSheet->setSelectedCells($holdSelectedCells);
985-
if ($xmlSheetNS && $xmlSheetNS->ignoredErrors) {
986-
foreach ($xmlSheetNS->ignoredErrors->ignoredError as $ignoredErrorx) {
987-
$ignoredError = self::testSimpleXml($ignoredErrorx);
985+
if (!$this->readDataOnly && $xmlSheetNS && $xmlSheetNS->ignoredErrors) {
986+
foreach ($xmlSheetNS->ignoredErrors->ignoredError as $ignoredError) {
988987
$this->processIgnoredErrors($ignoredError, $docSheet);
989988
}
990989
}
@@ -2376,6 +2375,7 @@ private static function extractPalette(?SimpleXMLElement $sxml): array
23762375

23772376
private function processIgnoredErrors(SimpleXMLElement $xml, Worksheet $sheet): void
23782377
{
2378+
$cellCollection = $sheet->getCellCollection();
23792379
$attributes = self::getAttributes($xml);
23802380
$sqref = (string) ($attributes['sqref'] ?? '');
23812381
$numberStoredAsText = (string) ($attributes['numberStoredAsText'] ?? '');
@@ -2400,6 +2400,9 @@ private function processIgnoredErrors(SimpleXMLElement $xml, Worksheet $sheet):
24002400
++$lastCol;
24012401
for ($row = $firstRow; $row <= $lastRow; ++$row) {
24022402
for ($col = $firstCol; $col !== $lastCol; ++$col) {
2403+
if (!$cellCollection->has2("$col$row")) {
2404+
continue;
2405+
}
24032406
if ($numberStoredAsText === '1') {
24042407
$sheet->getCell("$col$row")->getIgnoredErrors()->setNumberStoredAsText(true);
24052408
}
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);
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)