Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Substitute a literal dot inside quotes within number format masks to prevent it being mistaken for a decimal separator #1830

Merged
merged 2 commits into from Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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