Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  
   3  namespace Box\Spout\Writer\Common\Entity;
   4  
   5  use Box\Spout\Writer\Common\Manager\SheetManager;
   6  
   7  /**
   8   * Class Sheet
   9   * External representation of a worksheet
  10   */
  11  class Sheet
  12  {
  13      const DEFAULT_SHEET_NAME_PREFIX = 'Sheet';
  14  
  15      /** @var int Index of the sheet, based on order in the workbook (zero-based) */
  16      private $index;
  17  
  18      /** @var string ID of the sheet's associated workbook. Used to restrict sheet name uniqueness enforcement to a single workbook */
  19      private $associatedWorkbookId;
  20  
  21      /** @var string Name of the sheet */
  22      private $name;
  23  
  24      /** @var bool Visibility of the sheet */
  25      private $isVisible;
  26  
  27      /** @var SheetManager Sheet manager */
  28      private $sheetManager;
  29  
  30      /**
  31       * @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based)
  32       * @param string $associatedWorkbookId ID of the sheet's associated workbook
  33       * @param SheetManager $sheetManager To manage sheets
  34       */
  35      public function __construct($sheetIndex, $associatedWorkbookId, SheetManager $sheetManager)
  36      {
  37          $this->index = $sheetIndex;
  38          $this->associatedWorkbookId = $associatedWorkbookId;
  39  
  40          $this->sheetManager = $sheetManager;
  41          $this->sheetManager->markWorkbookIdAsUsed($associatedWorkbookId);
  42  
  43          $this->setName(self::DEFAULT_SHEET_NAME_PREFIX . ($sheetIndex + 1));
  44          $this->setIsVisible(true);
  45      }
  46  
  47      /**
  48       * @return int Index of the sheet, based on order in the workbook (zero-based)
  49       */
  50      public function getIndex()
  51      {
  52          return $this->index;
  53      }
  54  
  55      /**
  56       * @return string
  57       */
  58      public function getAssociatedWorkbookId()
  59      {
  60          return $this->associatedWorkbookId;
  61      }
  62  
  63      /**
  64       * @return string Name of the sheet
  65       */
  66      public function getName()
  67      {
  68          return $this->name;
  69      }
  70  
  71      /**
  72       * Sets the name of the sheet. Note that Excel has some restrictions on the name:
  73       *  - it should not be blank
  74       *  - it should not exceed 31 characters
  75       *  - it should not contain these characters: \ / ? * : [ or ]
  76       *  - it should be unique
  77       *
  78       * @param string $name Name of the sheet
  79       * @throws \Box\Spout\Writer\Exception\InvalidSheetNameException If the sheet's name is invalid.
  80       * @return Sheet
  81       */
  82      public function setName($name)
  83      {
  84          $this->sheetManager->throwIfNameIsInvalid($name, $this);
  85  
  86          $this->name = $name;
  87  
  88          $this->sheetManager->markSheetNameAsUsed($this);
  89  
  90          return $this;
  91      }
  92  
  93      /**
  94       * @return bool isVisible Visibility of the sheet
  95       */
  96      public function isVisible()
  97      {
  98          return $this->isVisible;
  99      }
 100  
 101      /**
 102       * @param bool $isVisible Visibility of the sheet
 103       * @return Sheet
 104       */
 105      public function setIsVisible($isVisible)
 106      {
 107          $this->isVisible = $isVisible;
 108  
 109          return $this;
 110      }
 111  }