Skip to content

Commit 3eee749

Browse files
committed
Ignore cell formatting when the format is a single @
This commit fix two issues that happened when the a cell was formatted as text * When the cell contains a number prefixed with dollar sign, this number is getting replaced with 0. The replacement happens due to the preg_replace function. * When the cell contains quotes, the quote would be removed.
1 parent fd562af commit 3eee749

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
2525

2626
### Fixed
2727

28-
- Nothing yet.
28+
- Ignore cell formatting when the format is a single @. [Issue #4242](https://github.com/PHPOffice/PhpSpreadsheet/issues/4242) [PR #4243](https://github.com/PHPOffice/PhpSpreadsheet/pull/4243)
2929

3030
## 2024-11-22 - 3.5.0
3131

src/PhpSpreadsheet/Style/NumberFormat/Formatter.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,13 @@ public static function toFormattedString($value, string $format, ?array $callBac
126126
// For now we do not treat strings in sections, although section 4 of a format code affects strings
127127
// Process a single block format code containing @ for text substitution
128128
if (preg_match(self::SECTION_SPLIT, $format) === 0 && preg_match(self::SYMBOL_AT, $format) === 1) {
129-
return str_replace('"', '', preg_replace(self::SYMBOL_AT, (string) $value, $format) ?? '');
129+
if (!str_contains($format, '"')) {
130+
return str_replace('@', $value, $format);
131+
}
132+
//escape any dollar signs on the string, so they are not replaced with an empty value
133+
$value = str_replace('$', '\\$', (string) $value);
134+
135+
return str_replace('"', '', preg_replace(self::SYMBOL_AT, $value, $format) ?? $value);
130136
}
131137

132138
// If we have a text value, return it "as is"

tests/data/Style/NumberFormat.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,4 +1682,24 @@
16821682
'#,##0.00;;"---"',
16831683
],
16841684
'issue 4124' => ['1 HUF', 1, '#,##0_-[$HUF]'],
1685+
'issue 4242-0' => [
1686+
'General $200 - 200', // expected result
1687+
'General $200 - 200', // cell contents
1688+
NumberFormat::FORMAT_GENERAL, // cell style
1689+
],
1690+
'issue 4242-1' => [
1691+
'Text $200 - 200',
1692+
'Text $200 - 200',
1693+
NumberFormat::FORMAT_TEXT,
1694+
],
1695+
'issue 4242-2' => [
1696+
'"Hello" she said and "Hello" I replied',
1697+
'"Hello" she said and "Hello" I replied',
1698+
NumberFormat::FORMAT_TEXT,
1699+
],
1700+
'issue 4242-3' => [
1701+
'Text: $200 - 200',
1702+
'$200 - 200',
1703+
'"Text: "' . NumberFormat::FORMAT_TEXT,
1704+
],
16851705
];

0 commit comments

Comments
 (0)