Skip to content

skip non numeric value in SUMIF #618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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],
],
],

];