Improved documentation for setting row height/column width

This commit is contained in:
MarkBaker 2022-04-19 16:42:34 +02:00
parent eabe0ca68a
commit 9275d0c59e
3 changed files with 30 additions and 12 deletions

View File

@ -1167,13 +1167,15 @@ that you are setting is measured in.
Valid units are `pt` (points), `px` (pixels), `pc` (pica), `in` (inches), Valid units are `pt` (points), `px` (pixels), `pc` (pica), `in` (inches),
`cm` (centimeters) and `mm` (millimeters). `cm` (centimeters) and `mm` (millimeters).
Setting the column width to `-1` tells MS Excel to display the column using its default width.
```php ```php
$spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(120, 'pt'); $spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(120, 'pt');
``` ```
If you want PhpSpreadsheet to perform an automatic width calculation, If you want PhpSpreadsheet to perform an automatic width calculation,
use the following code. PhpSpreadsheet will approximate the column with use the following code. PhpSpreadsheet will approximate the column width
to the width of the widest column value. to the width of the widest value displayed in that column.
```php ```php
$spreadsheet->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); $spreadsheet->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
@ -1266,6 +1268,18 @@ Valid units are `pt` (points), `px` (pixels), `pc` (pica), `in` (inches),
$spreadsheet->getActiveSheet()->getRowDimension('10')->setRowHeight(100, 'pt'); $spreadsheet->getActiveSheet()->getRowDimension('10')->setRowHeight(100, 'pt');
``` ```
Setting the row height to `-1` tells MS Excel to display the column using its default height, which is based on the character font size.
If you have wrapped text in a cell, then the `-1` default will only set the row height to display a single line of that wrapped text.
If you need to calculate the actual height for the row, then count the lines that should be displayed (count the `\n` and add 1); then adjust for the font.
The adjustment for Calibri 11 is approximately 14.5; for Calibri 12 15.9, etc.
```php
$spreadsheet->getActiveSheet()->getRowDimension(1)->setRowHeight(
14.5 * (substr_count($sheet->getCell('A1')->getValue(), "\n") + 1)
);
```
## Show/hide a row ## Show/hide a row
To set a worksheet''s row visibility, you can use the following code. To set a worksheet''s row visibility, you can use the following code.

View File

@ -83,9 +83,10 @@ class ColumnDimension extends Dimension
/** /**
* Get Width. * Get Width.
* *
* Each unit of column width is equal to the width of one character in the default font size. * Each unit of column width is equal to the width of one character in the default font size. A value of -1
* By default, this will be the return value; but this method also accepts a unit of measure argument and will * tells Excel to display this column in its default width.
* return the value converted to the specified UoM using an approximation method. * By default, this will be the return value; but this method also accepts an optional unit of measure argument
* and will convert the returned value to the specified UoM..
*/ */
public function getWidth(?string $unitOfMeasure = null): float public function getWidth(?string $unitOfMeasure = null): float
{ {
@ -97,9 +98,11 @@ class ColumnDimension extends Dimension
/** /**
* Set Width. * Set Width.
* *
* Each unit of column width is equal to the width of one character in the default font size. * Each unit of column width is equal to the width of one character in the default font size. A value of -1
* By default, this will be the unit of measure for the passed value; but this method accepts a unit of measure * tells Excel to display this column in its default width.
* argument, and will convert the value from the specified UoM using an approximation method. * By default, this will be the unit of measure for the passed value; but this method also accepts an
* optional unit of measure argument, and will convert the value from the specified UoM using an
* approximation method.
* *
* @return $this * @return $this
*/ */

View File

@ -65,8 +65,9 @@ class RowDimension extends Dimension
/** /**
* Get Row Height. * Get Row Height.
* By default, this will be in points; but this method accepts a unit of measure * By default, this will be in points; but this method also accepts an optional unit of measure
* argument, and will convert the value to the specified UoM. * argument, and will convert the value from points to the specified UoM.
* A value of -1 tells Excel to display this column in its default height.
* *
* @return float * @return float
*/ */
@ -80,8 +81,8 @@ class RowDimension extends Dimension
/** /**
* Set Row Height. * Set Row Height.
* *
* @param float $height in points * @param float $height in points. A value of -1 tells Excel to display this column in its default height.
* By default, this will be the passed argument value; but this method accepts a unit of measure * By default, this will be the passed argument value; but this method also accepts an optional unit of measure
* argument, and will convert the passed argument value to points from the specified UoM * argument, and will convert the passed argument value to points from the specified UoM
* *
* @return $this * @return $this