Skip to content

Commit

Permalink
Allow color palette index values in number format masks
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkBaker committed Apr 6, 2023
1 parent 5da2f6e commit c5fb58f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/PhpSpreadsheet/Style/NumberFormat/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
37 changes: 37 additions & 0 deletions tests/data/Style/NumberFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit c5fb58f

Please sign in to comment.