Skip to content

Commit

Permalink
Fixed parsing of conditionals in COUNTIF functions
Browse files Browse the repository at this point in the history
Fix for issue PHPOffice#526.  Conditional operators
in the selection parameter of COUNTIF functions were not being
parsed properly, causing evaluations of formulae with such
functions to sometimes fail.
  • Loading branch information
billblume committed Jun 2, 2018
1 parent 931aea9 commit 08e832c
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 @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- `Helper\Html` support UTF-8 HTML input - [#444](https://github.com/PHPOffice/PhpSpreadsheet/issues/444)
- Xlsx loaded an extra empty comment for each real comment - [#375](https://github.com/PHPOffice/PhpSpreadsheet/issues/375)
- Xlsx reader do not read rows and columns filtered out in readFilter at all - [#370](https://github.com/PHPOffice/PhpSpreadsheet/issues/370)
- 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 08e832c

Please sign in to comment.