Skip to content

Commit cd9811c

Browse files
authored
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.
1 parent c6b095b commit cd9811c

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2162,6 +2162,6 @@ private static function extractPalette(?SimpleXMLElement $sxml): array
21622162
}
21632163
}
21642164

2165-
return (count($array) === 64) ? $array : [];
2165+
return $array;
21662166
}
21672167
}

src/PhpSpreadsheet/Reader/Xlsx/Styles.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,12 @@ public function readColor(SimpleXMLElement $color, bool $background = false): st
365365
return (string) $attr['rgb'];
366366
}
367367
if (isset($attr['indexed'])) {
368-
if (empty($this->workbookPalette)) {
369-
return Color::indexedColor((int) ($attr['indexed'] - 7), $background)->getARGB() ?? '';
368+
$indexedColor = (int) $attr['indexed'];
369+
if ($indexedColor >= count($this->workbookPalette)) {
370+
return Color::indexedColor($indexedColor - 7, $background)->getARGB() ?? '';
370371
}
371372

372-
return Color::indexedColor((int) ($attr['indexed']), $background, $this->workbookPalette)->getARGB() ?? '';
373+
return Color::indexedColor($indexedColor, $background, $this->workbookPalette)->getARGB() ?? '';
373374
}
374375
if (isset($attr['theme'])) {
375376
if ($this->theme !== null) {

tests/PhpSpreadsheetTests/Reader/Xlsx/Issue2490Test.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class Issue2490Test extends TestCase
1212
*/
1313
private static $testbook = 'tests/data/Reader/XLSX/issue.2490.xlsx';
1414

15+
/**
16+
* @var string
17+
*/
18+
private static $testbook3093 = 'tests/data/Reader/XLSX/issue.3093.xlsx';
19+
1520
public function testPreliminaries(): void
1621
{
1722
$file = 'zip://';
@@ -23,6 +28,7 @@ public function testPreliminaries(): void
2328
self::fail('Unable to read file');
2429
} else {
2530
self::assertStringContainsString('<colors><indexedColors><rgbColor rgb="00000000"/>', $data);
31+
self::assertSame(64, substr_count($data, '<rgbColor'));
2632
}
2733
}
2834

@@ -38,4 +44,34 @@ public function testIssue2490(): void
3844
self::assertSame('00F0F0F0', $sheet->getCell('B1')->getStyle()->getFill()->getStartColor()->getArgb());
3945
$spreadsheet->disconnectWorksheets();
4046
}
47+
48+
public function testPreliminaries3093(): void
49+
{
50+
$file = 'zip://';
51+
$file .= self::$testbook3093;
52+
$file .= '#xl/styles.xml';
53+
$data = file_get_contents($file);
54+
// confirm that file contains expected color index tag
55+
if ($data === false) {
56+
self::fail('Unable to read file');
57+
} else {
58+
self::assertStringContainsString('<colors><indexedColors><rgbColor rgb="ff000000"/>', $data);
59+
self::assertSame(15, substr_count($data, '<rgbColor'));
60+
}
61+
}
62+
63+
public function testIssue3093(): void
64+
{
65+
// Same as above, except with fewer than 64 entries.
66+
// (And with colors in lowercase hex and alpha set to ff.)
67+
$filename = self::$testbook3093;
68+
$reader = IOFactory::createReader('Xlsx');
69+
$spreadsheet = $reader->load($filename);
70+
$sheet = $spreadsheet->getActiveSheet();
71+
self::assertSame('ffc0c0c0', $sheet->getCell('B2')->getStyle()->getFill()->getStartColor()->getArgb());
72+
self::assertSame('ffffff00', $sheet->getCell('D2')->getStyle()->getFill()->getStartColor()->getArgb());
73+
self::assertSame('ffdfa7a6', $sheet->getCell('F2')->getStyle()->getFill()->getStartColor()->getArgb());
74+
self::assertSame('ff7ba0cd', $sheet->getCell('H2')->getStyle()->getFill()->getStartColor()->getArgb());
75+
$spreadsheet->disconnectWorksheets();
76+
}
4177
}
42.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)