Skip to content

Commit 1d86675

Browse files
committed
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.
1 parent 1b68270 commit 1d86675

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/PhpSpreadsheet/Reader/Ods.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use DOMDocument;
77
use DOMElement;
88
use DOMNode;
9+
use DOMText;
910
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
1011
use PhpOffice\PhpSpreadsheet\Cell\DataType;
1112
use PhpOffice\PhpSpreadsheet\Helper\Dimension as HelperDimension;
@@ -403,8 +404,11 @@ public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Sp
403404
}
404405

405406
$columnID = 'A';
406-
/** @var DOMElement $cellData */
407+
/** @var DOMElement|DOMText $cellData */
407408
foreach ($childNode->childNodes as $cellData) {
409+
if ($cellData instanceof DOMText) {
410+
continue; // should just be whitespace
411+
}
408412
if ($this->getReadFilter() !== null) {
409413
if (!$this->getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
410414
if ($cellData->hasAttributeNS($tableNs, 'number-columns-repeated')) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;
6+
7+
use PhpOffice\PhpSpreadsheet\Reader\Ods;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class Issue804Test extends TestCase
11+
{
12+
public function testPreliminaries(): void
13+
{
14+
$file = 'zip://';
15+
$file .= 'tests/data/Reader/Ods/issue.804.ods';
16+
$file .= '#content.xml';
17+
$data = file_get_contents($file);
18+
// confirm that file contains expected namespaced xml tag
19+
if ($data === false) {
20+
self::fail('Unable to read file');
21+
} else {
22+
self::assertStringContainsString('<table:table-row>
23+
<table:table-cell office:value-type="string" table:number-rows-spanned="1" table:style-name="heading">
24+
<text:p>Name</text:p>', $data);
25+
}
26+
}
27+
28+
public function testIssue2810(): void
29+
{
30+
// Whitespace between Xml nodes
31+
$filename = 'tests/data/Reader/Ods/issue.804.ods';
32+
$reader = new Ods();
33+
$spreadsheet = $reader->load($filename);
34+
$sheet = $spreadsheet->getActiveSheet();
35+
self::assertSame('Straße', $sheet->getCell('G1')->getValue());
36+
$spreadsheet->disconnectWorksheets();
37+
}
38+
}

tests/data/Reader/Ods/issue.804.ods

4.86 KB
Binary file not shown.

0 commit comments

Comments
 (0)