Skip to content

Commit

Permalink
Merge branch 'master' into php83
Browse files Browse the repository at this point in the history
  • Loading branch information
sprain committed Nov 23, 2023
2 parents 8944021 + b3f8308 commit d5dd4f1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 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
41 changes: 38 additions & 3 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,14 +210,25 @@ 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 = [];
if ($attributeStyles = $documentElement->getAttribute('_excel-styles')) {
if (!is_array($attributeStyles)) {
$decodedJson = json_decode($attributeStyles, true, 512, JSON_THROW_ON_ERROR);
if (null !== $decodedJson) {
$attributeStyles = $decodedJson;
try {
$attributeStyles = json_decode($attributeStyles, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException) {
}
}
}
Expand Down

0 comments on commit d5dd4f1

Please sign in to comment.