Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
 
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
     */
21
    public static function sumIgnoringStrings(mixed ...$args): float|int|string
22
    {
23
        $returnValue = 0;
24
 
25
        // Loop through the arguments
26
        foreach (Functions::flattenArray($args) as $arg) {
27
            // Is it a numeric value?
28
            if (is_numeric($arg)) {
29
                $returnValue += $arg;
30
            } elseif (ErrorValue::isError($arg)) {
31
                return $arg;
32
            }
33
        }
34
 
35
        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
    public static function sumErroringStrings(mixed ...$args): float|int|string|array
49
    {
50
        $returnValue = 0;
51
        // Loop through the arguments
52
        $aArgs = Functions::flattenArrayIndexed($args);
53
        foreach ($aArgs as $k => $arg) {
54
            // Is it a numeric value?
55
            if (is_numeric($arg)) {
56
                $returnValue += $arg;
57
            } elseif (is_bool($arg)) {
58
                $returnValue += (int) $arg;
59
            } elseif (ErrorValue::isError($arg)) {
60
                return $arg;
61
            } elseif ($arg !== null && !Functions::isCellValue($k)) {
62
                // ignore non-numerics from cell, but fail as literals (except null)
63
                return ExcelError::VALUE();
64
            }
65
        }
66
 
67
        return $returnValue;
68
    }
69
 
70
    /**
71
     * SUMPRODUCT.
72
     *
73
     * Excel Function:
74
     *        SUMPRODUCT(value1[,value2[, ...]])
75
     *
76
     * @param mixed ...$args Data values
77
     *
78
     * @return float|int|string The result, or a string containing an error
79
     */
80
    public static function product(mixed ...$args): string|int|float
81
    {
82
        $arrayList = $args;
83
 
84
        $wrkArray = Functions::flattenArray(array_shift($arrayList));
85
        $wrkCellCount = count($wrkArray);
86
 
87
        for ($i = 0; $i < $wrkCellCount; ++$i) {
88
            if ((!is_numeric($wrkArray[$i])) || (is_string($wrkArray[$i]))) {
89
                $wrkArray[$i] = 0;
90
            }
91
        }
92
 
93
        foreach ($arrayList as $matrixData) {
94
            $array2 = Functions::flattenArray($matrixData);
95
            $count = count($array2);
96
            if ($wrkCellCount != $count) {
97
                return ExcelError::VALUE();
98
            }
99
 
100
            foreach ($array2 as $i => $val) {
101
                if ((!is_numeric($val)) || (is_string($val))) {
102
                    $val = 0;
103
                }
104
                $wrkArray[$i] *= $val;
105
            }
106
        }
107
 
108
        return array_sum($wrkArray);
109
    }
110
}