Merge pull request #2835 from PHPOffice/Issue-2833_Null-handling-in-PRODUCT()-Function

Filter null values in PRODUCT() function

> It all looks good to me.

ty
This commit is contained in:
Mark Baker 2022-05-17 17:10:32 +02:00 committed by GitHub
commit b8957205e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 16 deletions

View File

@ -102,29 +102,27 @@ class Operations
*/ */
public static function product(...$args) public static function product(...$args)
{ {
$args = array_filter(
Functions::flattenArray($args),
function ($value) {
return $value !== null;
}
);
// Return value // Return value
$returnValue = null; $returnValue = (count($args) === 0) ? 0.0 : 1.0;
// Loop through arguments // Loop through arguments
foreach (Functions::flattenArray($args) as $arg) { foreach ($args as $arg) {
// Is it a numeric value? // Is it a numeric value?
if (is_numeric($arg) || $arg === null) { if (is_numeric($arg)) {
if ($returnValue === null) { $returnValue *= $arg;
$returnValue = $arg;
} else {
$returnValue *= $arg;
}
} else { } else {
return ExcelError::VALUE(); return ExcelError::VALUE();
} }
} }
// Return return (float) $returnValue;
if ($returnValue === null) {
return 0;
}
return $returnValue;
} }
/** /**

View File

@ -49,17 +49,29 @@ return [
-2, -2,
], ],
[ [
0, 31.25,
12.5, 12.5,
null, null,
2.5, 2.5,
], ],
[ [
0, 31.25,
12.5, 12.5,
2.5, 2.5,
null, null,
], ],
[
12.5,
12.5,
null,
null,
],
[
0.0,
null,
null,
null,
],
['#VALUE!', 1, 'y', 3], ['#VALUE!', 1, 'y', 3],
[6, 1, '2', 3], [6, 1, '2', 3],
]; ];