| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Statistical; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcException; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 7 |
|
| 8 |
class Counts extends AggregateBase |
| 9 |
{ |
| 10 |
/** |
| 11 |
* COUNT. |
| 12 |
* |
| 13 |
* Counts the number of cells that contain numbers within the list of arguments |
| 14 |
* |
| 15 |
* Excel Function: |
| 16 |
* COUNT(value1[,value2[, ...]]) |
| 17 |
* |
| 18 |
* @param mixed ...$args Data values |
| 19 |
*/ |
| 20 |
public static function COUNT(...$args): int |
| 21 |
{ |
| 22 |
$returnValue = 0; |
| 23 |
|
| 24 |
// Loop through arguments |
| 25 |
$aArgs = Functions::flattenArrayIndexed($args); |
| 26 |
foreach ($aArgs as $k => $arg) { |
| 27 |
$arg = self::testAcceptedBoolean($arg, $k); |
| 28 |
// Is it a numeric value? |
| 29 |
// Strings containing numeric values are only counted if they are string literals (not cell values) |
| 30 |
// and then only in MS Excel and in Open Office, not in Gnumeric |
| 31 |
if (self::isAcceptedCountable($arg, $k, true)) { |
| 32 |
++$returnValue; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
return $returnValue; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* COUNTA. |
| 41 |
* |
| 42 |
* Counts the number of cells that are not empty within the list of arguments |
| 43 |
* |
| 44 |
* Excel Function: |
| 45 |
* COUNTA(value1[,value2[, ...]]) |
| 46 |
* |
| 47 |
* @param mixed ...$args Data values |
| 48 |
*/ |
| 49 |
public static function COUNTA(...$args): int |
| 50 |
{ |
| 51 |
$returnValue = 0; |
| 52 |
|
| 53 |
// Loop through arguments |
| 54 |
$aArgs = Functions::flattenArrayIndexed($args); |
| 55 |
foreach ($aArgs as $k => $arg) { |
| 56 |
// Nulls are counted if literals, but not if cell values |
| 57 |
if ($arg !== null || (!Functions::isCellValue($k))) { |
| 58 |
++$returnValue; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
return $returnValue; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* COUNTBLANK. |
| 67 |
* |
| 68 |
* Counts the number of empty cells within the list of arguments |
| 69 |
* |
| 70 |
* Excel Function: |
| 71 |
* COUNTBLANK(value1[,value2[, ...]]) |
| 72 |
* |
| 73 |
* @param mixed $range Data values |
| 74 |
*/ |
| 75 |
public static function COUNTBLANK($range): int |
| 76 |
{ |
| 77 |
if ($range === null) { |
| 78 |
return 1; |
| 79 |
} |
| 80 |
if (!is_array($range) || array_key_exists(0, $range)) { |
| 81 |
throw new CalcException('Must specify range of cells, not any kind of literal'); |
| 82 |
} |
| 83 |
$returnValue = 0; |
| 84 |
|
| 85 |
// Loop through arguments |
| 86 |
$aArgs = Functions::flattenArray($range); |
| 87 |
foreach ($aArgs as $arg) { |
| 88 |
// Is it a blank cell? |
| 89 |
if (($arg === null) || ((is_string($arg)) && ($arg == ''))) { |
| 90 |
++$returnValue; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return $returnValue; |
| 95 |
} |
| 96 |
} |
| 97 |
|