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 401 and 402] [Versions 401 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet;
   4  
   5  use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
   6  
   7  class CellReferenceHelper
   8  {
   9      /**
  10       * @var string
  11       */
  12      protected $beforeCellAddress;
  13  
  14      /**
  15       * @var int
  16       */
  17      protected $beforeColumn;
  18  
  19      /**
  20       * @var int
  21       */
  22      protected $beforeRow;
  23  
  24      /**
  25       * @var int
  26       */
  27      protected $numberOfColumns;
  28  
  29      /**
  30       * @var int
  31       */
  32      protected $numberOfRows;
  33  
  34      public function __construct(string $beforeCellAddress = 'A1', int $numberOfColumns = 0, int $numberOfRows = 0)
  35      {
  36          $this->beforeCellAddress = str_replace('$', '', $beforeCellAddress);
  37          $this->numberOfColumns = $numberOfColumns;
  38          $this->numberOfRows = $numberOfRows;
  39  
  40          // Get coordinate of $beforeCellAddress
  41          [$beforeColumn, $beforeRow] = Coordinate::coordinateFromString($beforeCellAddress);
  42          $this->beforeColumn = (int) Coordinate::columnIndexFromString($beforeColumn);
  43          $this->beforeRow = (int) $beforeRow;
  44      }
  45  
  46      public function beforeCellAddress(): string
  47      {
  48          return $this->beforeCellAddress;
  49      }
  50  
  51      public function refreshRequired(string $beforeCellAddress, int $numberOfColumns, int $numberOfRows): bool
  52      {
  53          return $this->beforeCellAddress !== $beforeCellAddress ||
  54              $this->numberOfColumns !== $numberOfColumns ||
  55              $this->numberOfRows !== $numberOfRows;
  56      }
  57  
  58      public function updateCellReference(string $cellReference = 'A1', bool $includeAbsoluteReferences = false): string
  59      {
  60          if (Coordinate::coordinateIsRange($cellReference)) {
  61              throw new Exception('Only single cell references may be passed to this method.');
  62          }
  63  
  64          // Get coordinate of $cellReference
  65          [$newColumn, $newRow] = Coordinate::coordinateFromString($cellReference);
  66          $newColumnIndex = (int) Coordinate::columnIndexFromString(str_replace('$', '', $newColumn));
  67          $newRowIndex = (int) str_replace('$', '', $newRow);
  68  
  69          $absoluteColumn = $newColumn[0] === '$' ? '$' : '';
  70          $absoluteRow = $newRow[0] === '$' ? '$' : '';
  71          // Verify which parts should be updated
  72          if ($includeAbsoluteReferences === false) {
  73              $updateColumn = (($absoluteColumn !== '$') && $newColumnIndex >= $this->beforeColumn);
  74              $updateRow = (($absoluteRow !== '$') && $newRowIndex >= $this->beforeRow);
  75          } else {
  76              $updateColumn = ($newColumnIndex >= $this->beforeColumn);
  77              $updateRow = ($newRowIndex >= $this->beforeRow);
  78          }
  79  
  80          // Create new column reference
  81          if ($updateColumn) {
  82              $newColumn = ($includeAbsoluteReferences === false)
  83                  ? Coordinate::stringFromColumnIndex($newColumnIndex + $this->numberOfColumns)
  84                  : $absoluteColumn . Coordinate::stringFromColumnIndex($newColumnIndex + $this->numberOfColumns);
  85          }
  86  
  87          // Create new row reference
  88          if ($updateRow) {
  89              $newRow = ($includeAbsoluteReferences === false)
  90                  ? $newRowIndex + $this->numberOfRows
  91                  : $absoluteRow . (string) ($newRowIndex + $this->numberOfRows);
  92          }
  93  
  94          // Return new reference
  95          return "{$newColumn}{$newRow}";
  96      }
  97  
  98      public function cellAddressInDeleteRange(string $cellAddress): bool
  99      {
 100          [$cellColumn, $cellRow] = Coordinate::coordinateFromString($cellAddress);
 101          $cellColumnIndex = Coordinate::columnIndexFromString($cellColumn);
 102          //    Is cell within the range of rows/columns if we're deleting
 103          if (
 104              $this->numberOfRows < 0 &&
 105              ($cellRow >= ($this->beforeRow + $this->numberOfRows)) &&
 106              ($cellRow < $this->beforeRow)
 107          ) {
 108              return true;
 109          } elseif (
 110              $this->numberOfColumns < 0 &&
 111              ($cellColumnIndex >= ($this->beforeColumn + $this->numberOfColumns)) &&
 112              ($cellColumnIndex < $this->beforeColumn)
 113          ) {
 114              return true;
 115          }
 116  
 117          return false;
 118      }
 119  }