From c5fb58fe686d1f9218bcb9273e47fc03ceba49ca Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Wed, 5 Apr 2023 09:02:47 +0200 Subject: [PATCH 1/2] 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 79c5b5d3d9..41a17151d3 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 @@ private static function splitFormatForSectionSelection(array $sections, $value): // 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 43291c3e94..733f8c469b 100644 --- a/tests/data/Style/NumberFormat.php +++ b/tests/data/Style/NumberFormat.php @@ -448,6 +448,43 @@ -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', From 44c74f8eca57692b0c670aa784303a1758cc13b1 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Thu, 6 Apr 2023 10:18:22 +0200 Subject: [PATCH 2/2] Update change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e8772ec36..70cde7180c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). - Xlsx Writer Honor Alignment in Default Font. [Issue #3443](https://github.com/PHPOffice/PhpSpreadsheet/issues/3443) [PR #3459](https://github.com/PHPOffice/PhpSpreadsheet/pull/3459) - Support Border for Charts. [PR #3462](https://github.com/PHPOffice/PhpSpreadsheet/pull/3462) - Error in "this row" structured reference calculation (cached result from first row when using a range) [Issue #3504](https://github.com/PHPOffice/PhpSpreadsheet/issues/3504) [PR #3505](https://github.com/PHPOffice/PhpSpreadsheet/pull/3505) +- Allow colour palette index references in Number Format masks [Issue #3511](https://github.com/PHPOffice/PhpSpreadsheet/issues/3511) [PR #3512](https://github.com/PHPOffice/PhpSpreadsheet/pull/3512) ## 1.28.0 - 2023-02-25