From db4dac3de9c67d20c0e866997a763fc5bac4ba21 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Tue, 17 May 2022 16:40:14 +0200 Subject: [PATCH] Filter null values in PRODUCT() function, as they don't affect the calculation in any way, and actually require additional code handling if they're present within the calculation itself --- .../Calculation/MathTrig/Operations.php | 26 +++++++++---------- tests/data/Calculation/MathTrig/PRODUCT.php | 16 ++++++++++-- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Operations.php b/src/PhpSpreadsheet/Calculation/MathTrig/Operations.php index 44e13526..f26da389 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Operations.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Operations.php @@ -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; } /** diff --git a/tests/data/Calculation/MathTrig/PRODUCT.php b/tests/data/Calculation/MathTrig/PRODUCT.php index ab500953..657c713c 100644 --- a/tests/data/Calculation/MathTrig/PRODUCT.php +++ b/tests/data/Calculation/MathTrig/PRODUCT.php @@ -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], ];