Skip to content

Commit 5ef48e9

Browse files
authored
Merge pull request #3512 from PHPOffice/Issue-3511_NumberFormat-colour-indexed-palette
Allow color palette index values in number format masks
2 parents e5ed8e9 + 24725c8 commit 5ef48e9

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
4747
- 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)
4848
- Support Border for Charts. [PR #3462](https://github.com/PHPOffice/PhpSpreadsheet/pull/3462)
4949
- 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)
50+
- 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)
5051

5152

5253
## 1.28.0 - 2023-02-25

src/PhpSpreadsheet/Style/NumberFormat/Formatter.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
44

55
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6+
use PhpOffice\PhpSpreadsheet\Reader\Xls\Color\BIFF8;
67
use PhpOffice\PhpSpreadsheet\RichText\RichText;
78
use PhpOffice\PhpSpreadsheet\Style\Color;
89
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
@@ -67,14 +68,19 @@ private static function splitFormatForSectionSelection(array $sections, $value):
6768
// 3 sections: [POSITIVE/TEXT] [NEGATIVE] [ZERO]
6869
// 4 sections: [POSITIVE] [NEGATIVE] [ZERO] [TEXT]
6970
$sectionCount = count($sections);
70-
$color_regex = '/\\[(' . implode('|', Color::NAMED_COLORS) . ')\\]/mui';
71+
// Colour could be a named colour, or a numeric index entry in the colour-palette
72+
$color_regex = '/\\[(' . implode('|', Color::NAMED_COLORS) . '|color\\s*(\\d+))\\]/mui';
7173
$cond_regex = '/\\[(>|>=|<|<=|=|<>)([+-]?\\d+([.]\\d+)?)\\]/';
7274
$colors = ['', '', '', '', ''];
7375
$conditionOperations = ['', '', '', '', ''];
7476
$conditionComparisonValues = [0, 0, 0, 0, 0];
7577
for ($idx = 0; $idx < $sectionCount; ++$idx) {
7678
if (preg_match($color_regex, $sections[$idx], $matches)) {
77-
$colors[$idx] = $matches[0];
79+
if (isset($matches[2])) {
80+
$colors[$idx] = '#' . BIFF8::lookup((int) $matches[2] + 7)['rgb'];
81+
} else {
82+
$colors[$idx] = $matches[0];
83+
}
7884
$sections[$idx] = (string) preg_replace($color_regex, '', $sections[$idx]);
7985
}
8086
if (preg_match($cond_regex, $sections[$idx], $matches)) {

tests/data/Style/NumberFormat.php

+37
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,43 @@
448448
-2,
449449
'[Green]"Positive";[Red]"Negative";[Blue]"Zero"',
450450
],
451+
// Colour palette index
452+
[
453+
'+710',
454+
710,
455+
'[color 10]+#,##0;[color 12]-#,##0',
456+
],
457+
[
458+
'-710',
459+
-710,
460+
'[color 10]+#,##0;[color 12]-#,##0',
461+
],
462+
// Colour palette index
463+
[
464+
'+710',
465+
710,
466+
'[color10]+#,##0;[color12]-#,##0',
467+
],
468+
[
469+
'-710',
470+
-710,
471+
'[color10]+#,##0;[color12]-#,##0',
472+
],
473+
[
474+
'-710',
475+
-710,
476+
'[color01]+#,##0;[color02]-#,##0',
477+
],
478+
[
479+
'-710',
480+
-710,
481+
'[color08]+#,##0;[color09]-#,##0',
482+
],
483+
[
484+
'-710',
485+
-710,
486+
'[color55]+#,##0;[color56]-#,##0',
487+
],
451488
// Value break points
452489
[
453490
'<=3500 red',

0 commit comments

Comments
 (0)