Skip to content

Commit 9275d0c

Browse files
author
MarkBaker
committed
Improved documentation for setting row height/column width
1 parent eabe0ca commit 9275d0c

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

docs/topics/recipes.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,13 +1167,15 @@ that you are setting is measured in.
11671167
Valid units are `pt` (points), `px` (pixels), `pc` (pica), `in` (inches),
11681168
`cm` (centimeters) and `mm` (millimeters).
11691169

1170+
Setting the column width to `-1` tells MS Excel to display the column using its default width.
1171+
11701172
```php
11711173
$spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(120, 'pt');
11721174
```
11731175

11741176
If you want PhpSpreadsheet to perform an automatic width calculation,
1175-
use the following code. PhpSpreadsheet will approximate the column with
1176-
to the width of the widest column value.
1177+
use the following code. PhpSpreadsheet will approximate the column width
1178+
to the width of the widest value displayed in that column.
11771179

11781180
```php
11791181
$spreadsheet->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
@@ -1266,6 +1268,18 @@ Valid units are `pt` (points), `px` (pixels), `pc` (pica), `in` (inches),
12661268
$spreadsheet->getActiveSheet()->getRowDimension('10')->setRowHeight(100, 'pt');
12671269
```
12681270

1271+
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.
1272+
1273+
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.
1274+
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.
1275+
The adjustment for Calibri 11 is approximately 14.5; for Calibri 12 15.9, etc.
1276+
```php
1277+
$spreadsheet->getActiveSheet()->getRowDimension(1)->setRowHeight(
1278+
14.5 * (substr_count($sheet->getCell('A1')->getValue(), "\n") + 1)
1279+
);
1280+
```
1281+
1282+
12691283
## Show/hide a row
12701284

12711285
To set a worksheet''s row visibility, you can use the following code.

src/PhpSpreadsheet/Worksheet/ColumnDimension.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ public function setColumnNumeric(int $index): self
8383
/**
8484
* Get Width.
8585
*
86-
* Each unit of column width is equal to the width of one character in the default font size.
87-
* By default, this will be the return value; but this method also accepts a unit of measure argument and will
88-
* return the value converted to the specified UoM using an approximation method.
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..
8990
*/
9091
public function getWidth(?string $unitOfMeasure = null): float
9192
{
@@ -97,9 +98,11 @@ public function getWidth(?string $unitOfMeasure = null): float
9798
/**
9899
* Set Width.
99100
*
100-
* Each unit of column width is equal to the width of one character in the default font size.
101-
* By default, this will be the unit of measure for the passed value; but this method accepts a unit of measure
102-
* argument, and will convert the value from the specified UoM using an approximation method.
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.
103106
*
104107
* @return $this
105108
*/

src/PhpSpreadsheet/Worksheet/RowDimension.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ public function setRowIndex(int $index)
6565

6666
/**
6767
* Get Row Height.
68-
* By default, this will be in points; but this method accepts a unit of measure
69-
* argument, and will convert the value to the specified UoM.
68+
* By default, this will be in points; but this method also accepts an optional unit of measure
69+
* argument, and will convert the value from points to the specified UoM.
70+
* A value of -1 tells Excel to display this column in its default height.
7071
*
7172
* @return float
7273
*/
@@ -80,8 +81,8 @@ public function getRowHeight(?string $unitOfMeasure = null)
8081
/**
8182
* Set Row Height.
8283
*
83-
* @param float $height in points
84-
* By default, this will be the passed argument value; but this method accepts a unit of measure
84+
* @param float $height in points. A value of -1 tells Excel to display this column in its default height.
85+
* By default, this will be the passed argument value; but this method also accepts an optional unit of measure
8586
* argument, and will convert the passed argument value to points from the specified UoM
8687
*
8788
* @return $this

0 commit comments

Comments
 (0)