Skip to content

Commit 957cb62

Browse files
authored
TextData Coverage and Minor Bug Fixes (#1744)
This had been intended to get 100% coverage for TextData functions, and it does that. However, some minor bugs requiring source changes arose during testing. - the Excel CHAR function restricts its argument to 1-255. PhpSpreadsheet CHARACTER had been allowing 0+. Also, there is no need to test if iconv exists, since it is part of Composer requirements. - The DOLLAR function had been returning NUM for invalid arguments. Excel returns VALUE. Also, negative amounts were not being handled correctly. - The FIXEDFORMAT function had been returning NUM for invalid arguments. Excel FIXED returns VALUE.
1 parent 78774d6 commit 957cb62

File tree

5 files changed

+81
-17
lines changed

5 files changed

+81
-17
lines changed

src/PhpSpreadsheet/Calculation/TextData.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ public static function CHARACTER($character)
2727
{
2828
$character = Functions::flattenSingleValue($character);
2929

30-
if ((!is_numeric($character)) || ($character < 0)) {
30+
if (!is_numeric($character)) {
3131
return Functions::VALUE();
3232
}
33-
34-
if (function_exists('iconv')) {
35-
return iconv('UCS-4LE', 'UTF-8', pack('V', $character));
33+
$character = (int) $character;
34+
if ($character < 1 || $character > 255) {
35+
return Functions::VALUE();
3636
}
3737

38-
return mb_convert_encoding('&#' . (int) $character . ';', 'UTF-8', 'HTML-ENTITIES');
38+
return iconv('UCS-4LE', 'UTF-8', pack('V', $character));
3939
}
4040

4141
/**
@@ -160,7 +160,7 @@ public static function DOLLAR($value = 0, $decimals = 2)
160160

161161
// Validate parameters
162162
if (!is_numeric($value) || !is_numeric($decimals)) {
163-
return Functions::NAN();
163+
return Functions::VALUE();
164164
}
165165
$decimals = floor($decimals);
166166

@@ -174,6 +174,7 @@ public static function DOLLAR($value = 0, $decimals = 2)
174174
}
175175
$value = MathTrig::MROUND($value, $round);
176176
}
177+
$mask = "$mask;($mask)";
177178

178179
return NumberFormat::toFormattedString($value, $mask);
179180
}
@@ -265,7 +266,7 @@ public static function FIXEDFORMAT($value, $decimals = 2, $no_commas = false)
265266

266267
// Validate parameters
267268
if (!is_numeric($value) || !is_numeric($decimals)) {
268-
return Functions::NAN();
269+
return Functions::VALUE();
269270
}
270271
$decimals = (int) floor($decimals);
271272

tests/data/Calculation/TextData/CHAR.php

+22-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
'#VALUE!',
1010
-5,
1111
],
12+
[
13+
'#VALUE!',
14+
0,
15+
],
1216
[
1317
'A',
1418
65,
@@ -22,27 +26,39 @@
2226
126,
2327
],
2428
[
25-
'',
29+
'Á',
30+
193,
31+
],
32+
[
33+
'ÿ',
34+
255,
35+
],
36+
[
37+
'#VALUE!',
38+
256,
39+
],
40+
[
41+
'#VALUE!', // '⽇',
2642
12103,
2743
],
2844
[
29-
'œ',
45+
'#VALUE!', // 'œ',
3046
0x153,
3147
],
3248
[
33-
'ƒ',
49+
'#VALUE!', // 'ƒ',
3450
0x192,
3551
],
3652
[
37-
'',
53+
'#VALUE!', // '℅',
3854
0x2105,
3955
],
4056
[
41-
'',
57+
'#VALUE!', // '∑',
4258
0x2211,
4359
],
4460
[
45-
'',
61+
'#VALUE!', // '†',
4662
0x2020,
4763
],
4864
];

tests/data/Calculation/TextData/DOLLAR.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,20 @@
66
123.456,
77
2,
88
],
9+
[
10+
'$123.46',
11+
123.456,
12+
],
913
[
1014
'$123.32',
1115
123.321,
1216
2,
1317
],
18+
[
19+
'($123.32)',
20+
-123.321,
21+
2,
22+
],
1423
[
1524
'$1,235,000',
1625
1234567,
@@ -22,12 +31,17 @@
2231
-5,
2332
],
2433
[
25-
'#NUM!',
34+
'($1,200,000)',
35+
-1234567,
36+
-5,
37+
],
38+
[
39+
'#VALUE!',
2640
'ABC',
2741
2,
2842
],
2943
[
30-
'#NUM!',
44+
'#VALUE!',
3145
123.456,
3246
'ABC',
3347
],

tests/data/Calculation/TextData/FIXED.php

+30-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,41 @@
2020
true,
2121
],
2222
[
23-
'#NUM!',
23+
'-123456.79',
24+
-123456.789,
25+
2,
26+
true,
27+
],
28+
[
29+
'123500',
30+
123456.789,
31+
-2,
32+
true,
33+
],
34+
[
35+
'123,500',
36+
123456.789,
37+
-2,
38+
],
39+
[
40+
'-123500',
41+
-123456.789,
42+
-2,
43+
true,
44+
],
45+
[
46+
'-123,500',
47+
-123456.789,
48+
-2,
49+
],
50+
[
51+
'#VALUE!',
2452
'ABC',
2553
2,
2654
null,
2755
],
2856
[
29-
'#NUM!',
57+
'#VALUE!',
3058
123.456,
3159
'ABC',
3260
null,

tests/data/Calculation/TextData/SEARCH.php

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
'Mark Baker',
5555
2,
5656
],
57+
[
58+
1,
59+
'',
60+
'Mark Baker',
61+
],
5762
[
5863
'#VALUE!',
5964
'BITE',

0 commit comments

Comments
 (0)