Skip to content
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

Countif strict comparison #1078

Merged
merged 4 commits into from
Jul 14, 2019
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 @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Fixed

- Stricter-typed comparison testing in COUNTIF() and COUNTIFS() evaluation [Issue #1046](https://github.com/PHPOffice/PhpSpreadsheet/issues/1046)
- COUPNUM should not return zero when settlement is in the last period - [Issue #1020](https://github.com/PHPOffice/PhpSpreadsheet/issues/1020) and [PR #1021](https://github.com/PHPOffice/PhpSpreadsheet/pull/1021)

## [1.8.2] - 2019-07-08
Expand Down
4 changes: 3 additions & 1 deletion src/PhpSpreadsheet/Calculation/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ public static function ifCondition($condition)
preg_match('/(=|<[>=]?|>=?)(.*)/', $condition, $matches);
list(, $operator, $operand) = $matches;

if (!is_numeric($operand)) {
if (is_numeric(trim($operand, '"'))) {
$operand = trim($operand, '"');
} elseif (!is_numeric($operand)) {
$operand = str_replace('"', '""', $operand);
$operand = Calculation::wrapResult(strtoupper($operand));
}
Expand Down
16 changes: 16 additions & 0 deletions src/PhpSpreadsheet/Calculation/Statistical.php
Original file line number Diff line number Diff line change
Expand Up @@ -1119,10 +1119,16 @@ public static function COUNTIF($aArgs, $condition)

$aArgs = Functions::flattenArray($aArgs);
$condition = Functions::ifCondition($condition);
$conditionIsNumeric = strpos($condition, '"') === false;
// Loop through arguments
foreach ($aArgs as $arg) {
if (!is_numeric($arg)) {
if ($conditionIsNumeric) {
continue;
}
$arg = Calculation::wrapResult(strtoupper($arg));
} elseif (!$conditionIsNumeric) {
continue;
}
$testCondition = '=' . $arg . $condition;
if (Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
Expand Down Expand Up @@ -1172,11 +1178,21 @@ public static function COUNTIFS(...$args)
$valid = true;

foreach ($conditions as $cidx => $condition) {
$conditionIsNumeric = strpos($condition, '"') === false;
$arg = $aArgsArray[$cidx][$index];

// Loop through arguments
if (!is_numeric($arg)) {
if ($conditionIsNumeric) {
$valid = false;

break; // if false found, don't need to check other conditions
}
$arg = Calculation::wrapResult(strtoupper($arg));
} elseif (!$conditionIsNumeric) {
$valid = false;

break; // if false found, don't need to check other conditions
}
$testCondition = '=' . $arg . $condition;
if (!Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
Expand Down
16 changes: 16 additions & 0 deletions tests/PhpSpreadsheetTests/Calculation/StatisticalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ public function setUp()
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}

/**
* @dataProvider providerCOUNTIF
*
* @param mixed $expectedResult
*/
public function testCOUNTIF($expectedResult, ...$args)
{
$result = Statistical::COUNTIF(...$args);
self::assertEquals($expectedResult, $result, '', 1E-12);
}

public function providerCOUNTIF()
{
return require 'data/Calculation/Statistical/COUNTIF.php';
}

/**
* @dataProvider providerCOUNTIFS
*
Expand Down
34 changes: 34 additions & 0 deletions tests/data/Calculation/Statistical/COUNTIF.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

return [
[
2,
['apples', 'oranges', 'peaches', 'apples'],
'apples',
],
[
2,
['ApPlEs', 'oranges', 'peaches', 'APPles'],
'aPpLeS',
],
[
2,
[32, 54, 75, 86],
'>55',
],
[
3,
[32, 54, 75, 86],
'<=75',
],
[
2,
[6, 3, 4, 'X', ''],
'<=4',
],
[
2,
[6, 3, 4, 'X', ''],
'<="4"',
],
];
30 changes: 11 additions & 19 deletions tests/data/Calculation/Statistical/COUNTIFS.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,22 @@
return [
[
2,
[
['Y'],
['Y'],
['N'],
],
['Y', 'Y', 'N'],
'=Y',
],
[
3,
[
['A'],
['B'],
['C'],
['B'],
['B'],
],
['A', 'B', 'C', 'B', 'B'],
'=B',
[
['C'],
['B'],
['A'],
['B'],
['B'],
],
],
[
3,
['C', 'B', 'A', 'B', 'B'],
'=B',
],
[
2,
[1, 2, 3, 'B', '', false],
'<=2',
],
];