From b068639513bab86bbabc8f00a92cec23708a3ac6 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Mon, 8 Feb 2021 15:06:07 +0100 Subject: [PATCH] Substitute a literal dot inside quotes within number format masks to prevent it being mistaken for a decimal separator (#1830) * Substitute a literal dot inside quotes within number format masks to prevent it being mistaken for a decimal separator --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Style/NumberFormat.php | 10 ++++++++++ tests/data/Style/NumberFormat.php | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 181fb84624..9cd09b13ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). ### Fixed +- Fix problem resulting from literal dot inside quotes in number format masks. [PR #1830](https://github.com/PHPOffice/PhpSpreadsheet/pull/1830) - Resolve Google Sheets Xlsx charts issue. Google Sheets uses oneCellAnchor positioning and does not include *Cache values in the exported Xlsx. [PR #1761](https://github.com/PHPOffice/PhpSpreadsheet/pull/1761) - Fix for Xlsx Chart axis titles mapping to correct X or Y axis label when only one is present. [PR #1760](https://github.com/PHPOffice/PhpSpreadsheet/pull/1760) - Fix For Null Exception on ODS Read of Page Settings. [#1772](https://github.com/PHPOffice/PhpSpreadsheet/issues/1772) diff --git a/src/PhpSpreadsheet/Style/NumberFormat.php b/src/PhpSpreadsheet/Style/NumberFormat.php index 9a0acb6f2e..9a2549bc0a 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat.php +++ b/src/PhpSpreadsheet/Style/NumberFormat.php @@ -833,6 +833,14 @@ public static function toFormattedString($value, $format, $callBack = null) return $value; } + $format = preg_replace_callback( + '/(["])(?:(?=(\\\\?))\\2.)*?\\1/u', + function ($matches) { + return str_replace('.', chr(0x00), $matches[0]); + }, + $format + ); + // Convert any other escaped characters to quoted strings, e.g. (\T to "T") $format = preg_replace('/(\\\(((.)(?!((AM\/PM)|(A\/P))))|([^ ])))(?=(?:[^"]|"[^"]*")*$)/u', '"${2}"', $format); @@ -868,6 +876,8 @@ public static function toFormattedString($value, $format, $callBack = null) $value = $writerInstance->$function($value, $colors); } + $value = str_replace(chr(0x00), '.', $value); + return $value; } diff --git a/tests/data/Style/NumberFormat.php b/tests/data/Style/NumberFormat.php index 5038d6da47..12aec1d328 100644 --- a/tests/data/Style/NumberFormat.php +++ b/tests/data/Style/NumberFormat.php @@ -88,6 +88,26 @@ 12345.678900000001, '#,##0.000', ], + [ + '12.34 kg', + 12.34, + '0.00 "kg"', + ], + [ + 'kg 12.34', + 12.34, + '"kg" 0.00', + ], + [ + '12.34 kg.', + 12.34, + '0.00 "kg."', + ], + [ + 'kg. 12.34', + 12.34, + '"kg." 0.00', + ], [ '£ 12,345.68', 12345.678900000001,