Ods Xml Reader and Whitespace Text Nodes

Fix #804, opened in Dec. 2018, and closed as stale in Feb. 2019, and which I have re-opened to be closed properly by this PR. Better late than never, I suppose. A third party generated an ODS spreadsheet which PhpSpreadsheet could not read. By way of explanation, the xml in the file contained lots of whitespace between tags, which is wonderful for those humans among us who have to analyze it; but PhpSpreadsheet was not prepared for it. It is now.
This commit is contained in:
oleibman
2024-07-03 19:56:38 -07:00
parent 1b68270f80
commit 1d86675e04
3 changed files with 43 additions and 1 deletions
+5 -1
View File
@@ -6,6 +6,7 @@ use DOMAttr;
use DOMDocument;
use DOMElement;
use DOMNode;
use DOMText;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Helper\Dimension as HelperDimension;
@@ -403,8 +404,11 @@ class Ods extends BaseReader
}
$columnID = 'A';
/** @var DOMElement $cellData */
/** @var DOMElement|DOMText $cellData */
foreach ($childNode->childNodes as $cellData) {
if ($cellData instanceof DOMText) {
continue; // should just be whitespace
}
if ($this->getReadFilter() !== null) {
if (!$this->getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
if ($cellData->hasAttributeNS($tableNs, 'number-columns-repeated')) {
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;
use PhpOffice\PhpSpreadsheet\Reader\Ods;
use PHPUnit\Framework\TestCase;
class Issue804Test extends TestCase
{
public function testPreliminaries(): void
{
$file = 'zip://';
$file .= 'tests/data/Reader/Ods/issue.804.ods';
$file .= '#content.xml';
$data = file_get_contents($file);
// confirm that file contains expected namespaced xml tag
if ($data === false) {
self::fail('Unable to read file');
} else {
self::assertStringContainsString('<table:table-row>
<table:table-cell office:value-type="string" table:number-rows-spanned="1" table:style-name="heading">
<text:p>Name</text:p>', $data);
}
}
public function testIssue2810(): void
{
// Whitespace between Xml nodes
$filename = 'tests/data/Reader/Ods/issue.804.ods';
$reader = new Ods();
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('Straße', $sheet->getCell('G1')->getValue());
$spreadsheet->disconnectWorksheets();
}
}
Binary file not shown.