From ae9dd13aa05d000e534284636190e9d065e8d04f Mon Sep 17 00:00:00 2001 From: Scorty Date: Sun, 7 Oct 2018 08:24:23 +0200 Subject: [PATCH] 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 --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Calculation/MathTrig.php | 6 ++++-- .../Calculation/MathTrigTest.php | 2 +- tests/data/Calculation/MathTrig/SUMIF.php | 17 +++++++++++++++-- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f37d67bc..963f01d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index 41fc0de2..e8a0397e 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -1222,8 +1222,10 @@ class MathTrig } $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]; } } diff --git a/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php b/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php index fa497964..11c958fb 100644 --- a/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php @@ -560,7 +560,7 @@ class MathTrigTest extends TestCase 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() diff --git a/tests/data/Calculation/MathTrig/SUMIF.php b/tests/data/Calculation/MathTrig/SUMIF.php index 0b94701a..df542bee 100644 --- a/tests/data/Calculation/MathTrig/SUMIF.php +++ b/tests/data/Calculation/MathTrig/SUMIF.php @@ -40,7 +40,7 @@ return [ ['"text with quotes"'], [''], ], - '>"', // Compare to the single characater " (double quote) + '>"', // Compare to the single character " (double quote) [ [10], [100], @@ -52,10 +52,23 @@ return [ [''], ['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], + ], + ], + ];