Skip to content

Commit 5e26de6

Browse files
committed
Handle Empty String in SharedStrings
Fix PHPOffice#4063. Fix PHPOffice#1560. Fix PHPOffice#1293. PhpSpreadsheet is not accounting for an empty string in Xlsx sharedStrings.xml.The code which parses it in Reader/Xlsx looks for a `t` or `r` tag descending from `si`, but, in this case, the tag is coded as `<si/>`, with neither t nor r tag descending. An else clause is added to set the string to empty string in this case. I was surprised that this had not turned up before, and a search through the archives found at least 2 earlier reports from 4 years ago. Those had been marked stale; the stale indicator is removed, and the issues are re-opened, to be closed when this PR is merged.
1 parent 318a82e commit 5e26de6

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/PhpSpreadsheet/Reader/Xlsx.php

+2
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,8 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
700700
$sharedStrings[] = StringHelper::controlCharacterOOXML2PHP((string) $val->t);
701701
} elseif (isset($val->r)) {
702702
$sharedStrings[] = $this->parseRichText($val);
703+
} else {
704+
$sharedStrings[] = '';
703705
}
704706
}
705707
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
6+
7+
use PhpOffice\PhpSpreadsheet\IOFactory;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class Issue4063Test extends TestCase
11+
{
12+
private static string $testbook = 'tests/data/Reader/XLSX/issue.4063.xlsx';
13+
14+
public function testSharedStringsWithEmptyString(): void
15+
{
16+
$spreadsheet = IOFactory::load(self::$testbook);
17+
$sheet = $spreadsheet->getActiveSheet();
18+
$data = $sheet->toArray(null, true, true, true);
19+
$nbsp = "\u{00a0}";
20+
self::assertSame(['A' => '226', 'B' => '', 'C' => $nbsp], $data[17]);
21+
self::assertSame(['A' => '38873', 'B' => 'gg', 'C' => ' '], $data[22]);
22+
$spreadsheet->disconnectWorksheets();
23+
}
24+
}
6.76 KB
Binary file not shown.

0 commit comments

Comments
 (0)