From 6f88d1b54e0f951adbfdde7cba6378c81c048896 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Wed, 16 Jun 2021 22:38:41 +0200 Subject: [PATCH] Only calculate column autosize for a cell if it contains data (#2167) Only calculate column autosize for a cell if it contains data --- src/PhpSpreadsheet/Worksheet/Worksheet.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/PhpSpreadsheet/Worksheet/Worksheet.php b/src/PhpSpreadsheet/Worksheet/Worksheet.php index 0f260291..91e4ccb9 100644 --- a/src/PhpSpreadsheet/Worksheet/Worksheet.php +++ b/src/PhpSpreadsheet/Worksheet/Worksheet.php @@ -757,15 +757,17 @@ class Worksheet implements IComparable $this->getParent()->getCellXfByIndex($cell->getXfIndex())->getNumberFormat()->getFormatCode() ); - $autoSizes[$this->cellCollection->getCurrentColumn()] = max( - (float) $autoSizes[$this->cellCollection->getCurrentColumn()], - (float) Shared\Font::calculateColumnWidth( - $this->getParent()->getCellXfByIndex($cell->getXfIndex())->getFont(), - $cellValue, - $this->getParent()->getCellXfByIndex($cell->getXfIndex())->getAlignment()->getTextRotation(), - $this->getParent()->getDefaultStyle()->getFont() - ) - ); + if ($cellValue !== null && $cellValue !== '') { + $autoSizes[$this->cellCollection->getCurrentColumn()] = max( + (float) $autoSizes[$this->cellCollection->getCurrentColumn()], + (float) Shared\Font::calculateColumnWidth( + $this->getParent()->getCellXfByIndex($cell->getXfIndex())->getFont(), + $cellValue, + $this->getParent()->getCellXfByIndex($cell->getXfIndex())->getAlignment()->getTextRotation(), + $this->getParent()->getDefaultStyle()->getFont() + ) + ); + } } } }