From 97b3b21d6164595c3a6880bf55b7a96e0aef8b15 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Sat, 4 Feb 2023 04:17:16 +0100 Subject: [PATCH] Improved handling of @ format mask With some limitations remaining: - Still doesn't work in section 4 - Doesn't work correctly if there is an @ in quotes, not recognised as a literal --- .../Style/NumberFormat/Formatter.php | 4 ++++ tests/data/Style/NumberFormat.php | 21 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php b/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php index 03b002e533..a2650d1cc6 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php +++ b/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php @@ -114,6 +114,10 @@ public static function toFormattedString($value, $format, $callBack = null) } // For now we do not treat strings although section 4 of a format code affects strings if (!is_numeric($value)) { + if (strpos($format, ';') === false && preg_match('/@(?=(?:[^"]*"[^"]*")*[^"]*\Z)/', $format) === 1) { + return str_replace('"', '', preg_replace('/@(?=(?:[^"]*"[^"]*")*[^"]*\Z)/', $value, $format)); + } + return (string) $value; } diff --git a/tests/data/Style/NumberFormat.php b/tests/data/Style/NumberFormat.php index 4bd6b48118..9b43db345a 100644 --- a/tests/data/Style/NumberFormat.php +++ b/tests/data/Style/NumberFormat.php @@ -373,6 +373,27 @@ 'test', '_-€* #,##0.00_-;"-€"* #,##0.00_-;_-€* -??_-;_-@_-', ], + // String masks (ie. @) + [ + 'World', + 'World', + '@', + ], + [ + 'Hello World', + 'World', + 'Hello @', + ], + [ + 'Hello World', + 'World', + '"Hello "@', + ], + [ + 'Meet me @ The Boathouse @ 16:30', + 'The Boathouse', + '"Meet me @ "@" @ 16:30"', + ], // Named colours // Simple color [