'php spreadsheet getWidth return -1.0

I am using Laravel-excel in a Laravel project, and I need to export an excel with an image centered in B1, and because I needed other stylings I used WithEvents, and inside the registerEvents() function I try to get the column width of B as follow $event->sheet->getDelegate()->getColumnDimension('B')->getWidth(), but I get -1.0 in return which is not correct, also I used auto width columns because I needed that as follow:

foreach($event->sheet->getDelegate()->getColumnIterator() as $col)
{
  $event->sheet->getDelegate()->getColumnDimension($col->getColumnIndex())->setAutoSize(true);
 }

Note: $event->sheet->getDelegate() will return active sheet which is equal to $spreadsheet->getActiveSheet()



Solution 1:[1]

The -1 is the value for the demention of auto with size. If you will work with it you must have something like

$autowith = sheet->getDelegate()->getColumnDimension($col->getColumnIndex());
$calc=5
if($autowith == -1){
   $calc += 22.2;
}else{
   $calc += $autowith;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Collie-IT Anne K. Frey