Format multiple cells at once

Topics: Developer Forum, User Forum
Jun 11, 2010 at 7:01 PM

I have the following code, which works perfect:

$objPHPExcel->getActiveSheet()->getStyle('A1:U1')->getFont()->setBold(true);

which works like a charm (sets the font to bold for all values in row 1 of columns A through U. However, this code:

$objPHPExcel->getActiveSheet()->getColumnDimension('A:U')->setAutoSize(true);

does not autosize columns A through U, only A. In order to achieve this I need to do the following:

for ($col = 'A'; $col != 'V'; $col++) {

$objPHPExcel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);

OR, I need to specify the setAutoSize(true) for each column.

 

Am I missing something here or is this by design? Why would a range work for one feature but not another?

Jun 26, 2010 at 10:59 AM
Edited Jun 26, 2010 at 11:02 AM

I reviewed the source code and indeed it seems that you cannot set the autosize on a range of columns.

However, you can apply one of the following workaround:


1. Change the default autosize to "true"(in Worksheet\ColumnDimension.php) as this is set to false in the constructor

2. Apply the autosize to a range of columns by providing the columns number. For your case, to set the autosize for A:U you could do something like:

for($i=1;$i<=21;$i++)

getColumnDimensionByColumn($i)->setAutoSize(true);


Hope it works.

Cheers