ColumnDimension.php 3.3 KB

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