From e8aa00e12159a116be186062703e02bbe4e7b370 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 7 Mar 2022 22:31:07 -0300 Subject: [PATCH] FIX: columns --- app/Service/Informe/Informe.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/Service/Informe/Informe.php b/app/Service/Informe/Informe.php index 25660b1..ea9ecca 100644 --- a/app/Service/Informe/Informe.php +++ b/app/Service/Informe/Informe.php @@ -12,9 +12,24 @@ class Informe $this->spreadsheet = new Spreadsheet(); return $this; } + protected function formatArray(array $data) + { + $maxCols = 0; + foreach ($data as $row) { + if (count($row) > $maxCols) { + $maxCols = count($row); + } + } + foreach ($data as &$row) { + if (count($row) < $maxCols) { + $row = array_merge($row, array_fill(0, $maxCols - count($row), '')); + } + } + return $data; + } public function addArray(array $data, string $start = 'A1') { - $this->spreadsheet->getActiveSheet()->fromArray($data, null, $start); + $this->spreadsheet->getActiveSheet()->fromArray($this->formatArray($data), null, $start); return $this; } public function send(string $filename)