Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 400 and 403] [Versions 401 and 403]

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