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 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [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      public function getCellIterator($startRow = 1, $endRow = null): ColumnCellIterator
  57      {
  58          return new ColumnCellIterator($this->worksheet, $this->columnIndex, $startRow, $endRow);
  59      }
  60  
  61      /**
  62       * Get row iterator. Synonym for getCellIterator().
  63       *
  64       * @param int $startRow The row number at which to start iterating
  65       * @param int $endRow Optionally, the row number at which to stop iterating
  66       */
  67      public function getRowIterator($startRow = 1, $endRow = null): ColumnCellIterator
  68      {
  69          return $this->getCellIterator($startRow, $endRow);
  70      }
  71  
  72      /**
  73       * Returns a boolean true if the column contains no cells. By default, this means that no cell records exist in the
  74       *         collection for this column. false will be returned otherwise.
  75       *     This rule can be modified by passing a $definitionOfEmptyFlags value:
  76       *          1 - CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL If the only cells in the collection are null value
  77       *                  cells, then the column will be considered empty.
  78       *          2 - CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL If the only cells in the collection are empty
  79       *                  string value cells, then the column will be considered empty.
  80       *          3 - CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL | CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL
  81       *                  If the only cells in the collection are null value or empty string value cells, then the column
  82       *                  will be considered empty.
  83       *
  84       * @param int $definitionOfEmptyFlags
  85       *              Possible Flag Values are:
  86       *                  CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL
  87       *                  CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL
  88       * @param int $startRow The row number at which to start checking if cells are empty
  89       * @param int $endRow Optionally, the row number at which to stop checking if cells are empty
  90       */
  91      public function isEmpty(int $definitionOfEmptyFlags = 0, $startRow = 1, $endRow = null): bool
  92      {
  93          $nullValueCellIsEmpty = (bool) ($definitionOfEmptyFlags & CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL);
  94          $emptyStringCellIsEmpty = (bool) ($definitionOfEmptyFlags & CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL);
  95  
  96          $cellIterator = $this->getCellIterator($startRow, $endRow);
  97          $cellIterator->setIterateOnlyExistingCells(true);
  98          foreach ($cellIterator as $cell) {
  99              /** @scrutinizer ignore-call */
 100              $value = $cell->getValue();
 101              if ($value === null && $nullValueCellIsEmpty === true) {
 102                  continue;
 103              }
 104              if ($value === '' && $emptyStringCellIsEmpty === true) {
 105                  continue;
 106              }
 107  
 108              return false;
 109          }
 110  
 111          return true;
 112      }
 113  
 114      /**
 115       * Returns bound worksheet.
 116       */
 117      public function getWorksheet(): Worksheet
 118      {
 119          return $this->worksheet;
 120      }
 121  }