| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
|
|
|
8 |
use PhpOffice\PhpSpreadsheet\Calculation\Statistical;
|
|
|
9 |
|
|
|
10 |
class Subtotal
|
|
|
11 |
{
|
|
|
12 |
protected static function filterHiddenArgs(mixed $cellReference, mixed $args): array
|
|
|
13 |
{
|
|
|
14 |
return array_filter(
|
|
|
15 |
$args,
|
|
|
16 |
function ($index) use ($cellReference) {
|
|
|
17 |
$explodeArray = explode('.', $index);
|
|
|
18 |
$row = $explodeArray[1] ?? '';
|
|
|
19 |
if (!is_numeric($row)) {
|
|
|
20 |
return true;
|
|
|
21 |
}
|
|
|
22 |
|
|
|
23 |
return $cellReference->getWorksheet()->getRowDimension($row)->getVisible();
|
|
|
24 |
},
|
|
|
25 |
ARRAY_FILTER_USE_KEY
|
|
|
26 |
);
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
protected static function filterFormulaArgs(mixed $cellReference, mixed $args): array
|
|
|
30 |
{
|
|
|
31 |
return array_filter(
|
|
|
32 |
$args,
|
|
|
33 |
function ($index) use ($cellReference): bool {
|
|
|
34 |
$explodeArray = explode('.', $index);
|
|
|
35 |
$row = $explodeArray[1] ?? '';
|
|
|
36 |
$column = $explodeArray[2] ?? '';
|
|
|
37 |
$retVal = true;
|
|
|
38 |
if ($cellReference->getWorksheet()->cellExists($column . $row)) {
|
|
|
39 |
//take this cell out if it contains the SUBTOTAL or AGGREGATE functions in a formula
|
|
|
40 |
$isFormula = $cellReference->getWorksheet()->getCell($column . $row)->isFormula();
|
|
|
41 |
$cellFormula = !preg_match(
|
|
|
42 |
'/^=.*\b(SUBTOTAL|AGGREGATE)\s*\(/i',
|
|
|
43 |
$cellReference->getWorksheet()->getCell($column . $row)->getValue() ?? ''
|
|
|
44 |
);
|
|
|
45 |
|
|
|
46 |
$retVal = !$isFormula || $cellFormula;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
return $retVal;
|
|
|
50 |
},
|
|
|
51 |
ARRAY_FILTER_USE_KEY
|
|
|
52 |
);
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* @var array<int, callable>
|
|
|
57 |
*/
|
|
|
58 |
private const CALL_FUNCTIONS = [
|
|
|
59 |
1 => [Statistical\Averages::class, 'average'], // 1 and 101
|
|
|
60 |
[Statistical\Counts::class, 'COUNT'], // 2 and 102
|
|
|
61 |
[Statistical\Counts::class, 'COUNTA'], // 3 and 103
|
|
|
62 |
[Statistical\Maximum::class, 'max'], // 4 and 104
|
|
|
63 |
[Statistical\Minimum::class, 'min'], // 5 and 105
|
|
|
64 |
[Operations::class, 'product'], // 6 and 106
|
|
|
65 |
[Statistical\StandardDeviations::class, 'STDEV'], // 7 and 107
|
|
|
66 |
[Statistical\StandardDeviations::class, 'STDEVP'], // 8 and 108
|
|
|
67 |
[Sum::class, 'sumIgnoringStrings'], // 9 and 109
|
|
|
68 |
[Statistical\Variances::class, 'VAR'], // 10 and 110
|
|
|
69 |
[Statistical\Variances::class, 'VARP'], // 111 and 111
|
|
|
70 |
];
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* SUBTOTAL.
|
|
|
74 |
*
|
|
|
75 |
* Returns a subtotal in a list or database.
|
|
|
76 |
*
|
|
|
77 |
* @param mixed $functionType
|
|
|
78 |
* A number 1 to 11 that specifies which function to
|
|
|
79 |
* use in calculating subtotals within a range
|
|
|
80 |
* list
|
|
|
81 |
* Numbers 101 to 111 shadow the functions of 1 to 11
|
|
|
82 |
* but ignore any values in the range that are
|
|
|
83 |
* in hidden rows
|
|
|
84 |
* @param mixed[] $args A mixed data series of values
|
|
|
85 |
*/
|
|
|
86 |
public static function evaluate(mixed $functionType, ...$args): float|int|string
|
|
|
87 |
{
|
|
|
88 |
$cellReference = array_pop($args);
|
|
|
89 |
$bArgs = Functions::flattenArrayIndexed($args);
|
|
|
90 |
$aArgs = [];
|
|
|
91 |
// int keys must come before string keys for PHP 8.0+
|
|
|
92 |
// Otherwise, PHP thinks positional args follow keyword
|
|
|
93 |
// in the subsequent call to call_user_func_array.
|
|
|
94 |
// Fortunately, order of args is unimportant to Subtotal.
|
|
|
95 |
foreach ($bArgs as $key => $value) {
|
|
|
96 |
if (is_int($key)) {
|
|
|
97 |
$aArgs[$key] = $value;
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
foreach ($bArgs as $key => $value) {
|
|
|
101 |
if (!is_int($key)) {
|
|
|
102 |
$aArgs[$key] = $value;
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
try {
|
|
|
107 |
$subtotal = (int) Helpers::validateNumericNullBool($functionType);
|
|
|
108 |
} catch (Exception $e) {
|
|
|
109 |
return $e->getMessage();
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
// Calculate
|
|
|
113 |
if ($subtotal > 100) {
|
|
|
114 |
$aArgs = self::filterHiddenArgs($cellReference, $aArgs);
|
|
|
115 |
$subtotal -= 100;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
$aArgs = self::filterFormulaArgs($cellReference, $aArgs);
|
|
|
119 |
if (array_key_exists($subtotal, self::CALL_FUNCTIONS)) {
|
|
|
120 |
$call = self::CALL_FUNCTIONS[$subtotal];
|
|
|
121 |
|
|
|
122 |
return call_user_func_array($call, $aArgs);
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
return ExcelError::VALUE();
|
|
|
126 |
}
|
|
|
127 |
}
|