NamedRange.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet;
  3. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  4. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  5. class NamedRange extends DefinedName
  6. {
  7. /**
  8. * Create a new Named Range.
  9. */
  10. public function __construct(
  11. string $name,
  12. ?Worksheet $worksheet = null,
  13. string $range = 'A1',
  14. bool $localOnly = false,
  15. ?Worksheet $scope = null
  16. ) {
  17. if ($worksheet === null && $scope === null) {
  18. throw new Exception('You must specify a worksheet or a scope for a Named Range');
  19. }
  20. parent::__construct($name, $worksheet, $range, $localOnly, $scope);
  21. }
  22. /**
  23. * Get the range value.
  24. */
  25. public function getRange(): string
  26. {
  27. return $this->value;
  28. }
  29. /**
  30. * Set the range value.
  31. */
  32. public function setRange(string $range): self
  33. {
  34. if (!empty($range)) {
  35. $this->value = $range;
  36. }
  37. return $this;
  38. }
  39. public function getCellsInRange(): array
  40. {
  41. $range = $this->value;
  42. if (substr($range, 0, 1) === '=') {
  43. $range = substr($range, 1);
  44. }
  45. return Coordinate::extractAllCellReferencesInRange($range);
  46. }
  47. }