Xlsx Reader Accept Palette of Fewer than 64 Colors (#3096)

Fix #3093. PR #2595 added the ability for Xlsx Reader to accept a custom color palette. With no examples other than the one at hand, which included a full complement of 64 colors, that PR required 64 colors in the palette. It turns out that Mac Numbers exports to Excel with a palette with less than 64 colors. So, with an example of that at hand, relax the original restriction and accept a palette of any size.
This commit is contained in:
oleibman 2022-10-02 18:02:34 -07:00 committed by GitHub
parent c6b095b626
commit cd9811cbd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 4 deletions

View File

@ -2162,6 +2162,6 @@ class Xlsx extends BaseReader
}
}
return (count($array) === 64) ? $array : [];
return $array;
}
}

View File

@ -365,11 +365,12 @@ class Styles extends BaseParserClass
return (string) $attr['rgb'];
}
if (isset($attr['indexed'])) {
if (empty($this->workbookPalette)) {
return Color::indexedColor((int) ($attr['indexed'] - 7), $background)->getARGB() ?? '';
$indexedColor = (int) $attr['indexed'];
if ($indexedColor >= count($this->workbookPalette)) {
return Color::indexedColor($indexedColor - 7, $background)->getARGB() ?? '';
}
return Color::indexedColor((int) ($attr['indexed']), $background, $this->workbookPalette)->getARGB() ?? '';
return Color::indexedColor($indexedColor, $background, $this->workbookPalette)->getARGB() ?? '';
}
if (isset($attr['theme'])) {
if ($this->theme !== null) {

View File

@ -12,6 +12,11 @@ class Issue2490Test extends TestCase
*/
private static $testbook = 'tests/data/Reader/XLSX/issue.2490.xlsx';
/**
* @var string
*/
private static $testbook3093 = 'tests/data/Reader/XLSX/issue.3093.xlsx';
public function testPreliminaries(): void
{
$file = 'zip://';
@ -23,6 +28,7 @@ class Issue2490Test extends TestCase
self::fail('Unable to read file');
} else {
self::assertStringContainsString('<colors><indexedColors><rgbColor rgb="00000000"/>', $data);
self::assertSame(64, substr_count($data, '<rgbColor'));
}
}
@ -38,4 +44,34 @@ class Issue2490Test extends TestCase
self::assertSame('00F0F0F0', $sheet->getCell('B1')->getStyle()->getFill()->getStartColor()->getArgb());
$spreadsheet->disconnectWorksheets();
}
public function testPreliminaries3093(): void
{
$file = 'zip://';
$file .= self::$testbook3093;
$file .= '#xl/styles.xml';
$data = file_get_contents($file);
// confirm that file contains expected color index tag
if ($data === false) {
self::fail('Unable to read file');
} else {
self::assertStringContainsString('<colors><indexedColors><rgbColor rgb="ff000000"/>', $data);
self::assertSame(15, substr_count($data, '<rgbColor'));
}
}
public function testIssue3093(): void
{
// Same as above, except with fewer than 64 entries.
// (And with colors in lowercase hex and alpha set to ff.)
$filename = self::$testbook3093;
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('ffc0c0c0', $sheet->getCell('B2')->getStyle()->getFill()->getStartColor()->getArgb());
self::assertSame('ffffff00', $sheet->getCell('D2')->getStyle()->getFill()->getStartColor()->getArgb());
self::assertSame('ffdfa7a6', $sheet->getCell('F2')->getStyle()->getFill()->getStartColor()->getArgb());
self::assertSame('ff7ba0cd', $sheet->getCell('H2')->getStyle()->getFill()->getStartColor()->getArgb());
$spreadsheet->disconnectWorksheets();
}
}

Binary file not shown.