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

Fix incorrect font color read from xlsx #3465

Merged
merged 2 commits into from
Mar 18, 2023
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
41 changes: 21 additions & 20 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,26 +507,6 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
$relsWorkbook = $this->loadZip("$dir/_rels/" . basename($relTarget) . '.rels', '');
$relsWorkbook->registerXPathNamespace('rel', Namespaces::RELATIONSHIPS);

$sharedStrings = [];
$relType = "rel:Relationship[@Type='"
//. Namespaces::SHARED_STRINGS
. "$xmlNamespaceBase/sharedStrings"
. "']";
$xpath = self::getArrayItem($relsWorkbook->xpath($relType));

if ($xpath) {
$xmlStrings = $this->loadZip("$dir/$xpath[Target]", $mainNS);
if (isset($xmlStrings->si)) {
foreach ($xmlStrings->si as $val) {
if (isset($val->t)) {
$sharedStrings[] = StringHelper::controlCharacterOOXML2PHP((string) $val->t);
} elseif (isset($val->r)) {
$sharedStrings[] = $this->parseRichText($val);
}
}
}
}

$worksheets = [];
$macros = $customUI = null;
foreach ($relsWorkbook->Relationship as $elex) {
Expand Down Expand Up @@ -682,6 +662,27 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
$dxfs = $this->styleReader->dxfs($this->readDataOnly);
$styles = $this->styleReader->styles();

// Read content after setting the styles
$sharedStrings = [];
$relType = "rel:Relationship[@Type='"
//. Namespaces::SHARED_STRINGS
. "$xmlNamespaceBase/sharedStrings"
. "']";
$xpath = self::getArrayItem($relsWorkbook->xpath($relType));

if ($xpath) {
$xmlStrings = $this->loadZip("$dir/$xpath[Target]", $mainNS);
if (isset($xmlStrings->si)) {
foreach ($xmlStrings->si as $val) {
if (isset($val->t)) {
$sharedStrings[] = StringHelper::controlCharacterOOXML2PHP((string) $val->t);
} elseif (isset($val->r)) {
$sharedStrings[] = $this->parseRichText($val);
}
}
}
}

$xmlWorkbook = $this->loadZipNoNamespace($relTarget, $mainNS);
$xmlWorkbookNS = $this->loadZip($relTarget, $mainNS);

Expand Down
38 changes: 38 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3464Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

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

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

public function testReadFontColor(): void
{
$inputFileType = IOFactory::identify(self::$testbook);
$objReader = IOFactory::createReader($inputFileType);
$objReader->setReadEmptyCells(false);

$sheet = $objReader->load(self::$testbook)->getActiveSheet();
$rickText = $sheet->getCell([1, 1])->getValue();
self::assertInstanceOf(RichText::class, $rickText);

$elements = $rickText->getRichTextElements();
self::assertCount(2, $elements);

self::assertEquals("产品介绍\n", $elements[0]->getText());
$font = $elements[0]->getFont();
self::assertNotNull($font);
self::assertEquals('7f7f7f', $font->getColor()->getRGB());

self::assertEquals('(这是一行示例数据,在导入时需要删除该行)', $elements[1]->getText());
$font = $elements[1]->getFont();
self::assertNotNull($font);
self::assertEquals('ff2600', $font->getColor()->getRGB());
}
}
Binary file added tests/data/Reader/XLSX/issue.3464.xlsx
Binary file not shown.