Skip to content

Commit

Permalink
Refactor cell merging
Browse files Browse the repository at this point in the history
  • Loading branch information
sprain committed Nov 23, 2023
1 parent 5408095 commit b006153
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions lib/HtmlPhpExcel/HtmlPhpExcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,16 @@ private function createExcel(): void
// Loop over all cells in a row
$colIndex = 1;
foreach($row->getCells() as $cell) {
$excelCellIndex = Helper::colLetter($colIndex).$rowIndex;

// Skip cells withing merge range
// Skip cells within merged range
$excelCellIndex = Helper::colLetter($colIndex).$rowIndex;
while ($this->isMerged($sheet, $excelCellIndex)) {
$colIndex++;
$excelCellIndex = Helper::colLetter($colIndex).$rowIndex;
$sheet->cell($excelCellIndex);
}

// Write cell
$cellStyles = $this->getStyles($cell);
$sheet->writeCell(
trim($cell->getValue()),
Expand All @@ -188,21 +189,16 @@ private function createExcel(): void
$sheet->addNote(Excel::cellAddress($rowIndex, $colIndex), $cellComment);
}

// Merge cells, if necessary
$colspan = $cell->getAttribute('colspan');
$rowspan = $cell->getAttribute('rowspan');

if ($colspan || $rowspan) {
if ($colspan) {
$colspan = $colspan - 1;
}

if ($rowspan) {
$rowspan = $rowspan - 1;
}
$colspan = $colspan ? $colspan - 1 : 0;
$rowspan = $rowspan ? $rowspan - 1 : 0;

$mergeCellsTargetCellIndex = Helper::colLetter($colIndex + $colspan).($rowIndex + $rowspan);

$sheet->mergeCells($excelCellIndex.':'.$mergeCellsTargetCellIndex);
$sheet->mergeCells($excelCellIndex . ':' . $mergeCellsTargetCellIndex);
}

$colIndex++;
Expand Down

0 comments on commit b006153

Please sign in to comment.