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
This commit is contained in:
Mark Baker 2021-04-29 14:15:45 +02:00 committed by GitHub
parent 475874bed3
commit 160ae59751
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 7 deletions

View File

@ -130,7 +130,7 @@ class Formatter
// In Excel formats, "_" is used to add spacing, // In Excel formats, "_" is used to add spacing,
// The following character indicates the size of the spacing, which we can't do in HTML, so we just use a standard space // The following character indicates the size of the spacing, which we can't do in HTML, so we just use a standard space
$format = preg_replace('/_/ui', ' ', $format); $format = preg_replace('/_.?/ui', ' ', $format);
// Let's begin inspecting the format and converting the value to a formatted string // Let's begin inspecting the format and converting the value to a formatted string

View File

@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat; namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class NumberFormatter class NumberFormatter
{ {
@ -145,7 +146,6 @@ class NumberFormatter
$format = preg_replace('/0,+/', '0', $format); $format = preg_replace('/0,+/', '0', $format);
$format = preg_replace('/#,+/', '#', $format); $format = preg_replace('/#,+/', '#', $format);
} }
if (preg_match('/#?.*\?\/\?/', $format, $m)) { if (preg_match('/#?.*\?\/\?/', $format, $m)) {
if ($value != (int) $value) { if ($value != (int) $value) {
$value = FractionFormatter::format($value, $format); $value = FractionFormatter::format($value, $format);
@ -163,7 +163,12 @@ class NumberFormatter
$n = '/\\[[^\\]]+\\]/'; $n = '/\\[[^\\]]+\\]/';
$m = preg_replace($n, '', $format); $m = preg_replace($n, '', $format);
if (preg_match(self::NUMBER_REGEX, $m, $matches)) { if (preg_match(self::NUMBER_REGEX, $m, $matches)) {
// There are placeholders for digits, so inject digits from the value into the mask
$value = self::formatStraightNumericValue($value, $format, $matches, $useThousands); $value = self::formatStraightNumericValue($value, $format, $matches, $useThousands);
} elseif ($format !== NumberFormat::FORMAT_GENERAL) {
// Yes, I know that this is basically just a hack;
// if there's no placeholders for digits, just return the format mask "as is"
$value = str_replace('?', '', $format ?? '');
} }
} }

View File

@ -131,7 +131,7 @@ return [
'Spacing Character' => [ 'Spacing Character' => [
'826.00 €', '826.00 €',
826, 826,
'#,##0.00 _€', '#,##0.00 __€',
], ],
[ [
'5.68', '5.68',
@ -263,7 +263,7 @@ return [
[ [
'( 12.30% )', '( 12.30% )',
-0.123, -0.123,
'_0.00%_;( 0.00% )', '_(0.00%_;( 0.00% )',
], ],
// Fraction // Fraction
[ [
@ -374,15 +374,35 @@ return [
'[$-1010409]#,##0.00;-#,##0.00', '[$-1010409]#,##0.00;-#,##0.00',
], ],
[ [
' ($ 23.06 )', ' $ 23.06 ',
23.0597, 23.0597,
'_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)', '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)',
], ],
[ [
' (€ 13.03 )', ' (13.03)',
13.0316, -13.0316,
'_("€"* #,##0.00_);_("€"* \(#,##0.00\);_("€"* "-"??_);_(@_)', '_("€"* #,##0.00_);_("€"* \(#,##0.00\);_("€"* "-"??_);_(@_)',
], ],
[
' € 11.70 ',
11.7,
'_-€* #,##0.00_-;"-€"* #,##0.00_-;_-€* -??_-;_-@_-',
],
[
'-€ 12.14 ',
-12.14,
'_-€* #,##0.00_-;"-€"* #,##0.00_-;_-€* -??_-;_-@_-',
],
[
' € - ',
0,
'_-€* #,##0.00_-;"-€"* #,##0.00_-;_-€* -??_-;_-@_-',
],
[
'test',
'test',
'_-€* #,##0.00_-;"-€"* #,##0.00_-;_-€* -??_-;_-@_-',
],
// Named colours // Named colours
// Simple color // Simple color
[ [