diff --git a/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php b/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php index b2b0e669..3c8b176f 100644 --- a/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php +++ b/src/PhpSpreadsheet/Calculation/Database/DatabaseAbstract.php @@ -95,7 +95,7 @@ abstract class DatabaseAbstract foreach ($criteria as $key => $criterion) { foreach ($criterion as $field => $value) { $criterionName = $criteriaNames[$field]; - if ($value !== null && $value !== '') { + if ($value !== null) { $condition = self::buildCondition($value, $criterionName); $baseQuery[$key][] = $condition; } @@ -104,12 +104,12 @@ abstract class DatabaseAbstract $rowQuery = array_map( function ($rowValue) { - return (count($rowValue) > 1) ? 'AND(' . implode(',', $rowValue) . ')' : $rowValue[0]; + return (count($rowValue) > 1) ? 'AND(' . implode(',', $rowValue) . ')' : ($rowValue[0] ?? ''); }, $baseQuery ); - return (count($rowQuery) > 1) ? 'OR(' . implode(',', $rowQuery) . ')' : $rowQuery[0]; + return (count($rowQuery) > 1) ? 'OR(' . implode(',', $rowQuery) . ')' : ($rowQuery[0] ?? ''); } private static function buildCondition($criterion, string $criterionName): string diff --git a/src/PhpSpreadsheet/Calculation/Functions.php b/src/PhpSpreadsheet/Calculation/Functions.php index aea2323e..9b56aad4 100644 --- a/src/PhpSpreadsheet/Calculation/Functions.php +++ b/src/PhpSpreadsheet/Calculation/Functions.php @@ -251,7 +251,7 @@ class Functions $condition = self::flattenSingleValue($condition); if ($condition === '') { - $condition = '=""'; + return '=""'; } if (!is_string($condition) || !in_array($condition[0], ['>', '<', '='])) { $condition = self::operandSpecialHandling($condition); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumIfTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumIfTest.php index 39ac9a66..93c2ae32 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumIfTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumIfTest.php @@ -20,8 +20,7 @@ class SumIfTest extends AllSetupTeardown $sheet->fromArray($array1, null, 'A1', true); $maxARow = count($array1); $firstArg = "A1:A$maxARow"; - //$secondArg = is_string($condition) ? "\"$condition\"" : $condition; - $sheet->getCell('B1')->setValue($condition); + $this->setCell('B1', $condition); $secondArg = 'B1'; if (empty($array2)) { $sheet->getCell('D1')->setValue("=SUMIF($firstArg, $secondArg)"); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/AverageIf2Test.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/AverageIf2Test.php new file mode 100644 index 00000000..1899239f --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/AverageIf2Test.php @@ -0,0 +1,40 @@ +getActiveSheet(); + $sheet->fromArray($table, null, 'A1', true); + $sheet->getCell('C1')->setValue('=AVERAGEIF(A:A,"",B:B)'); + $sheet->getCell('C2')->setValue('=AVERAGEIF(A:A,,B:B)'); + $sheet->getCell('C3')->setValue('=AVERAGEIF(A:A,"n",B:B)'); + $sheet->getCell('C4')->setValue('=AVERAGEIF(A:A,"y",B:B)'); + $sheet->getCell('C5')->setValue('=AVERAGEIF(A:A,"x",B:B)'); + + self::assertEqualsWithDelta(5.75, $sheet->getCell('C1')->getCalculatedValue(), 1E-12); + self::assertEquals('#DIV/0!', $sheet->getCell('C2')->getCalculatedValue()); + self::assertEqualsWithDelta(4.75, $sheet->getCell('C3')->getCalculatedValue(), 1E-12); + self::assertEqualsWithDelta(3, $sheet->getCell('C4')->getCalculatedValue(), 1E-12); + self::assertEquals('#DIV/0!', $sheet->getCell('C5')->getCalculatedValue()); + $spreadsheet->disconnectWorksheets(); + } +} diff --git a/tests/data/Calculation/MathTrig/SUMIF.php b/tests/data/Calculation/MathTrig/SUMIF.php index 0a6cfbdc..a427624f 100644 --- a/tests/data/Calculation/MathTrig/SUMIF.php +++ b/tests/data/Calculation/MathTrig/SUMIF.php @@ -150,4 +150,22 @@ return [ 'North ?', [[36693], [22100], ['=3/0'], [34440], [29889], [50090], [32080], [45500]], ], + [ + 23, + [['n'], [''], ['y'], [''], ['n'], ['n'], ['n'], [''], []], + '', + [[1], [2], [3], [4], [5], [6], [7], [8], [9]], + ], + [ + 0, + [['n'], [''], ['y'], [''], ['n'], ['n'], ['n'], [''], []], + null, + [[1], [2], [3], [4], [5], [6], [7], [8], [9]], + ], + [ + 19, + [['n'], [''], ['y'], [''], ['n'], ['n'], ['n'], [''], []], + 'n', + [[1], [2], [3], [4], [5], [6], [7], [8], [9]], + ], ];