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.
This commit is contained in:
oleibman
2025-02-19 19:15:01 -08:00
parent cd7386f912
commit 2b7e6f50a2
4 changed files with 67 additions and 3 deletions
+5
View File
@@ -81,6 +81,11 @@ class Cells
return ($cellCoordinate === $this->currentCoordinate) || isset($this->index[$cellCoordinate]);
}
public function has2(string $cellCoordinate): bool
{
return isset($this->index[$cellCoordinate]);
}
/**
* Add or update a cell in the collection.
*
+6 -3
View File
@@ -980,9 +980,8 @@ class Xlsx extends BaseReader
}
}
$docSheet->setSelectedCells($holdSelectedCells);
if ($xmlSheetNS && $xmlSheetNS->ignoredErrors) {
foreach ($xmlSheetNS->ignoredErrors->ignoredError as $ignoredErrorx) {
$ignoredError = self::testSimpleXml($ignoredErrorx);
if (!$this->readDataOnly && $xmlSheetNS && $xmlSheetNS->ignoredErrors) {
foreach ($xmlSheetNS->ignoredErrors->ignoredError as $ignoredError) {
$this->processIgnoredErrors($ignoredError, $docSheet);
}
}
@@ -2375,6 +2374,7 @@ class Xlsx extends BaseReader
private function processIgnoredErrors(SimpleXMLElement $xml, Worksheet $sheet): void
{
$cellCollection = $sheet->getCellCollection();
$attributes = self::getAttributes($xml);
$sqref = (string) ($attributes['sqref'] ?? '');
$numberStoredAsText = (string) ($attributes['numberStoredAsText'] ?? '');
@@ -2399,6 +2399,9 @@ class Xlsx extends BaseReader
++$lastCol;
for ($row = $firstRow; $row <= $lastRow; ++$row) {
for ($col = $firstCol; $col !== $lastCol; ++$col) {
if (!$cellCollection->has2("$col$row")) {
continue;
}
if ($numberStoredAsText === '1') {
$sheet->getCell("$col$row")->getIgnoredErrors()->setNumberStoredAsText(true);
}
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PHPUnit\Framework\TestCase;
class Issue4375Test extends TestCase
{
private static string $file = 'tests/data/Reader/XLSX/issue.4375.small.xlsx';
public function testPreliminaries(): void
{
$file = 'zip://';
$file .= self::$file;
$file .= '#xl/worksheets/sheet1.xml';
$data = file_get_contents($file) ?: '';
$expected = '<ignoredErrors><ignoredError sqref="A2:B5 B1:F1" numberStoredAsText="1"/></ignoredErrors>';
self::assertStringContainsString($expected, $data, 'neither fgColor nor bgColor');
}
public function testDataOnly(): void
{
$file = self::$file;
$reader = new XlsxReader();
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($file);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('0', $sheet->getCell('A2')->getValue());
self::assertFalse(
$sheet->getCell('A2')
->getIgnoredErrors()
->getNumberStoredAsText()
);
self::assertFalse($sheet->cellExists('A3'));
$spreadsheet->disconnectWorksheets();
}
public function testNormalRead(): void
{
$file = self::$file;
$reader = new XlsxReader();
$spreadsheet = $reader->load($file);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('0', $sheet->getCell('A2')->getValue());
self::assertTrue(
$sheet->getCell('A2')
->getIgnoredErrors()
->getNumberStoredAsText()
);
self::assertFalse($sheet->cellExists('A3'));
$spreadsheet->disconnectWorksheets();
}
}
Binary file not shown.