Handle Empty String in SharedStrings

Fix #4063. Fix #1560. Fix #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.
This commit is contained in:
oleibman
2024-06-06 06:17:02 -07:00
parent 318a82e0f9
commit 5e26de63a9
3 changed files with 26 additions and 0 deletions
+2
View File
@@ -700,6 +700,8 @@ class Xlsx extends BaseReader
$sharedStrings[] = StringHelper::controlCharacterOOXML2PHP((string) $val->t);
} elseif (isset($val->r)) {
$sharedStrings[] = $this->parseRichText($val);
} else {
$sharedStrings[] = '';
}
}
}
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PHPUnit\Framework\TestCase;
class Issue4063Test extends TestCase
{
private static string $testbook = 'tests/data/Reader/XLSX/issue.4063.xlsx';
public function testSharedStringsWithEmptyString(): void
{
$spreadsheet = IOFactory::load(self::$testbook);
$sheet = $spreadsheet->getActiveSheet();
$data = $sheet->toArray(null, true, true, true);
$nbsp = "\u{00a0}";
self::assertSame(['A' => '226', 'B' => '', 'C' => $nbsp], $data[17]);
self::assertSame(['A' => '38873', 'B' => 'gg', 'C' => ' '], $data[22]);
$spreadsheet->disconnectWorksheets();
}
}
Binary file not shown.