Skip to content

Commit

Permalink
A Little Tinkering
Browse files Browse the repository at this point in the history
We want special processing for quote marks only when they are part of the format, not part of the value.
  • Loading branch information
oleibman committed Nov 26, 2024
1 parent 3af8f4e commit 4d7bc6b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
16 changes: 9 additions & 7 deletions src/PhpSpreadsheet/Style/NumberFormat/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
45 changes: 35 additions & 10 deletions tests/data/Style/NumberFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,16 +416,6 @@
'The Boathouse',
'"Meet me @ "@" @ 16:30"',
],
[
'"',
'"',
'@',
],
[
'$100',
'$100',
'@',
],
// Named colours
// Simple color
[
Expand Down Expand Up @@ -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',
'@ @ @',
],
];

0 comments on commit 4d7bc6b

Please sign in to comment.