Xlsx Reader Warning When No sz Tag for RichText (#2550)

Fix #2542. Xlsx Reader is expecting a `sz` tag when reading RichText, but it is not required, and PhpSpreadsheet issues a warning message when it is missing.
This commit is contained in:
oleibman 2022-02-12 06:43:29 -08:00 committed by GitHub
parent b300e186fe
commit 5bf0656e92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 6 deletions

View File

@ -1691,13 +1691,17 @@ class Xlsx extends BaseReader
} else {
$objText = $value->createTextRun(StringHelper::controlCharacterOOXML2PHP((string) $run->t));
$attr = $run->rPr->rFont->attributes();
if (isset($attr['val'])) {
$objText->getFont()->setName((string) $attr['val']);
if (isset($run->rPr->rFont)) {
$attr = $run->rPr->rFont->attributes();
if (isset($attr['val'])) {
$objText->getFont()->setName((string) $attr['val']);
}
}
$attr = $run->rPr->sz->attributes();
if (isset($attr['val'])) {
$objText->getFont()->setSize((float) $attr['val']);
if (isset($run->rPr->sz)) {
$attr = $run->rPr->sz->attributes();
if (isset($attr['val'])) {
$objText->getFont()->setSize((float) $attr['val']);
}
}
if (isset($run->rPr->color)) {
$objText->getFont()->setColor(new Color($this->styleReader->readColor($run->rPr->color)));

View File

@ -0,0 +1,47 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PHPUnit\Framework\TestCase;
class Issue2542Test extends TestCase
{
/**
* @var string
*/
private static $testbook = 'tests/data/Reader/XLSX/issue.2542.xlsx';
public function testPreliminaries(): void
{
// Rich text without 'sz' tag
$file = 'zip://';
$file .= self::$testbook;
$file .= '#xl/sharedStrings.xml';
$data = file_get_contents($file);
// confirm that file contains expected namespaced xml tag
if ($data === false) {
self::fail('Unable to read file sharedStrings.xml');
} else {
self::assertStringContainsString('<si><r><rPr><rFont val="Arial"/><b/><color theme="1"/></rPr><t xml:space="preserve">Factor group
</t></r><r><rPr><rFont val="Arial"/><b val="0"/><color theme="1"/></rPr><t>(for Rental items only)</t></r></si>', $data);
}
}
public function testIssue2542(): void
{
$filename = self::$testbook;
$reader = new Xlsx();
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
$value = $sheet->getCell('P1')->getValue();
if ($value instanceof RichText) {
self::assertSame("Factor group\n(for Rental items only)", $value->getPlainText());
} else {
self::fail('Cell P1 is not RichText');
}
$spreadsheet->disconnectWorksheets();
}
}

Binary file not shown.