From e3fb160f5ff9daba1f0127557665a47455ef7c03 Mon Sep 17 00:00:00 2001 From: Bill Blume Date: Fri, 1 Jun 2018 19:34:44 -0700 Subject: [PATCH] Fixed parsing of conditionals in COUNTIF functions Conditional operators in the selection parameter of COUNTIF functions were not being parsed properly, causing evaluations of formulae with such functions to sometimes fail. Fixes #526 Closes #528 --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Calculation/Functions.php | 2 +- .../Calculation/FunctionsTest.php | 16 +++++++++ .../Calculation/Functions/IF_CONDITION.php | 36 +++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 tests/data/Calculation/Functions/IF_CONDITION.php diff --git a/CHANGELOG.md b/CHANGELOG.md index c22d5bca..afc5716d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Xlsx reader do not read rows and columns filtered out in readFilter at all - [#370](https://github.com/PHPOffice/PhpSpreadsheet/issues/370) - Make newer Excel versions properly recalculate formulas on document open - [#456](https://github.com/PHPOffice/PhpSpreadsheet/issues/456) - `Coordinate::extractAllCellReferencesInRange()` throws an exception for an invalid range – [#519](https://github.com/PHPOffice/PhpSpreadsheet/issues/519) +- Fixed parsing of conditionals in COUNTIF functions - [#526](https://github.com/PHPOffice/PhpSpreadsheet/issues/526) ## [1.2.1] - 2018-04-10 diff --git a/src/PhpSpreadsheet/Calculation/Functions.php b/src/PhpSpreadsheet/Calculation/Functions.php index b6389a8d..bf11ceaf 100644 --- a/src/PhpSpreadsheet/Calculation/Functions.php +++ b/src/PhpSpreadsheet/Calculation/Functions.php @@ -277,7 +277,7 @@ class Functions return '=' . $condition; } - preg_match('/([<>=]+)(.*)/', $condition, $matches); + preg_match('/(=|<[>=]?|>=?)(.*)/', $condition, $matches); list(, $operator, $operand) = $matches; if (!is_numeric($operand)) { diff --git a/tests/PhpSpreadsheetTests/Calculation/FunctionsTest.php b/tests/PhpSpreadsheetTests/Calculation/FunctionsTest.php index ed85e80c..1255d910 100644 --- a/tests/PhpSpreadsheetTests/Calculation/FunctionsTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/FunctionsTest.php @@ -307,4 +307,20 @@ class FunctionsTest extends TestCase { return require 'data/Calculation/Functions/ISFORMULA.php'; } + + /** + * @dataProvider providerIfCondition + * + * @param mixed $expectedResult + */ + public function testIfCondition($expectedResult, ...$args) + { + $result = Functions::ifCondition(...$args); + self::assertEquals($expectedResult, $result); + } + + public function providerIfCondition() + { + return require 'data/Calculation/Functions/IF_CONDITION.php'; + } } diff --git a/tests/data/Calculation/Functions/IF_CONDITION.php b/tests/data/Calculation/Functions/IF_CONDITION.php new file mode 100644 index 00000000..0b510ed9 --- /dev/null +++ b/tests/data/Calculation/Functions/IF_CONDITION.php @@ -0,0 +1,36 @@ +"A"', + '>A', + ], + [ + '<="A"', + '<=A', + ], + [ + '>"A"', + '>A', + ], + [ + '>="A"', + '>=A', + ], + [ + '<>"A"', + '<>A', + ], + [ + '<""< PLEASE SELECT >"', + '<>< Please Select >', + ], +];