Skip to content

Commit

Permalink
Fix error when handling two or more mergedColumns Rules
Browse files Browse the repository at this point in the history
Add $curRowItem to formatter function
  • Loading branch information
tansautn committed Oct 16, 2024
1 parent c7622e1 commit 483d840
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Traits/ExcelExportable.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
trait ExcelExportable
{
use HasExportAttributes {
HasExportAttributes::formatValue as parentFormat;
formatValue as parentFormat;
}
use HasExportMerging;

Expand Down Expand Up @@ -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);
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/Traits/HasExportAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/Traits/HasExportMerging.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down

0 comments on commit 483d840

Please sign in to comment.