mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-15 12:36:32 +00:00
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:
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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.
Reference in New Issue
Block a user