getActiveSheet()->getColumnDimensions() returns empty array

Jan 29, 2009 at 8:20 AM
Edited Jan 29, 2009 at 8:25 AM
Hello.
As in topic.
When I call $objPHPExcel->getActiveSheet()->getColumnDimension('A'), I get data,
but calling objPHPExcel->getActiveSheet()->getColumnDimensions() returns empty array.

Do You maybe know why?

I want this to create code which will auto size all columns at once.
Something like this:

foreach ($this->objPHPExcel->getActiveSheet()->getColumnDimensions() as $dimension) {
            $dimension->setAutoSize(true);
        }
Coordinator
Jan 29, 2009 at 8:43 AM
$columnID = 'A';
$lastColumn = $worksheet->getHighestColumn();
do {
   $worksheet->getColumnDimension($columnID)->setAutoSize(true);
   $columnID++;
} while ($columnID != $lastColumn);
Jan 29, 2009 at 9:18 AM
Thanks for Your quick reply.

This solution works, but still size isn't corretc.

Is there way to do this better?

http://antczak.org/bug/screenshot1233220096.jpg
Developer
Jan 30, 2009 at 1:47 AM
You're right. AutoSize isn't very accurate in PHPExcel. But it will be very difficult to get it 100% accurate in PHPExcel. We would need to implement a full Excel rendering engine that can measure preferred column width on the basis of individual glyph widths, font family, and font size. I think this feature is not coming in the near future. However, we should try to improve setAutoSize(). We can probably do it a bit better than now!

Meanwhile, I must suggest that you set column widths to fixed sizes. Make them e.g. a bit wider than expected maximum width. That is the best I can suggest right now.