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 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Worksheet;
   4  
   5  use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
   6  use PhpOffice\PhpSpreadsheet\Helper\Dimension as CssDimension;
   7  
   8  class ColumnDimension extends Dimension
   9  {
  10      /**
  11       * Column index.
  12       *
  13       * @var string
  14       */
  15      private $columnIndex;
  16  
  17      /**
  18       * Column width.
  19       *
  20       * When this is set to a negative value, the column width should be ignored by IWriter
  21       *
  22       * @var float
  23       */
  24      private $width = -1;
  25  
  26      /**
  27       * Auto size?
  28       *
  29       * @var bool
  30       */
  31      private $autoSize = false;
  32  
  33      /**
  34       * Create a new ColumnDimension.
  35       *
  36       * @param string $index Character column index
  37       */
  38      public function __construct($index = 'A')
  39      {
  40          // Initialise values
  41          $this->columnIndex = $index;
  42  
  43          // set dimension as unformatted by default
  44          parent::__construct(0);
  45      }
  46  
  47      /**
  48       * Get column index as string eg: 'A'.
  49       */
  50      public function getColumnIndex(): string
  51      {
  52          return $this->columnIndex;
  53      }
  54  
  55      /**
  56       * Set column index as string eg: 'A'.
  57       */
  58      public function setColumnIndex(string $index): self
  59      {
  60          $this->columnIndex = $index;
  61  
  62          return $this;
  63      }
  64  
  65      /**
  66       * Get column index as numeric.
  67       */
  68      public function getColumnNumeric(): int
  69      {
  70          return Coordinate::columnIndexFromString($this->columnIndex);
  71      }
  72  
  73      /**
  74       * Set column index as numeric.
  75       */
  76      public function setColumnNumeric(int $index): self
  77      {
  78          $this->columnIndex = Coordinate::stringFromColumnIndex($index);
  79  
  80          return $this;
  81      }
  82  
  83      /**
  84       * Get Width.
  85       *
  86       * Each unit of column width is equal to the width of one character in the default font size. A value of -1
  87       *      tells Excel to display this column in its default width.
  88       * By default, this will be the return value; but this method also accepts an optional unit of measure argument
  89       *    and will convert the returned value to the specified UoM..
  90       */
  91      public function getWidth(?string $unitOfMeasure = null): float
  92      {
  93          return ($unitOfMeasure === null || $this->width < 0)
  94              ? $this->width
  95              : (new CssDimension((string) $this->width))->toUnit($unitOfMeasure);
  96      }
  97  
  98      /**
  99       * Set Width.
 100       *
 101       * Each unit of column width is equal to the width of one character in the default font size. A value of -1
 102       *      tells Excel to display this column in its default width.
 103       * By default, this will be the unit of measure for the passed value; but this method also accepts an
 104       *    optional unit of measure argument, and will convert the value from the specified UoM using an
 105       *    approximation method.
 106       *
 107       * @return $this
 108       */
 109      public function setWidth(float $width, ?string $unitOfMeasure = null)
 110      {
 111          $this->width = ($unitOfMeasure === null || $width < 0)
 112              ? $width
 113              : (new CssDimension("{$width}{$unitOfMeasure}"))->width();
 114  
 115          return $this;
 116      }
 117  
 118      /**
 119       * Get Auto Size.
 120       */
 121      public function getAutoSize(): bool
 122      {
 123          return $this->autoSize;
 124      }
 125  
 126      /**
 127       * Set Auto Size.
 128       *
 129       * @return $this
 130       */
 131      public function setAutoSize(bool $autosizeEnabled)
 132      {
 133          $this->autoSize = $autosizeEnabled;
 134  
 135          return $this;
 136      }
 137  }