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.
   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Cell;
   4  
   5  use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
   6  
   7  class ColumnRange implements AddressRange
   8  {
   9      /**
  10       * @var ?Worksheet
  11       */
  12      protected $worksheet;
  13  
  14      /**
  15       * @var int
  16       */
  17      protected $from;
  18  
  19      /**
  20       * @var int
  21       */
  22      protected $to;
  23  
  24      public function __construct(string $from, ?string $to = null, ?Worksheet $worksheet = null)
  25      {
  26          $this->validateFromTo(
  27              Coordinate::columnIndexFromString($from),
  28              Coordinate::columnIndexFromString($to ?? $from)
  29          );
  30          $this->worksheet = $worksheet;
  31      }
  32  
  33      public static function fromColumnIndexes(int $from, int $to, ?Worksheet $worksheet = null): self
  34      {
  35          return new self(Coordinate::stringFromColumnIndex($from), Coordinate::stringFromColumnIndex($to), $worksheet);
  36      }
  37  
  38      /**
  39       * @param array<int|string> $array
  40       */
  41      public static function fromArray(array $array, ?Worksheet $worksheet = null): self
  42      {
  43          array_walk(
  44              $array,
  45              function (&$column): void {
  46                  $column = is_numeric($column) ? Coordinate::stringFromColumnIndex((int) $column) : $column;
  47              }
  48          );
  49          /** @var string $from */
  50          /** @var string $to */
  51          [$from, $to] = $array;
  52  
  53          return new self($from, $to, $worksheet);
  54      }
  55  
  56      private function validateFromTo(int $from, int $to): void
  57      {
  58          // Identify actual top and bottom values (in case we've been given bottom and top)
  59          $this->from = min($from, $to);
  60          $this->to = max($from, $to);
  61      }
  62  
  63      public function columnCount(): int
  64      {
  65          return $this->to - $this->from + 1;
  66      }
  67  
  68      public function shiftDown(int $offset = 1): self
  69      {
  70          $newFrom = $this->from + $offset;
  71          $newFrom = ($newFrom < 1) ? 1 : $newFrom;
  72  
  73          $newTo = $this->to + $offset;
  74          $newTo = ($newTo < 1) ? 1 : $newTo;
  75  
  76          return self::fromColumnIndexes($newFrom, $newTo, $this->worksheet);
  77      }
  78  
  79      public function shiftUp(int $offset = 1): self
  80      {
  81          return $this->shiftDown(0 - $offset);
  82      }
  83  
  84      public function from(): string
  85      {
  86          return Coordinate::stringFromColumnIndex($this->from);
  87      }
  88  
  89      public function to(): string
  90      {
  91          return Coordinate::stringFromColumnIndex($this->to);
  92      }
  93  
  94      public function fromIndex(): int
  95      {
  96          return $this->from;
  97      }
  98  
  99      public function toIndex(): int
 100      {
 101          return $this->to;
 102      }
 103  
 104      public function toCellRange(): CellRange
 105      {
 106          return new CellRange(
 107              CellAddress::fromColumnAndRow($this->from, 1, $this->worksheet),
 108              CellAddress::fromColumnAndRow($this->to, AddressRange::MAX_ROW)
 109          );
 110      }
 111  
 112      public function __toString(): string
 113      {
 114          $from = $this->from();
 115          $to = $this->to();
 116  
 117          if ($this->worksheet !== null) {
 118              $title = str_replace("'", "''", $this->worksheet->getTitle());
 119  
 120              return "'{$title}'!{$from}:{$to}";
 121          }
 122  
 123          return "{$from}:{$to}";
 124      }
 125  }