Skip to content

Commit 10d6017

Browse files
committed
Xlsx Reader Warning When No sz Tag for RichText
Fix PHPOffice#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.
1 parent 35dfdc6 commit 10d6017

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,13 +1691,17 @@ private function parseRichText(?SimpleXMLElement $is)
16911691
} else {
16921692
$objText = $value->createTextRun(StringHelper::controlCharacterOOXML2PHP((string) $run->t));
16931693

1694-
$attr = $run->rPr->rFont->attributes();
1695-
if (isset($attr['val'])) {
1696-
$objText->getFont()->setName((string) $attr['val']);
1694+
if (isset($run->rPr->rFont)) {
1695+
$attr = $run->rPr->rFont->attributes();
1696+
if (isset($attr['val'])) {
1697+
$objText->getFont()->setName((string) $attr['val']);
1698+
}
16971699
}
1698-
$attr = $run->rPr->sz->attributes();
1699-
if (isset($attr['val'])) {
1700-
$objText->getFont()->setSize((float) $attr['val']);
1700+
if (isset($run->rPr->sz)) {
1701+
$attr = $run->rPr->sz->attributes();
1702+
if (isset($attr['val'])) {
1703+
$objText->getFont()->setSize((float) $attr['val']);
1704+
}
17011705
}
17021706
if (isset($run->rPr->color)) {
17031707
$objText->getFont()->setColor(new Color($this->styleReader->readColor($run->rPr->color)));
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
6+
use PhpOffice\PhpSpreadsheet\RichText\RichText;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class Issue2542Test extends TestCase
10+
{
11+
/**
12+
* @var string
13+
*/
14+
private static $testbook = 'tests/data/Reader/XLSX/issue.2542.xlsx';
15+
16+
public function testPreliminaries(): void
17+
{
18+
// Rich text without 'sz' tag
19+
$file = 'zip://';
20+
$file .= self::$testbook;
21+
$file .= '#xl/sharedStrings.xml';
22+
$data = file_get_contents($file);
23+
24+
// confirm that file contains expected namespaced xml tag
25+
if ($data === false) {
26+
self::fail('Unable to read file sharedStrings.xml');
27+
} else {
28+
self::assertStringContainsString('<si><r><rPr><rFont val="Arial"/><b/><color theme="1"/></rPr><t xml:space="preserve">Factor group
29+
</t></r><r><rPr><rFont val="Arial"/><b val="0"/><color theme="1"/></rPr><t>(for Rental items only)</t></r></si>', $data);
30+
}
31+
}
32+
33+
public function testIssue2542(): void
34+
{
35+
$filename = self::$testbook;
36+
$reader = new Xlsx();
37+
$spreadsheet = $reader->load($filename);
38+
$sheet = $spreadsheet->getActiveSheet();
39+
$value = $sheet->getCell('P1')->getValue();
40+
if ($value instanceof RichText) {
41+
self::assertSame("Factor group\n(for Rental items only)", $value->getPlainText());
42+
} else {
43+
self::fail('Cell P1 is not RichText');
44+
}
45+
$spreadsheet->disconnectWorksheets();
46+
}
47+
}
15.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)