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)
{
$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;
}
/**

View File

@ -49,17 +49,29 @@ return [
-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],
];