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