| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ErrorValue; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 8 |
|
| 9 |
class Sum |
| 10 |
{ |
| 11 |
/** |
| 12 |
* SUM, ignoring non-numeric non-error strings. This is eventually used by SUMIF. |
| 13 |
* |
| 14 |
* SUM computes the sum of all the values and cells referenced in the argument list. |
| 15 |
* |
| 16 |
* Excel Function: |
| 17 |
* SUM(value1[,value2[, ...]]) |
| 18 |
* |
| 19 |
* @param mixed ...$args Data values |
| 20 |
* @return float|int|string |
| 21 |
*/ |
| 22 |
public static function sumIgnoringStrings(...$args) |
| 23 |
{ |
| 24 |
$returnValue = 0; |
| 25 |
|
| 26 |
// Loop through the arguments |
| 27 |
foreach (Functions::flattenArray($args) as $arg) { |
| 28 |
// Is it a numeric value? |
| 29 |
if (is_numeric($arg)) { |
| 30 |
$returnValue += $arg; |
| 31 |
} elseif (ErrorValue::isError($arg)) { |
| 32 |
/** @var string $arg */ |
| 33 |
return $arg; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
return $returnValue; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* SUM, returning error for non-numeric strings. This is used by Excel SUM function. |
| 42 |
* |
| 43 |
* SUM computes the sum of all the values and cells referenced in the argument list. |
| 44 |
* |
| 45 |
* Excel Function: |
| 46 |
* SUM(value1[,value2[, ...]]) |
| 47 |
* |
| 48 |
* @param mixed ...$args Data values |
| 49 |
* |
| 50 |
* @return array<mixed>|float|int|string |
| 51 |
*/ |
| 52 |
public static function sumErroringStrings(...$args) |
| 53 |
{ |
| 54 |
$returnValue = 0; |
| 55 |
// Loop through the arguments |
| 56 |
$aArgs = Functions::flattenArrayIndexed($args); |
| 57 |
foreach ($aArgs as $k => $arg) { |
| 58 |
// Is it a numeric value? |
| 59 |
if (is_numeric($arg)) { |
| 60 |
$returnValue += $arg; |
| 61 |
} elseif (is_bool($arg)) { |
| 62 |
$returnValue += (int) $arg; |
| 63 |
} elseif (ErrorValue::isError($arg, true)) { |
| 64 |
/** @var string $arg */ |
| 65 |
return $arg; |
| 66 |
} elseif ($arg !== null && !Functions::isCellValue($k)) { |
| 67 |
// ignore non-numerics from cell, but fail as literals (except null) |
| 68 |
return ExcelError::VALUE(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return $returnValue; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* SUMPRODUCT. |
| 77 |
* |
| 78 |
* Excel Function: |
| 79 |
* SUMPRODUCT(value1[,value2[, ...]]) |
| 80 |
* |
| 81 |
* @param mixed ...$args Data values |
| 82 |
* |
| 83 |
* @return float|int|string The result, or a string containing an error |
| 84 |
*/ |
| 85 |
public static function product(...$args) |
| 86 |
{ |
| 87 |
$arrayList = $args; |
| 88 |
|
| 89 |
$wrkArray = Functions::flattenArray(array_shift($arrayList)); |
| 90 |
$wrkCellCount = count($wrkArray); |
| 91 |
|
| 92 |
for ($i = 0; $i < $wrkCellCount; ++$i) { |
| 93 |
if ((!is_numeric($wrkArray[$i])) || (is_string($wrkArray[$i]))) { |
| 94 |
$wrkArray[$i] = 0; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
foreach ($arrayList as $matrixData) { |
| 99 |
$array2 = Functions::flattenArray($matrixData); |
| 100 |
$count = count($array2); |
| 101 |
if ($wrkCellCount != $count) { |
| 102 |
return ExcelError::VALUE(); |
| 103 |
} |
| 104 |
|
| 105 |
foreach ($array2 as $i => $val) { |
| 106 |
if ((!is_numeric($val)) || (is_string($val))) { |
| 107 |
$val = 0; |
| 108 |
} |
| 109 |
/** @var array<float|int> $wrkArray */ |
| 110 |
$wrkArray[$i] *= $val; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** @var array<float|int> $wrkArray */ |
| 115 |
return array_sum($wrkArray); |
| 116 |
} |
| 117 |
} |
| 118 |
|