Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet;
   4  
   5  use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
   6  
   7  class NamedRange
   8  {
   9      /**
  10       * Range name.
  11       *
  12       * @var string
  13       */
  14      private $name;
  15  
  16      /**
  17       * Worksheet on which the named range can be resolved.
  18       *
  19       * @var Worksheet
  20       */
  21      private $worksheet;
  22  
  23      /**
  24       * Range of the referenced cells.
  25       *
  26       * @var string
  27       */
  28      private $range;
  29  
  30      /**
  31       * Is the named range local? (i.e. can only be used on $this->worksheet).
  32       *
  33       * @var bool
  34       */
  35      private $localOnly;
  36  
  37      /**
  38       * Scope.
  39       *
  40       * @var Worksheet
  41       */
  42      private $scope;
  43  
  44      /**
  45       * Create a new NamedRange.
  46       *
  47       * @param string $pName
  48       * @param Worksheet $pWorksheet
  49       * @param string $pRange
  50       * @param bool $pLocalOnly
  51       * @param null|Worksheet $pScope Scope. Only applies when $pLocalOnly = true. Null for global scope.
  52       *
  53       * @throws Exception
  54       */
  55      public function __construct($pName, Worksheet $pWorksheet, $pRange = 'A1', $pLocalOnly = false, $pScope = null)
  56      {
  57          // Validate data
  58          if (($pName === null) || ($pWorksheet === null) || ($pRange === null)) {
  59              throw new Exception('Parameters can not be null.');
  60          }
  61  
  62          // Set local members
  63          $this->name = $pName;
  64          $this->worksheet = $pWorksheet;
  65          $this->range = $pRange;
  66          $this->localOnly = $pLocalOnly;
  67          $this->scope = ($pLocalOnly == true) ? (($pScope == null) ? $pWorksheet : $pScope) : null;
  68      }
  69  
  70      /**
  71       * Get name.
  72       *
  73       * @return string
  74       */
  75      public function getName()
  76      {
  77          return $this->name;
  78      }
  79  
  80      /**
  81       * Set name.
  82       *
  83       * @param string $value
  84       *
  85       * @return NamedRange
  86       */
  87      public function setName($value)
  88      {
  89          if ($value !== null) {
  90              // Old title
  91              $oldTitle = $this->name;
  92  
  93              // Re-attach
  94              if ($this->worksheet !== null) {
  95                  $this->worksheet->getParent()->removeNamedRange($this->name, $this->worksheet);
  96              }
  97              $this->name = $value;
  98  
  99              if ($this->worksheet !== null) {
 100                  $this->worksheet->getParent()->addNamedRange($this);
 101              }
 102  
 103              // New title
 104              $newTitle = $this->name;
 105              ReferenceHelper::getInstance()->updateNamedFormulas($this->worksheet->getParent(), $oldTitle, $newTitle);
 106          }
 107  
 108          return $this;
 109      }
 110  
 111      /**
 112       * Get worksheet.
 113       *
 114       * @return Worksheet
 115       */
 116      public function getWorksheet()
 117      {
 118          return $this->worksheet;
 119      }
 120  
 121      /**
 122       * Set worksheet.
 123       *
 124       * @param Worksheet $value
 125       *
 126       * @return NamedRange
 127       */
 128      public function setWorksheet(Worksheet $value = null)
 129      {
 130          if ($value !== null) {
 131              $this->worksheet = $value;
 132          }
 133  
 134          return $this;
 135      }
 136  
 137      /**
 138       * Get range.
 139       *
 140       * @return string
 141       */
 142      public function getRange()
 143      {
 144          return $this->range;
 145      }
 146  
 147      /**
 148       * Set range.
 149       *
 150       * @param string $value
 151       *
 152       * @return NamedRange
 153       */
 154      public function setRange($value)
 155      {
 156          if ($value !== null) {
 157              $this->range = $value;
 158          }
 159  
 160          return $this;
 161      }
 162  
 163      /**
 164       * Get localOnly.
 165       *
 166       * @return bool
 167       */
 168      public function getLocalOnly()
 169      {
 170          return $this->localOnly;
 171      }
 172  
 173      /**
 174       * Set localOnly.
 175       *
 176       * @param bool $value
 177       *
 178       * @return NamedRange
 179       */
 180      public function setLocalOnly($value)
 181      {
 182          $this->localOnly = $value;
 183          $this->scope = $value ? $this->worksheet : null;
 184  
 185          return $this;
 186      }
 187  
 188      /**
 189       * Get scope.
 190       *
 191       * @return null|Worksheet
 192       */
 193      public function getScope()
 194      {
 195          return $this->scope;
 196      }
 197  
 198      /**
 199       * Set scope.
 200       *
 201       * @param null|Worksheet $value
 202       *
 203       * @return NamedRange
 204       */
 205      public function setScope(Worksheet $value = null)
 206      {
 207          $this->scope = $value;
 208          $this->localOnly = $value != null;
 209  
 210          return $this;
 211      }
 212  
 213      /**
 214       * Resolve a named range to a regular cell range.
 215       *
 216       * @param string $pNamedRange Named range
 217       * @param null|Worksheet $pSheet Scope. Use null for global scope
 218       *
 219       * @return NamedRange
 220       */
 221      public static function resolveRange($pNamedRange, Worksheet $pSheet)
 222      {
 223          return $pSheet->getParent()->getNamedRange($pNamedRange, $pSheet);
 224      }
 225  
 226      /**
 227       * Implement PHP __clone to create a deep clone, not just a shallow copy.
 228       */
 229      public function __clone()
 230      {
 231          $vars = get_object_vars($this);
 232          foreach ($vars as $key => $value) {
 233              if (is_object($value)) {
 234                  $this->$key = clone $value;
 235              } else {
 236                  $this->$key = $value;
 237              }
 238          }
 239      }
 240  }