Skip to content

Commit

Permalink
Refactor code for headers when modify width row (#11)
Browse files Browse the repository at this point in the history
* Add ability to set columns width (#1)

* Comment code for transform always data (#2)

* Update composer.json

* Update composer.json

* Update composer.json

* Update composer.json

* Update composer.json

* Update composer.json

* Update to PHP 8.*

* refactor code modify header row

* refactor code modify header row

---------

Co-authored-by: Eliurkis Diaz <[email protected]>
Co-authored-by: Dariel Ramos <[email protected]>
  • Loading branch information
3 people authored Jul 31, 2023
1 parent 46342ff commit fbe6f0b
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions src/Exportable.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,6 @@ private function exportOrDownload($path, $function, callable $callback = null)
$writer->addNewSheetAndMakeItCurrent();
}
}
if ($has_sheets) {
$this->resizeColumnWidths($writer->getCurrentSheet());
}

$writer->close();
}
Expand Down Expand Up @@ -255,6 +252,14 @@ private function writeHeader($writer, $first_row)
: array_keys(is_array($first_row) ? $first_row : $first_row->toArray());

$writer->addRow(Row::fromValues($keys, $this->header_style));

if (!empty($this->columns_width)) {
$sheet = $writer->getCurrentSheet();

foreach (array_values($this->columns_width) as $key => $value) {
$sheet->setColumnWidth($value, $key + 1); // Excel start from column 1
}
}
// $writer->addRow(WriterEntityFactory::createRowFromArray($keys, $this->header_style));
}

Expand Down Expand Up @@ -344,19 +349,4 @@ public function setColumnsWidth($widths)

return $this;
}

/**
* @param $sheet
* @return void
*/
private function resizeColumnWidths($sheet): void
{
if (empty($this->columns_width) || empty($sheet)) {
return;
}

foreach (array_values($this->columns_width) as $key => $value) {
$sheet->setColumnWidth($value, $key + 1); // Excel start from column 1
}
}
}

0 comments on commit fbe6f0b

Please sign in to comment.