From 4d7bc6b627b75a86a66ef22d44734c377653170f Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Tue, 26 Nov 2024 11:59:26 -0800 Subject: [PATCH] A Little Tinkering We want special processing for quote marks only when they are part of the format, not part of the value. --- .../Style/NumberFormat/Formatter.php | 16 ++++--- tests/data/Style/NumberFormat.php | 45 ++++++++++++++----- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php b/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php index 454fa394da..886cb688ef 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php +++ b/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php @@ -124,13 +124,15 @@ public static function toFormattedString($value, string $format, ?array $callBac return $value ? Calculation::getTRUE() : Calculation::getFALSE(); } // For now we do not treat strings in sections, although section 4 of a format code affects strings - // Process a single block format code containing @ for text substitution, but ignore if the whole format is @ - if ( - $format != '@' - && preg_match(self::SECTION_SPLIT, $format) === 0 - && preg_match(self::SYMBOL_AT, $format) === 1 - ) { - return str_replace('"', '', preg_replace(self::SYMBOL_AT, $value, $format) ?? ''); + // Process a single block format code containing @ for text substitution + if (preg_match(self::SECTION_SPLIT, $format) === 0 && preg_match(self::SYMBOL_AT, $format) === 1) { + if (!str_contains($format, '"')) { + return str_replace('@', $value, $format); + } + //escape any dollar signs on the string, so they are not replaced with an empty value + $value = str_replace('$', '\\$', (string) $value); + + return str_replace('"', '', preg_replace(self::SYMBOL_AT, $value, $format) ?? $value); } // If we have a text value, return it "as is" diff --git a/tests/data/Style/NumberFormat.php b/tests/data/Style/NumberFormat.php index 0f3fde02c3..d666c15737 100644 --- a/tests/data/Style/NumberFormat.php +++ b/tests/data/Style/NumberFormat.php @@ -416,16 +416,6 @@ 'The Boathouse', '"Meet me @ "@" @ 16:30"', ], - [ - '"', - '"', - '@', - ], - [ - '$100', - '$100', - '@', - ], // Named colours // Simple color [ @@ -1692,4 +1682,39 @@ '#,##0.00;;"---"', ], 'issue 4124' => ['1 HUF', 1, '#,##0_-[$HUF]'], + 'issue 4242-0' => [ + 'General $200 - 200', // expected result + 'General $200 - 200', // cell contents + NumberFormat::FORMAT_GENERAL, // cell style + ], + 'issue 4242-1' => [ + 'Text $200 - 200', + 'Text $200 - 200', + NumberFormat::FORMAT_TEXT, + ], + 'issue 4242-2' => [ + '"Hello" she said and "Hello" I replied', + '"Hello" she said and "Hello" I replied', + NumberFormat::FORMAT_TEXT, + ], + 'issue 4242-3' => [ + 'Text: $200 - 200', + '$200 - 200', + '"Text: "' . NumberFormat::FORMAT_TEXT, + ], + 'issue 4242-4' => [ + '"', + '"', + '@', + ], + 'issue 4242-5' => [ + '$100', + '$100', + '@', + ], + 'issue 4242-6' => [ + 'xy xy xy', + 'xy', + '@ @ @', + ], ];