Skip to content

Commit

Permalink
Merge pull request #3512 from PHPOffice/Issue-3511_NumberFormat-colou…
Browse files Browse the repository at this point in the history
…r-indexed-palette

Allow color palette index values in number format masks
  • Loading branch information
MarkBaker committed Apr 7, 2023
2 parents e5ed8e9 + 24725c8 commit 5ef48e9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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 5ef48e9

Please sign in to comment.