1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
6
|
|
|
|
7
|
|
|
class Sum |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* SUM, ignoring non-numeric non-error strings. This is eventually used by SUMIF. |
11
|
|
|
* |
12
|
|
|
* SUM computes the sum of all the values and cells referenced in the argument list. |
13
|
|
|
* |
14
|
|
|
* Excel Function: |
15
|
|
|
* SUM(value1[,value2[, ...]]) |
16
|
|
|
* |
17
|
|
|
* @param mixed ...$args Data values |
18
|
|
|
* |
19
|
|
|
* @return float|string |
20
|
|
|
*/ |
21
|
28 |
|
public static function sumIgnoringStrings(...$args) |
22
|
|
|
{ |
23
|
28 |
|
$returnValue = 0; |
24
|
|
|
|
25
|
|
|
// Loop through the arguments |
26
|
28 |
|
foreach (Functions::flattenArray($args) as $arg) { |
27
|
|
|
// Is it a numeric value? |
28
|
28 |
|
if (is_numeric($arg)) { |
29
|
27 |
|
$returnValue += $arg; |
30
|
4 |
|
} elseif (Functions::isError($arg)) { |
31
|
1 |
|
return $arg; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
27 |
|
return $returnValue; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* SUM, returning error for non-numeric strings. This is used by Excel SUM function. |
40
|
|
|
* |
41
|
|
|
* SUM computes the sum of all the values and cells referenced in the argument list. |
42
|
|
|
* |
43
|
|
|
* Excel Function: |
44
|
|
|
* SUM(value1[,value2[, ...]]) |
45
|
|
|
* |
46
|
|
|
* @param mixed ...$args Data values |
47
|
|
|
* |
48
|
|
|
* @return float|string |
49
|
|
|
*/ |
50
|
155 |
|
public static function sumErroringStrings(...$args) |
51
|
|
|
{ |
52
|
155 |
|
$returnValue = 0; |
53
|
|
|
// Loop through the arguments |
54
|
155 |
|
$aArgs = Functions::flattenArrayIndexed($args); |
55
|
155 |
|
foreach ($aArgs as $k => $arg) { |
56
|
|
|
// Is it a numeric value? |
57
|
155 |
|
if (is_numeric($arg) || empty($arg)) { |
58
|
144 |
|
if (is_string($arg)) { |
59
|
17 |
|
$arg = (int) $arg; |
60
|
|
|
} |
61
|
144 |
|
$returnValue += $arg; |
62
|
17 |
|
} elseif (is_bool($arg)) { |
63
|
3 |
|
$returnValue += (int) $arg; |
64
|
15 |
|
} elseif (Functions::isError($arg)) { |
65
|
11 |
|
return $arg; |
66
|
|
|
// ignore non-numerics from cell, but fail as literals (except null) |
67
|
4 |
|
} elseif ($arg !== null && !Functions::isCellValue($k)) { |
68
|
1 |
|
return Functions::VALUE(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
143 |
|
return $returnValue; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|