Skip to content

Filter null values in PRODUCT() function #2835

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

Merged
merged 2 commits into from
May 17, 2022
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
26 changes: 12 additions & 14 deletions src/PhpSpreadsheet/Calculation/MathTrig/Operations.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,29 +102,27 @@ public static function power($x, $y)
*/
public static function product(...$args)
{
$args = array_filter(
Functions::flattenArray($args),
function ($value) {
return $value !== null;
}
);

// Return value
$returnValue = null;
$returnValue = (count($args) === 0) ? 0.0 : 1.0;

// Loop through arguments
foreach (Functions::flattenArray($args) as $arg) {
foreach ($args as $arg) {
// Is it a numeric value?
if (is_numeric($arg) || $arg === null) {
if ($returnValue === null) {
$returnValue = $arg;
} else {
$returnValue *= $arg;
}
if (is_numeric($arg)) {
$returnValue *= $arg;
} else {
return ExcelError::VALUE();
}
}

// Return
if ($returnValue === null) {
return 0;
}

return $returnValue;
return (float) $returnValue;
}

/**
Expand Down
16 changes: 14 additions & 2 deletions tests/data/Calculation/MathTrig/PRODUCT.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,29 @@
-2,
],
[
0,
31.25,
12.5,
null,
2.5,
],
[
0,
31.25,
12.5,
2.5,
null,
],
[
12.5,
12.5,
null,
null,
],
[
0.0,
null,
null,
null,
],
['#VALUE!', 1, 'y', 3],
[6, 1, '2', 3],
];