mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-19 01:50:34 +00:00
Fix incorrect font color read from xlsx (#3465)
Place the read font color after setting the styles
This commit is contained in:
@@ -507,26 +507,6 @@ class Xlsx extends BaseReader
|
||||
$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) {
|
||||
@@ -682,6 +662,27 @@ class Xlsx extends BaseReader
|
||||
$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);
|
||||
|
||||
|
||||
@@ -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 not shown.
Reference in New Issue
Block a user