Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Worksheet;
   4  
   5  class Column
   6  {
   7      /**
   8       * \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet.
   9       *
  10       * @var Worksheet
  11       */
  12      private $worksheet;
  13  
  14      /**
  15       * Column index.
  16       *
  17       * @var string
  18       */
  19      private $columnIndex;
  20  
  21      /**
  22       * Create a new column.
  23       *
  24       * @param string $columnIndex
  25       */
  26      public function __construct(Worksheet $worksheet, $columnIndex = 'A')
  27      {
  28          // Set parent and column index
  29          $this->worksheet = $worksheet;
  30          $this->columnIndex = $columnIndex;
  31      }
  32  
  33      /**
  34       * Destructor.
  35       */
  36      public function __destruct()
  37      {
  38          // @phpstan-ignore-next-line
  39          $this->worksheet = null;
  40      }
  41  
  42      /**
  43       * Get column index as string eg: 'A'.
  44       */
  45      public function getColumnIndex(): string
  46      {
  47          return $this->columnIndex;
  48      }
  49  
  50      /**
  51       * Get cell iterator.
  52       *
  53       * @param int $startRow The row number at which to start iterating
  54       * @param int $endRow Optionally, the row number at which to stop iterating
  55       *
  56       * @return ColumnCellIterator
  57       */
  58      public function getCellIterator($startRow = 1, $endRow = null)
  59      {
  60          return new ColumnCellIterator($this->worksheet, $this->columnIndex, $startRow, $endRow);
  61      }
  62  
  63      /**
  64       * Returns a boolean true if the column contains no cells. By default, this means that no cell records exist in the
  65       *         collection for this column. false will be returned otherwise.
  66       *     This rule can be modified by passing a $definitionOfEmptyFlags value:
  67       *          1 - CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL If the only cells in the collection are null value
  68       *                  cells, then the column will be considered empty.
  69       *          2 - CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL If the only cells in the collection are empty
  70       *                  string value cells, then the column will be considered empty.
  71       *          3 - CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL | CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL
  72       *                  If the only cells in the collection are null value or empty string value cells, then the column
  73       *                  will be considered empty.
  74       *
  75       * @param int $definitionOfEmptyFlags
  76       *              Possible Flag Values are:
  77       *                  CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL
  78       *                  CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL
  79       */
  80      public function isEmpty(int $definitionOfEmptyFlags = 0): bool
  81      {
  82          $nullValueCellIsEmpty = (bool) ($definitionOfEmptyFlags & CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL);
  83          $emptyStringCellIsEmpty = (bool) ($definitionOfEmptyFlags & CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL);
  84  
  85          $cellIterator = $this->getCellIterator();
  86          $cellIterator->setIterateOnlyExistingCells(true);
  87          foreach ($cellIterator as $cell) {
  88              $value = $cell->getValue();
  89              if ($value === null && $nullValueCellIsEmpty === true) {
  90                  continue;
  91              }
  92              if ($value === '' && $emptyStringCellIsEmpty === true) {
  93                  continue;
  94              }
  95  
  96              return false;
  97          }
  98  
  99          return true;
 100      }
 101  
 102      /**
 103       * Returns bound worksheet.
 104       */
 105      public function getWorksheet(): Worksheet
 106      {
 107          return $this->worksheet;
 108      }
 109  }