Skip to content

Commit

Permalink
Merge pull request #40 from Ticketpark/cell-merging
Browse files Browse the repository at this point in the history
Add cell merging
  • Loading branch information
sprain authored Nov 23, 2023
2 parents 5a910b2 + b006153 commit b3f8308
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"ext-json": "*",
"ext-dom": "*",
"ext-intl": "*",
"avadim/fast-excel-helper": "^1.0.4",
"avadim/fast-excel-writer": "^4.5"
},
"require-dev": {
Expand Down
5 changes: 3 additions & 2 deletions example/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@
</tr>

<tr>
<td></td>
<td></td>
<!-- Merge cells -->
<td colspan="2" rowspan="2" _excel-styles='{"text-align": "center", "vertical-align": "center"}'>Merge cells</td>

<!-- Set column dimensions -->
<td _excel-styles='{"width": 100, "height": 50}'>No need to fill all cells.</td>
<td _excel-styles='{"width": "auto"}'>And no need to care about how many cells there are in each row.</td>
Expand Down
35 changes: 35 additions & 0 deletions lib/HtmlPhpExcel/HtmlPhpExcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Ticketpark\HtmlPhpExcel;

use avadim\FastExcelHelper\Helper;
use avadim\FastExcelWriter\Excel;
use avadim\FastExcelWriter\Sheet;
use Ticketpark\HtmlPhpExcel\Elements as HtmlPhpExcelElement;
use Ticketpark\HtmlPhpExcel\Exception\InexistentExcelObjectException;
use Ticketpark\HtmlPhpExcel\Parser\Parser;
Expand Down Expand Up @@ -158,6 +160,16 @@ private function createExcel(): void
// Loop over all cells in a row
$colIndex = 1;
foreach($row->getCells() as $cell) {

// 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 @@ -177,6 +189,18 @@ 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) {
$colspan = $colspan ? $colspan - 1 : 0;
$rowspan = $rowspan ? $rowspan - 1 : 0;

$mergeCellsTargetCellIndex = Helper::colLetter($colIndex + $colspan).($rowIndex + $rowspan);
$sheet->mergeCells($excelCellIndex . ':' . $mergeCellsTargetCellIndex);
}

$colIndex++;
}

Expand All @@ -186,6 +210,17 @@ private function createExcel(): void
}
}

private function isMerged(Sheet $sheet, string $cellAddress): bool
{
foreach ($sheet->getMergedCells() as $range) {
if (Helper::inRange($cellAddress, $range)) {
return true;
}
}

return false;
}

private function getStyles(HtmlPhpExcelElement\Element $documentElement): array
{
$styles = [];
Expand Down

0 comments on commit b3f8308

Please sign in to comment.