Calculate column auto width for merged cells (if horizontal, 1-column wide merge only)

This is to fix the situation when cells merged vertically are ignored in columns auto size calculations.

I also removed $calculateMergeCells param as it wasn't used and I believe the only situation when you
want to calculate the width of merged columns has been covered here (and there's no reason to not calculate it).

FIX #46
This commit is contained in:
tomaszsita 2016-11-24 10:37:51 +00:00 committed by Adrien Crivelli
parent 6d8ba6f7f0
commit 939f24ecb4
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
1 changed files with 18 additions and 4 deletions

View File

@ -701,10 +701,9 @@ class Worksheet implements IComparable
/** /**
* Calculate widths for auto-size columns * Calculate widths for auto-size columns
* *
* @param bool $calculateMergeCells Calculate merge cell width
* @return Worksheet; * @return Worksheet;
*/ */
public function calculateColumnWidths($calculateMergeCells = false) public function calculateColumnWidths()
{ {
// initialize $autoSizes array // initialize $autoSizes array
$autoSizes = []; $autoSizes = [];
@ -728,8 +727,23 @@ class Worksheet implements IComparable
foreach ($this->getCellCollection(false) as $cellID) { foreach ($this->getCellCollection(false) as $cellID) {
$cell = $this->getCell($cellID, false); $cell = $this->getCell($cellID, false);
if ($cell !== null && isset($autoSizes[$this->cellCollection->getCurrentColumn()])) { if ($cell !== null && isset($autoSizes[$this->cellCollection->getCurrentColumn()])) {
// Determine width if cell does not participate in a merge //Determine if cell is in merge range
if (!isset($isMergeCell[$this->cellCollection->getCurrentAddress()])) { $isMerged = isset($isMergeCell[$this->cellCollection->getCurrentAddress()]);
//By default merged cells should be ignored
$isMergedButProceed = false;
//The only exception is if it's a merge range value cell of a 'vertical' randge (1 column wide)
if ($isMerged && $cell->isMergeRangeValueCell()) {
$range = $cell->getMergeRange();
$rangeBoundaries = Cell::rangeDimension($range);
if ($rangeBoundaries[0] == 1) {
$isMergedButProceed = true;
}
}
// Determine width if cell does not participate in a merge or does and is a value cell of 1-column wide range
if (!$isMerged || $isMergedButProceed) {
// Calculated value // Calculated value
// To formatted string // To formatted string
$cellValue = Style\NumberFormat::toFormattedString( $cellValue = Style\NumberFormat::toFormattedString(