Skip to content

Commit 160ae59

Browse files
author
Mark Baker
authored
Resolve problem where underscore placeholder in a number format masks (#2038)
* Resolve problem where underscore placeholder in a number format mask was being replaced, but leaving the sizing character as part of the mask
1 parent 475874b commit 160ae59

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

src/PhpSpreadsheet/Style/NumberFormat/Formatter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ function ($matches) {
130130

131131
// In Excel formats, "_" is used to add spacing,
132132
// The following character indicates the size of the spacing, which we can't do in HTML, so we just use a standard space
133-
$format = preg_replace('/_/ui', ' ', $format);
133+
$format = preg_replace('/_.?/ui', ' ', $format);
134134

135135
// Let's begin inspecting the format and converting the value to a formatted string
136136

src/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
44

55
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
6+
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
67

78
class NumberFormatter
89
{
@@ -145,7 +146,6 @@ public static function format($value, $format): string
145146
$format = preg_replace('/0,+/', '0', $format);
146147
$format = preg_replace('/#,+/', '#', $format);
147148
}
148-
149149
if (preg_match('/#?.*\?\/\?/', $format, $m)) {
150150
if ($value != (int) $value) {
151151
$value = FractionFormatter::format($value, $format);
@@ -163,7 +163,12 @@ public static function format($value, $format): string
163163
$n = '/\\[[^\\]]+\\]/';
164164
$m = preg_replace($n, '', $format);
165165
if (preg_match(self::NUMBER_REGEX, $m, $matches)) {
166+
// There are placeholders for digits, so inject digits from the value into the mask
166167
$value = self::formatStraightNumericValue($value, $format, $matches, $useThousands);
168+
} elseif ($format !== NumberFormat::FORMAT_GENERAL) {
169+
// Yes, I know that this is basically just a hack;
170+
// if there's no placeholders for digits, just return the format mask "as is"
171+
$value = str_replace('?', '', $format ?? '');
167172
}
168173
}
169174

tests/data/Style/NumberFormat.php

+25-5
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
'Spacing Character' => [
132132
'826.00 €',
133133
826,
134-
'#,##0.00 _',
134+
'#,##0.00 __',
135135
],
136136
[
137137
'5.68',
@@ -263,7 +263,7 @@
263263
[
264264
'( 12.30% )',
265265
-0.123,
266-
'_0.00%_;( 0.00% )',
266+
'_(0.00%_;( 0.00% )',
267267
],
268268
// Fraction
269269
[
@@ -374,15 +374,35 @@
374374
'[$-1010409]#,##0.00;-#,##0.00',
375375
],
376376
[
377-
' ($ 23.06 )',
377+
' $ 23.06 ',
378378
23.0597,
379379
'_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)',
380380
],
381381
[
382-
' (€ 13.03 )',
383-
13.0316,
382+
' € (13.03)',
383+
-13.0316,
384384
'_("€"* #,##0.00_);_("€"* \(#,##0.00\);_("€"* "-"??_);_(@_)',
385385
],
386+
[
387+
' € 11.70 ',
388+
11.7,
389+
'_-€* #,##0.00_-;"-€"* #,##0.00_-;_-€* -??_-;_-@_-',
390+
],
391+
[
392+
'-€ 12.14 ',
393+
-12.14,
394+
'_-€* #,##0.00_-;"-€"* #,##0.00_-;_-€* -??_-;_-@_-',
395+
],
396+
[
397+
' € - ',
398+
0,
399+
'_-€* #,##0.00_-;"-€"* #,##0.00_-;_-€* -??_-;_-@_-',
400+
],
401+
[
402+
'test',
403+
'test',
404+
'_-€* #,##0.00_-;"-€"* #,##0.00_-;_-€* -??_-;_-@_-',
405+
],
386406
// Named colours
387407
// Simple color
388408
[

0 commit comments

Comments
 (0)