From ab1c6e53b6a41fa558cf8b81ddcd42cc27180044 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Sun, 14 Jul 2019 19:36:34 +0200 Subject: [PATCH] Number format with colours (#1080) * Fix number formatting using named colours in format masks * Handle number formatting with ful substitution of values by a string * Update changelog --- CHANGELOG.md | 3 +- src/PhpSpreadsheet/Style/Color.php | 11 ++++++ src/PhpSpreadsheet/Style/NumberFormat.php | 11 +++--- tests/data/Style/NumberFormat.php | 42 +++++++++++++++++++++++ tests/data/Style/NumberFormatDates.php | 18 ---------- 5 files changed, 61 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b626d2c5..a8960f5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org). ### Fixed -- Stricter-typed comparison testing in COUNTIF() and COUNTIFS() evaluation [Issue #1046](https://github.com/PHPOffice/PhpSpreadsheet/issues/1046) +- Fix number format masks containing named colours that were being misinterpreted as date formats; and add support for masks that fully replace the value with a full text string [Issue #1009](https://github.com/PHPOffice/PhpSpreadsheet/issues/1009) +- Stricter-typed comparison testing in COUNTIF() and COUNTIFS() evaluation [Issue #1046](https://github.com/PHPOffice/PhpSpreadsheet/issues/1046) - COUPNUM should not return zero when settlement is in the last period - [Issue #1020](https://github.com/PHPOffice/PhpSpreadsheet/issues/1020) and [PR #1021](https://github.com/PHPOffice/PhpSpreadsheet/pull/1021) ## [1.8.2] - 2019-07-08 diff --git a/src/PhpSpreadsheet/Style/Color.php b/src/PhpSpreadsheet/Style/Color.php index 8a1812d2..0a2209e8 100644 --- a/src/PhpSpreadsheet/Style/Color.php +++ b/src/PhpSpreadsheet/Style/Color.php @@ -6,6 +6,17 @@ use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; class Color extends Supervisor { + const NAMED_COLORS = [ + 'Black', + 'White', + 'Red', + 'Green', + 'Blue', + 'Yellow', + 'Magenta', + 'Cyan', + ]; + // Colors const COLOR_BLACK = 'FF000000'; const COLOR_WHITE = 'FFFFFFFF'; diff --git a/src/PhpSpreadsheet/Style/NumberFormat.php b/src/PhpSpreadsheet/Style/NumberFormat.php index 37442249..255f06db 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat.php +++ b/src/PhpSpreadsheet/Style/NumberFormat.php @@ -660,7 +660,9 @@ class NumberFormat extends Supervisor // Save format with color information for later use below $formatColor = $format; - + // Strip colour information + $color_regex = '/\[(' . implode('|', Color::NAMED_COLORS) . ')\]/'; + $format = preg_replace($color_regex, '', $format); // Let's begin inspecting the format and converting the value to a formatted string // Check for date/time characters (not inside quotes) @@ -668,10 +670,9 @@ class NumberFormat extends Supervisor // datetime format self::formatAsDate($value, $format); } else { - // Strip color information - $color_regex = '/^\\[[a-zA-Z]+\\]/'; - $format = preg_replace($color_regex, '', $format); - if (preg_match('/%$/', $format)) { + if (substr($format, 0, 1) === '"' && substr($format, -1, 1) === '"') { + $value = substr($format, 1, -1); + } elseif (preg_match('/%$/', $format)) { // % number format self::formatAsPercentage($value, $format); } else { diff --git a/tests/data/Style/NumberFormat.php b/tests/data/Style/NumberFormat.php index 69590b94..12e71a4d 100644 --- a/tests/data/Style/NumberFormat.php +++ b/tests/data/Style/NumberFormat.php @@ -33,6 +33,11 @@ return [ 12, '#.0#', ], + [ + '-70', + -70, + '#,##0;[Red]-#,##0' + ], [ '0.1', 0.10000000000000001, @@ -114,6 +119,12 @@ return [ 12200000, '0.0,,', ], + // Percentage + [ + '12%', + 0.12, + '0%', + ], [ '8%', 0.080000000000000002, @@ -221,4 +232,35 @@ return [ 13.0316, '_("€"* #,##0.00_);_("€"* \(#,##0.00\);_("€"* "-"??_);_(@_)', ], + // Named colours + // Simple color + [ + '12345', + 12345, + '[Green]General', + ], + // Multiple colors + [ + '12345', + 12345, + '[Blue]0;[Red]0', + ], + // Multiple colors + [ + 'Positive', + 12, + '[Green]"Positive";[Red]"Negative";[Blue]"Zero"', + ], + // Multiple colors + [ + 'Zero', + 0, + '[Green]"Positive";[Red]"Negative";[Blue]"Zero"', + ], + // Multiple colors + [ + 'Negative', + -2, + '[Green]"Positive";[Red]"Negative";[Blue]"Zero"', + ], ]; diff --git a/tests/data/Style/NumberFormatDates.php b/tests/data/Style/NumberFormatDates.php index dea04de7..9e476042 100644 --- a/tests/data/Style/NumberFormatDates.php +++ b/tests/data/Style/NumberFormatDates.php @@ -77,22 +77,4 @@ return [ 1.1354166666667, '[h]:mm', ], - // Percentage - [ - '12%', - 0.12, - '0%', - ], - // Simple color - [ - '12345', - 12345, - '[Green]General', - ], - // Multiple colors - [ - '12345', - 12345, - '[Blue]0;[Red]0', - ], ];