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