From d7aa0411217a05eff40cbf481243dc876165702a Mon Sep 17 00:00:00 2001 From: Josue <129712493+jnunez95@users.noreply.github.com> Date: Wed, 26 Jul 2023 17:46:10 -0500 Subject: [PATCH] Allow modify width in columns excel (#9) [skip ci] * 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.* * add width in excel[skip ci] * Update src/Exportable.php * fixed error functional test --------- Co-authored-by: Eliurkis Diaz Co-authored-by: Dariel Ramos --- src/Exportable.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Exportable.php b/src/Exportable.php index 3d5abdf..6bc32e1 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -131,6 +131,10 @@ private function exportOrDownload($path, $function, callable $callback = null) $writer->addNewSheetAndMakeItCurrent(); } } + if ($has_sheets) { + $this->resizeColumnWidths($writer->getCurrentSheet()); + } + $writer->close(); } @@ -336,4 +340,19 @@ 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 + } + } }