Skip to content

Commit

Permalink
Coerce Bool to Int for Unary Operation on Arrays
Browse files Browse the repository at this point in the history
Fix PHPOffice#3389. It seems some functionality was left behind when JAMA was eliminated (PR PHPOffice#3260). In particular, it is apparently a known trick to use double negation on boolean values as arguments to functions like SUMPRODUCT.
  • Loading branch information
oleibman committed Feb 22, 2023
1 parent 33eefe7 commit 77ef170
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4944,7 +4944,7 @@ private function processTokenStack($tokens, $cellID = null, ?Cell $cell = null)
[$rows, $columns] = self::checkMatrixOperands($result, $operand2, 0);
for ($row = 0; $row < $rows; ++$row) {
for ($column = 0; $column < $columns; ++$column) {
if (is_numeric($result[$row][$column])) {
if (is_numeric($result[$row][$column]) || is_bool($result[$row][$column])) {
$result[$row][$column] *= $multiplier;
} else {
$result[$row][$column] = Information\ExcelError::VALUE();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,28 @@ public function providerSUMPRODUCT(): array
{
return require 'tests/data/Calculation/MathTrig/SUMPRODUCT.php';
}

public function testBoolsAsInt(): void
{
// issue 3389 not handling unary minus with boolean value
$sheet = $this->getSheet();
$sheet->fromArray(
[
['Range 1', 'Range 2', null, 'Blue matches', 'Red matches'],
[0, 'Red', null, '=SUMPRODUCT(--(B3:B10=1), --(C3:C10="BLUE"))', '=SUMPRODUCT(--(B3:B10=1), --(C3:C10="RED"))'],
[1, 'Blue'],
[0, 'Blue'],
[1, 'Red'],
[1, 'Blue'],
[0, 'Blue'],
[1, 'Red'],
[1, 'Blue'],
],
null, // null value
'B2', // start cell
true // strict null comparison
);
self::assertSame(3, $sheet->getCell('E3')->getCalculatedValue());
self::assertSame(2, $sheet->getCell('F3')->getCalculatedValue());
}
}

0 comments on commit 77ef170

Please sign in to comment.