From c5fb58fe686d1f9218bcb9273e47fc03ceba49ca Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Wed, 5 Apr 2023 09:02:47 +0200 Subject: [PATCH] Allow color palette index values in number format masks --- .../Style/NumberFormat/Formatter.php | 10 ++++- tests/data/Style/NumberFormat.php | 37 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php b/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php index 79c5b5d3d..41a17151d 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php +++ b/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php @@ -3,6 +3,7 @@ namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat; use PhpOffice\PhpSpreadsheet\Calculation\Calculation; +use PhpOffice\PhpSpreadsheet\Reader\Xls\Color\BIFF8; use PhpOffice\PhpSpreadsheet\RichText\RichText; use PhpOffice\PhpSpreadsheet\Style\Color; use PhpOffice\PhpSpreadsheet\Style\NumberFormat; @@ -67,14 +68,19 @@ class Formatter // 3 sections: [POSITIVE/TEXT] [NEGATIVE] [ZERO] // 4 sections: [POSITIVE] [NEGATIVE] [ZERO] [TEXT] $sectionCount = count($sections); - $color_regex = '/\\[(' . implode('|', Color::NAMED_COLORS) . ')\\]/mui'; + // Colour could be a named colour, or a numeric index entry in the colour-palette + $color_regex = '/\\[(' . implode('|', Color::NAMED_COLORS) . '|color\\s*(\\d+))\\]/mui'; $cond_regex = '/\\[(>|>=|<|<=|=|<>)([+-]?\\d+([.]\\d+)?)\\]/'; $colors = ['', '', '', '', '']; $conditionOperations = ['', '', '', '', '']; $conditionComparisonValues = [0, 0, 0, 0, 0]; for ($idx = 0; $idx < $sectionCount; ++$idx) { if (preg_match($color_regex, $sections[$idx], $matches)) { - $colors[$idx] = $matches[0]; + if (isset($matches[2])) { + $colors[$idx] = '#' . BIFF8::lookup((int) $matches[2] + 7)['rgb']; + } else { + $colors[$idx] = $matches[0]; + } $sections[$idx] = (string) preg_replace($color_regex, '', $sections[$idx]); } if (preg_match($cond_regex, $sections[$idx], $matches)) { diff --git a/tests/data/Style/NumberFormat.php b/tests/data/Style/NumberFormat.php index 43291c3e9..733f8c469 100644 --- a/tests/data/Style/NumberFormat.php +++ b/tests/data/Style/NumberFormat.php @@ -448,6 +448,43 @@ return [ -2, '[Green]"Positive";[Red]"Negative";[Blue]"Zero"', ], + // Colour palette index + [ + '+710', + 710, + '[color 10]+#,##0;[color 12]-#,##0', + ], + [ + '-710', + -710, + '[color 10]+#,##0;[color 12]-#,##0', + ], + // Colour palette index + [ + '+710', + 710, + '[color10]+#,##0;[color12]-#,##0', + ], + [ + '-710', + -710, + '[color10]+#,##0;[color12]-#,##0', + ], + [ + '-710', + -710, + '[color01]+#,##0;[color02]-#,##0', + ], + [ + '-710', + -710, + '[color08]+#,##0;[color09]-#,##0', + ], + [ + '-710', + -710, + '[color55]+#,##0;[color56]-#,##0', + ], // Value break points [ '<=3500 red',