Skip to content

Commit

Permalink
Fixed parsing of conditionals in COUNTIF functions
Browse files Browse the repository at this point in the history
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 PHPOffice#526
Closes PHPOffice#528
  • Loading branch information
billblume authored and Frederic Delaunay committed Oct 29, 2018
1 parent e248cb1 commit 9f514a4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public static function ifCondition($condition)

return '=' . $condition;
}
preg_match('/([<>=]+)(.*)/', $condition, $matches);
preg_match('/(=|<[>=]?|>=?)(.*)/', $condition, $matches);
list(, $operator, $operand) = $matches;

if (!is_numeric($operand)) {
Expand Down
16 changes: 16 additions & 0 deletions tests/PhpSpreadsheetTests/Calculation/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,20 @@ public function providerIsFormula()
{
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';
}
}
36 changes: 36 additions & 0 deletions tests/data/Calculation/Functions/IF_CONDITION.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

return [
[
'<"A"',
'<A',
],
[
'>"A"',
'>A',
],
[
'<="A"',
'<=A',
],
[
'>"A"',
'>A',
],
[
'>="A"',
'>=A',
],
[
'<>"A"',
'<>A',
],
[
'<"<A"',
'<<A',
],
[
'<>"< PLEASE SELECT >"',
'<>< Please Select >',
],
];

0 comments on commit 9f514a4

Please sign in to comment.