Skip to content

Commit

Permalink
Fix: common merge columns rule when start & end rule is all alphabet
Browse files Browse the repository at this point in the history
  • Loading branch information
tansautn committed Oct 28, 2024
1 parent 5a56d0b commit 4fb6a1d
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/Traits/HasExportMerging.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ trait HasExportMerging
public function setColumnMergeRules(array $rules)
{
foreach ($rules as &$rule) {
if (isset($rule['start']) && !ctype_alpha($rule['start'])) {
if (isset($rule['start']) && (!ctype_alpha($rule['start']) || strlen($rule['start']) > 2)) {
$rule['start'] = $this->getColumnLetter($rule['start']);
}
if (isset($rule['end']) && !ctype_alpha($rule['end'])) {
if (isset($rule['end']) && (!ctype_alpha($rule['end']) || strlen($rule['end']) > 2)) {
$rule['end'] = $this->getColumnLetter($rule['end']);
}
$rule['shiftDown'] = $rule['shiftDown'] ?? false;
Expand Down Expand Up @@ -108,6 +108,7 @@ protected function applyMerging($sheet)
$this->applyColumnMerging($sheet);
$this->applyRowMerging($sheet);
}

/**
* Applies column merging rules to the given sheet.
*
Expand All @@ -128,7 +129,7 @@ protected function applyColumnMerging($sheet)
$endColumn = $rule['end'];
if ($rule['shiftDown'] ?? false) {
// Insert a new row above the current header row
if(!$hasShifted) {
if (!$hasShifted) {
$sheet->insertNewRowBefore($this->headerRowIndex);
$this->headerRowIndex++;
$targetRow = ($this->headerRowIndex - 1);
Expand All @@ -140,7 +141,7 @@ protected function applyColumnMerging($sheet)
$sheet->mergeCells($startColumn . $targetRow . ':' . $endColumn . $targetRow);
$startColumn = Coordinate::columnIndexFromString($startColumn);
$endColumn = Coordinate::columnIndexFromString($endColumn);
if(!in_array($ranges = range($startColumn, $endColumn), $mergedRanges, true)){
if (!in_array($ranges = range($startColumn, $endColumn), $mergedRanges, true)) {
$mergedRanges[] = $ranges;
}
} else {
Expand All @@ -149,10 +150,10 @@ protected function applyColumnMerging($sheet)
$sheet->setCellValue($startColumn . $this->headerRowIndex, $rule['label']);
}
}
if($hasShifted){
if ($hasShifted) {
$flat = array_merge(...$mergedRanges);
foreach ($this->headers as $index => $header) {
if(!in_array($index, $flat, true)){
if (!in_array($index, $flat, true)) {
$curLetter = Coordinate::stringFromColumnIndex($index);
$sheet->mergeCells($curLetter . $this->headerRowIndex . ':' . $curLetter . ($this->headerRowIndex - 1));
}
Expand Down

0 comments on commit 4fb6a1d

Please sign in to comment.