Skip to content

Commit

Permalink
Substitute a literal dot inside quotes within number format masks to …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
Mark Baker committed Feb 8, 2021
1 parent 2fac9ee commit b068639
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions src/PhpSpreadsheet/Style/NumberFormat.php
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}

Expand Down
20 changes: 20 additions & 0 deletions tests/data/Style/NumberFormat.php
Expand Up @@ -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,
Expand Down

0 comments on commit b068639

Please sign in to comment.