Skip to content

Commit ae9dd13

Browse files
ScortyPowerKiKi
authored andcommitted
Skip non numeric value in SUMIF
MS Excel skip non numeric values also. PhpSpreadsheet used to fail on string value with: Warning: A non-numeric value encountered. Fixes #618
1 parent 5e5be14 commit ae9dd13

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2424

2525
### Added
2626

27+
- skip non numeric value in SUMIF - [#618](https://github.com/PHPOffice/PhpSpreadsheet/pull/618)
2728
- Add excel function EXACT(value1, value2) support - [#595](https://github.com/PHPOffice/PhpSpreadsheet/pull/595)
2829
- Support workbook view attributes for Xlsx format - [#523](https://github.com/PHPOffice/PhpSpreadsheet/issues/523)
2930
- Read and write hyperlink for drawing image - [#490](https://github.com/PHPOffice/PhpSpreadsheet/pull/490)

src/PhpSpreadsheet/Calculation/MathTrig.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -1222,8 +1222,10 @@ public static function SUMIF($aArgs, $condition, $sumArgs = [])
12221222
}
12231223

12241224
$testCondition = '=' . $arg . $condition;
1225-
if (Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
1226-
// Is it a value within our criteria
1225+
1226+
if (is_numeric($sumArgs[$key]) &&
1227+
Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
1228+
// Is it a value within our criteria and only numeric can be added to the result
12271229
$returnValue += $sumArgs[$key];
12281230
}
12291231
}

tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ public function providerSQRTPI()
560560
public function testSUMIF($expectedResult, ...$args)
561561
{
562562
$result = MathTrig::SUMIF(...$args);
563-
self::assertEquals($expectedResult, $result, null, 1E-12);
563+
self::assertEquals($expectedResult, $result, '', 1E-12);
564564
}
565565

566566
public function providerSUMIF()

tests/data/Calculation/MathTrig/SUMIF.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
['"text with quotes"'],
4141
[''],
4242
],
43-
'>"', // Compare to the single characater " (double quote)
43+
'>"', // Compare to the single character " (double quote)
4444
[
4545
[10],
4646
[100],
@@ -52,10 +52,23 @@
5252
[''],
5353
['anything'],
5454
],
55-
'>"', // Compare to the single characater " (double quote)
55+
'>"', // Compare to the single character " (double quote)
5656
[
5757
[10],
5858
[100],
5959
],
6060
],
61+
[
62+
10,
63+
[
64+
[1],
65+
[2],
66+
],
67+
'<>', // any content
68+
[
69+
['non-numeric value'], // ignored in SUM
70+
[10],
71+
],
72+
],
73+
6174
];

0 commit comments

Comments
 (0)