Skip to content

Commit

Permalink
Fix incorrect font color read from xlsx
Browse files Browse the repository at this point in the history
Place the read font color after setting the styles
  • Loading branch information
anhoder committed Mar 18, 2023
1 parent 452ec07 commit f32fc43
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,25 +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;
Expand Down Expand Up @@ -682,6 +663,29 @@ 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
40 changes: 40 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3464Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
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();
$this->assertInstanceOf(RichText::class, $rickText);

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

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

$this->assertEquals('(这是一行示例数据,在导入时需要删除该行)', $elements[1]->getText());
$font = $elements[1]->getFont();
$this->assertNotNull($font);
$this->assertEquals('ff2600', $font->getColor()->getRGB());
}

}
Binary file added tests/data/Reader/XLSX/issue.3464.xlsx
Binary file not shown.

0 comments on commit f32fc43

Please sign in to comment.