Skip to content

Commit

Permalink
Skip non numeric value in SUMIF
Browse files Browse the repository at this point in the history
MS Excel skip non numeric values also. PhpSpreadsheet used to fail on string value with: Warning: A non-numeric value encountered.

Fixes  #618
  • Loading branch information
Scorty authored and PowerKiKi committed Oct 7, 2018
1 parent 5e5be14 commit ae9dd13
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

- skip non numeric value in SUMIF - [#618](https://github.com/PHPOffice/PhpSpreadsheet/pull/618)
- Add excel function EXACT(value1, value2) support - [#595](https://github.com/PHPOffice/PhpSpreadsheet/pull/595)
- Support workbook view attributes for Xlsx format - [#523](https://github.com/PHPOffice/PhpSpreadsheet/issues/523)
- Read and write hyperlink for drawing image - [#490](https://github.com/PHPOffice/PhpSpreadsheet/pull/490)
Expand Down
6 changes: 4 additions & 2 deletions src/PhpSpreadsheet/Calculation/MathTrig.php
Original file line number Diff line number Diff line change
Expand Up @@ -1222,8 +1222,10 @@ public static function SUMIF($aArgs, $condition, $sumArgs = [])
}

$testCondition = '=' . $arg . $condition;
if (Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
// Is it a value within our criteria

if (is_numeric($sumArgs[$key]) &&
Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
// Is it a value within our criteria and only numeric can be added to the result
$returnValue += $sumArgs[$key];
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ public function providerSQRTPI()
public function testSUMIF($expectedResult, ...$args)
{
$result = MathTrig::SUMIF(...$args);
self::assertEquals($expectedResult, $result, null, 1E-12);
self::assertEquals($expectedResult, $result, '', 1E-12);
}

public function providerSUMIF()
Expand Down
17 changes: 15 additions & 2 deletions tests/data/Calculation/MathTrig/SUMIF.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
['"text with quotes"'],
[''],
],
'>"', // Compare to the single characater " (double quote)
'>"', // Compare to the single character " (double quote)
[
[10],
[100],
Expand All @@ -52,10 +52,23 @@
[''],
['anything'],
],
'>"', // Compare to the single characater " (double quote)
'>"', // Compare to the single character " (double quote)
[
[10],
[100],
],
],
[
10,
[
[1],
[2],
],
'<>', // any content
[
['non-numeric value'], // ignored in SUM
[10],
],
],

];

0 comments on commit ae9dd13

Please sign in to comment.