Skip to content

Commit

Permalink
Resolution for [#Issue 1972](#1972) (#1978)
Browse files Browse the repository at this point in the history
* Resolution for [#Issue 1972](#1972) where format masks with a leading and trailing quote were always treated as literal strings, even when they masks containing quoted characters.

Also resolves issue with colour name case-sensitivity
  • Loading branch information
Mark Baker authored Apr 1, 2021
1 parent 17af132 commit 0c403ef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Fixed

- Fixed issue with quoted strings in number format mask rendered with toFormattedString() [Issue 1972#](https://github.com/PHPOffice/PhpSpreadsheet/issues/1972) [PR #1978](https://github.com/PHPOffice/PhpSpreadsheet/pull/1978)
- Fixed issue with percentage formats in number format mask rendered with toFormattedString() [Issue 1929#](https://github.com/PHPOffice/PhpSpreadsheet/issues/1929) [PR #1928](https://github.com/PHPOffice/PhpSpreadsheet/pull/1928)
- Fixed issue with _ spacing character in number format mask corrupting output from toFormattedString() [Issue 1924#](https://github.com/PHPOffice/PhpSpreadsheet/issues/1924) [PR #1927](https://github.com/PHPOffice/PhpSpreadsheet/pull/1927)
- Fix for [Issue #1887](https://github.com/PHPOffice/PhpSpreadsheet/issues/1887) - Lose Track of Selected Cells After Save
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Style/NumberFormat/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private static function splitFormat($sections, $value)
// 3 sections: [POSITIVE/TEXT] [NEGATIVE] [ZERO]
// 4 sections: [POSITIVE] [NEGATIVE] [ZERO] [TEXT]
$cnt = count($sections);
$color_regex = '/\\[(' . implode('|', Color::NAMED_COLORS) . ')\\]/';
$color_regex = '/\\[(' . implode('|', Color::NAMED_COLORS) . ')\\]/mui';
$cond_regex = '/\\[(>|>=|<|<=|=|<>)([+-]?\\d+([.]\\d+)?)\\]/';
$colors = ['', '', '', '', ''];
$condops = ['', '', '', '', ''];
Expand Down Expand Up @@ -139,7 +139,7 @@ function ($matches) {
// datetime format
$value = DateFormatter::format($value, $format);
} else {
if (substr($format, 0, 1) === '"' && substr($format, -1, 1) === '"') {
if (substr($format, 0, 1) === '"' && substr($format, -1, 1) === '"' && substr_count($format, '"') === 2) {
$value = substr($format, 1, -1);
} elseif (preg_match('/[0#, ]%/', $format)) {
// % number format
Expand Down
31 changes: 29 additions & 2 deletions tests/data/Style/NumberFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@
12345,
'[Green]General',
],
[
'12345',
12345,
'[GrEeN]General',
],
[
'-70',
-70,
Expand All @@ -404,14 +409,24 @@
[
'12345',
12345,
'[Blue]0;[Red]0',
'[Blue]0;[Red]0-',
],
[
'12345-',
-12345,
'[BLUE]0;[red]0-',
],
[
'12345-',
-12345,
'[blue]0;[RED]0-',
],
// Multiple colors with text substitution
[
'Positive',
12,
'[Green]"Positive";[Red]"Negative";[Blue]"Zero"',
],
// Multiple colors with text substitution
[
'Zero',
0,
Expand All @@ -422,6 +437,7 @@
-2,
'[Green]"Positive";[Red]"Negative";[Blue]"Zero"',
],
// Value break points
[
'<=3500 red',
3500,
Expand All @@ -442,6 +458,17 @@
25,
'[Green][<>25]"<>25 green";[Red]"else red"',
],
// Leading/trailing quotes in mask
[
'$12.34 ',
12.34,
'$#,##0.00_;[RED]"($"#,##0.00")"',
],
[
'($12.34)',
-12.34,
'$#,##0.00_;[RED]"($"#,##0.00")"',
],
[
'pfx. 25.00',
25,
Expand Down

0 comments on commit 0c403ef

Please sign in to comment.