mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-03 22:19:19 +00:00
ab420f4499
* Coerce Bool to Int for Unary Operation on Arrays Fix #3389. It seems some functionality was left behind when JAMA was eliminated (PR #3260). In particular, it is apparently a known trick to use double negation on boolean values as arguments to functions like SUMPRODUCT. * Fix 3396 Treat booleans in arrays as int for mathematical operators as well. * Edge Case When array operand was neither numeric nor boolean, PhpSpreadsheet had always been evaluating the operand as #NUM!. It will now propagate an error string like #DIV/0!, and treat non-error strings as #VALUE!, consistent with Excel.
66 lines
2.1 KiB
PHP
66 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
|
|
|
class SumProductTest extends AllSetupTeardown
|
|
{
|
|
/**
|
|
* @dataProvider providerSUMPRODUCT
|
|
*
|
|
* @param mixed $expectedResult
|
|
*/
|
|
public function testSUMPRODUCT($expectedResult, ...$args): void
|
|
{
|
|
$sheet = $this->getSheet();
|
|
$row = 0;
|
|
$arrayArg = '';
|
|
foreach ($args as $arr) {
|
|
$arr2 = Functions::flattenArray($arr);
|
|
$startRow = 0;
|
|
foreach ($arr2 as $arr3) {
|
|
++$row;
|
|
if (!$startRow) {
|
|
$startRow = $row;
|
|
}
|
|
$sheet->getCell("A$row")->setValue($arr3);
|
|
}
|
|
$arrayArg .= "A$startRow:A$row,";
|
|
}
|
|
$arrayArg = substr($arrayArg, 0, -1); // strip trailing comma
|
|
$sheet->getCell('B1')->setValue("=SUMPRODUCT($arrayArg)");
|
|
$result = $sheet->getCell('B1')->getCalculatedValue();
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
|
|
}
|
|
|
|
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());
|
|
}
|
|
}
|