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
This commit is contained in:
Mark Baker 2019-07-14 19:36:34 +02:00 committed by GitHub
parent a91acec5d9
commit ab1c6e53b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 24 deletions

View File

@ -15,7 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).
### Fixed ### 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) - 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 ## [1.8.2] - 2019-07-08

View File

@ -6,6 +6,17 @@ use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
class Color extends Supervisor class Color extends Supervisor
{ {
const NAMED_COLORS = [
'Black',
'White',
'Red',
'Green',
'Blue',
'Yellow',
'Magenta',
'Cyan',
];
// Colors // Colors
const COLOR_BLACK = 'FF000000'; const COLOR_BLACK = 'FF000000';
const COLOR_WHITE = 'FFFFFFFF'; const COLOR_WHITE = 'FFFFFFFF';

View File

@ -660,7 +660,9 @@ class NumberFormat extends Supervisor
// Save format with color information for later use below // Save format with color information for later use below
$formatColor = $format; $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 // Let's begin inspecting the format and converting the value to a formatted string
// Check for date/time characters (not inside quotes) // Check for date/time characters (not inside quotes)
@ -668,10 +670,9 @@ class NumberFormat extends Supervisor
// datetime format // datetime format
self::formatAsDate($value, $format); self::formatAsDate($value, $format);
} else { } else {
// Strip color information if (substr($format, 0, 1) === '"' && substr($format, -1, 1) === '"') {
$color_regex = '/^\\[[a-zA-Z]+\\]/'; $value = substr($format, 1, -1);
$format = preg_replace($color_regex, '', $format); } elseif (preg_match('/%$/', $format)) {
if (preg_match('/%$/', $format)) {
// % number format // % number format
self::formatAsPercentage($value, $format); self::formatAsPercentage($value, $format);
} else { } else {

View File

@ -33,6 +33,11 @@ return [
12, 12,
'#.0#', '#.0#',
], ],
[
'-70',
-70,
'#,##0;[Red]-#,##0'
],
[ [
'0.1', '0.1',
0.10000000000000001, 0.10000000000000001,
@ -114,6 +119,12 @@ return [
12200000, 12200000,
'0.0,,', '0.0,,',
], ],
// Percentage
[
'12%',
0.12,
'0%',
],
[ [
'8%', '8%',
0.080000000000000002, 0.080000000000000002,
@ -221,4 +232,35 @@ return [
13.0316, 13.0316,
'_("€"* #,##0.00_);_("€"* \(#,##0.00\);_("€"* "-"??_);_(@_)', '_("€"* #,##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"',
],
]; ];

View File

@ -77,22 +77,4 @@ return [
1.1354166666667, 1.1354166666667,
'[h]:mm', '[h]:mm',
], ],
// Percentage
[
'12%',
0.12,
'0%',
],
// Simple color
[
'12345',
12345,
'[Green]General',
],
// Multiple colors
[
'12345',
12345,
'[Blue]0;[Red]0',
],
]; ];