Xls Writer overwrites a custom palette color when the same color is added with different letter case
This is:
- a bug report
- a feature request
- not a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)
What is the expected behavior?
Each distinct color used in a workbook should get its own slot in the Xls color palette, so the colors read back from the saved .xls file match the colors that were set.
What is the current behavior?
Two custom colors can end up sharing the same palette slot: the color added later overwrites the earlier one. In the example below the gray font color 636363 is written to the file as the blue fill color 0097ff, so the gray text renders blue.
The same bug also exists on the currently supported 1.30.x / 2.1.x branches (the addColor() code is unchanged there). It does not reproduce when all colors use a consistent letter case, which is why it can stay hidden for a long time.
What are the steps to reproduce?
<?php
require __DIR__ . '/vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Reader\Xls as XlsReader;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Writer\Xls;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'header');
$sheet->setCellValue('A2', 'data');
// Data cell: a custom gray font color (not part of the built-in Xls palette).
$sheet->getStyle('A2')->getFont()->getColor()->setRGB('636363');
// Header: a lower-case white font (duplicates the 'FFFFFF' cached by the default style)
// on a custom blue fill.
$sheet->getStyle('A1')->applyFromArray([
'fill' => [
'fillType' => Fill::FILL_SOLID,
'color' => ['rgb' => '0097ff'],
],
'font' => [
'color' => ['rgb' => 'ffffff'],
],
]);
$path = sys_get_temp_dir() . '/xls-palette-color.xls';
(new Xls($spreadsheet))->save($path);
$readBack = (new XlsReader())->load($path)->getActiveSheet();
echo $readBack->getStyle('A1')->getFont()->getColor()->getARGB(), "\n"; // FFFFFFFF
echo $readBack->getStyle('A2')->getFont()->getColor()->getARGB(), "\n"; // FF0097FF, expected FF636363Output:
FFFFFFFF
FF0097FF <- expected FF636363Why it happens. Writer\Xls\Workbook::addColor() caches colors by their exact RGB string and picks the next free palette index with end($this->colors):
if (count($this->colors) === 0) {
$lastColor = 7;
} else {
$lastColor = end($this->colors);
}end() returns the value of the most recently inserted entry, not the highest index in use. A color that matches an existing palette entry is cached under its own (case-sensitive) key with the matched index. Here:
000000is cached as palette index 8 (default style).FFFFFFmatches the default palette white and is cached as index 9.636363is not in the palette, so it is cached as index 10.ffffffmatches the palette white again and is cached under a different key with index 9. Because it is appended to the cache,end($this->colors)now returns 9 again.0097fftherefore gets9 + 1 = 10and overwrites the palette slot that636363is using.
What features do you think are causing the issue
- Reader
- Writer
- Styles
- Data Validations
- Formula Calculations
- Charts
- AutoFilter
- Form Elements
Does an issue affect all spreadsheet file formats? If not, which formats are affected?
Only Xls (BIFF). Xlsx writes colors as RGB values, so it is unaffected.
Which versions of PhpSpreadsheet and PHP are affected?
The addColor() logic is unchanged from 1.30 through master (5.10), so all these versions are affected. Reproduced with master on PHP 8.5.
Source: PHPOffice/PhpSpreadsheet