From 483d8402dd9a7a46ef708ef4fde127c6f9fa1bbf Mon Sep 17 00:00:00 2001 From: Zuko Date: Wed, 16 Oct 2024 18:52:39 +0700 Subject: [PATCH] Fix error when handling two or more mergedColumns Rules Add $curRowItem to formatter function --- src/Traits/ExcelExportable.php | 4 ++-- src/Traits/HasExportAttributes.php | 12 ++++++++++-- src/Traits/HasExportMerging.php | 3 +-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Traits/ExcelExportable.php b/src/Traits/ExcelExportable.php index 0870fc1..acbd9d1 100644 --- a/src/Traits/ExcelExportable.php +++ b/src/Traits/ExcelExportable.php @@ -42,7 +42,7 @@ trait ExcelExportable { use HasExportAttributes { - HasExportAttributes::formatValue as parentFormat; + formatValue as parentFormat; } use HasExportMerging; @@ -317,7 +317,7 @@ protected function writeRow($sheet, $row, $rowIndex) foreach ($this->mapping as $key => $header) { if (!in_array($header, $this->hiddens, true)) { $value = $this->getValue($row, $key); - $value = $this->formatValue($header, $value); + $value = $this->formatValue($key, $value, $this->getValue($row)); $sheet->setCellValue([$columnIndex++, $rowIndex], $value); } } diff --git a/src/Traits/HasExportAttributes.php b/src/Traits/HasExportAttributes.php index 2b05d61..e227c86 100644 --- a/src/Traits/HasExportAttributes.php +++ b/src/Traits/HasExportAttributes.php @@ -78,13 +78,21 @@ public function setFormatters(array $formatters) * * @param string $mappingKey The key of the mapped column that is being exported. * @param mixed $value The value that is being exported. + * @param mixed $rowItem The current processing row item * * @return mixed The formatted value. */ - protected function formatValue($mappingKey, $value) + protected function formatValue($mappingKey, $value, $rowItem = null) { + // TODO : support for wildcard keys + if(@!$mappingKey){ + return $value; + } if (isset($this->formatters[$mappingKey])) { - return call_user_func($this->formatters[$mappingKey], [$value, $mappingKey]); + if(is_callable($this->formatters[$mappingKey])){ + return call_user_func_array($this->formatters[$mappingKey], [$value, $mappingKey, $rowItem]); + } + return $this->formatters[$mappingKey]; } if (method_exists($this, 'format' . str_replace('.', '', ucwords($mappingKey, '.')) . 'Attribute')) { return $this->{'format' . str_replace('.', '', ucwords($mappingKey, '.')) . 'Attribute'}($value); diff --git a/src/Traits/HasExportMerging.php b/src/Traits/HasExportMerging.php index d68346e..01a0547 100644 --- a/src/Traits/HasExportMerging.php +++ b/src/Traits/HasExportMerging.php @@ -78,10 +78,10 @@ protected function applyColumnMerging($sheet) { $mergedRanges = []; $hasShifted = false; + $targetRow = $this->headerRowIndex; foreach ($this->columnMergeRules as $rule) { $startColumn = $rule['start']; $endColumn = $rule['end']; - $targetRow = $this->headerRowIndex; if ($rule['shiftDown'] ?? false) { // Insert a new row above the current header row if(!$hasShifted) { @@ -92,7 +92,6 @@ protected function applyColumnMerging($sheet) } // Set the merged header value $sheet->setCellValue($startColumn . $targetRow, $rule['label']); - // Merge the cells $sheet->mergeCells($startColumn . $targetRow . ':' . $endColumn . $targetRow); $startColumn = Coordinate::columnIndexFromString($startColumn);