Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xlsx Reader Better Namespace Handling Phase 1 Second Bugfix #2303

Merged
merged 3 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Phase 1 of better namespace handling for Xlsx, resolving many open issues.
[PR #2173](https://github.com/PHPOffice/PhpSpreadsheet/pull/2173)
[PR #2204](https://github.com/PHPOffice/PhpSpreadsheet/pull/2204)
[PR #2303](https://github.com/PHPOffice/PhpSpreadsheet/pull/2303)
- Add ability to extract images if source is a URL. [Issue #1997](https://github.com/PHPOffice/PhpSpreadsheet/issues/1997) [PR #2072](https://github.com/PHPOffice/PhpSpreadsheet/pull/2072)
- Support for passing flags in the Reader `load()` and Writer `save()`methods, and through the IOFactory, to set behaviours. [PR #2136](https://github.com/PHPOffice/PhpSpreadsheet/pull/2136)
- See [documentation](https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-and-writing-to-file/) for details
Expand Down
77 changes: 47 additions & 30 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -1675,46 +1675,63 @@ private function parseRichText(?SimpleXMLElement $is)
} else {
$objText = $value->createTextRun(StringHelper::controlCharacterOOXML2PHP((string) $run->t));

if (isset($run->rPr->rFont['val'])) {
$objText->getFont()->setName((string) $run->rPr->rFont['val']);
$attr = $run->rPr->rFont->attributes();
if (isset($attr['val'])) {
$objText->getFont()->setName((string) $attr['val']);
}
if (isset($run->rPr->sz['val'])) {
$objText->getFont()->setSize((float) $run->rPr->sz['val']);
$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(Styles::readColor($run->rPr->color)));
}
if (
(isset($run->rPr->b['val']) && self::boolean((string) $run->rPr->b['val'])) ||
(isset($run->rPr->b) && !isset($run->rPr->b['val']))
) {
$objText->getFont()->setBold(true);
}
if (
(isset($run->rPr->i['val']) && self::boolean((string) $run->rPr->i['val'])) ||
(isset($run->rPr->i) && !isset($run->rPr->i['val']))
) {
$objText->getFont()->setItalic(true);
if (isset($run->rPr->b)) {
$attr = $run->rPr->b->attributes();
if (
(isset($attr['val']) && self::boolean((string) $attr['val'])) ||
(!isset($attr['val']))
) {
$objText->getFont()->setBold(true);
}
}
if (isset($run->rPr->vertAlign, $run->rPr->vertAlign['val'])) {
$vertAlign = strtolower((string) $run->rPr->vertAlign['val']);
if ($vertAlign == 'superscript') {
$objText->getFont()->setSuperscript(true);
if (isset($run->rPr->i)) {
$attr = $run->rPr->i->attributes();
if (
(isset($attr['val']) && self::boolean((string) $attr['val'])) ||
(!isset($attr['val']))
) {
$objText->getFont()->setItalic(true);
}
if ($vertAlign == 'subscript') {
$objText->getFont()->setSubscript(true);
}
if (isset($run->rPr->vertAlign)) {
$attr = $run->rPr->vertAlign->attributes();
if (isset($attr['val'])) {
$vertAlign = strtolower((string) $attr['val']);
if ($vertAlign == 'superscript') {
$objText->getFont()->setSuperscript(true);
}
if ($vertAlign == 'subscript') {
$objText->getFont()->setSubscript(true);
}
}
}
if (isset($run->rPr->u) && !isset($run->rPr->u['val'])) {
$objText->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE);
} elseif (isset($run->rPr->u, $run->rPr->u['val'])) {
$objText->getFont()->setUnderline((string) $run->rPr->u['val']);
if (isset($run->rPr->u)) {
$attr = $run->rPr->u->attributes();
if (!isset($attr['val'])) {
$objText->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE);
} else {
$objText->getFont()->setUnderline((string) $attr['val']);
}
}
if (
(isset($run->rPr->strike['val']) && self::boolean((string) $run->rPr->strike['val'])) ||
(isset($run->rPr->strike) && !isset($run->rPr->strike['val']))
) {
$objText->getFont()->setStrikethrough(true);
if (isset($run->rPr->strike)) {
$attr = $run->rPr->strike->attributes();
if (
(isset($attr['val']) && self::boolean((string) $attr['val'])) ||
(!isset($attr['val']))
) {
$objText->getFont()->setStrikethrough(true);
}
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/Issue2301Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\RichText\RichText;

class Issue2301Test extends \PHPUnit\Framework\TestCase
{
/**
* @var string
*/
private static $testbook = 'tests/data/Reader/XLSX/issue.2301.xlsx';

public static function testReadRichText(): void
{
$spreadsheet = IOFactory::load(self::$testbook);
$sheet = $spreadsheet->getActiveSheet();
$value = $sheet->getCell('B45')->getValue();
self::assertInstanceOf(RichText::class, $value);
$richtext = $value->getRichTextElements();
$font = $richtext[1]->getFont();
self::assertNotNull($font);
self::assertSame('Arial CE', $font->getName());
self::assertSame(9.0, $font->getSize());
self::assertSame('protected', $sheet->getCell('BT10')->getStyle()->getProtection()->getHidden());
}
}
Binary file added tests/data/Reader/XLSX/issue.2301.xlsx
Binary file not shown.