From 0c3bb52be734e22e3aead8fa978ace60b03fd9a5 Mon Sep 17 00:00:00 2001 From: Masood Date: Mon, 7 Mar 2022 22:36:08 +0330 Subject: [PATCH 1/4] Critical changs 1- Rename some variables on importSheet() for better understanding how it works. 2- Add critical function to handle Empty or Duplicated headers. --- src/Importable.php | 53 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/src/Importable.php b/src/Importable.php index 065df78..9fcb503 100644 --- a/src/Importable.php +++ b/src/Importable.php @@ -138,27 +138,29 @@ private function importSheet(SheetInterface $sheet, callable $callback = null) $collection = []; $count_header = 0; - foreach ($sheet->getRowIterator() as $k => $rowAsObject) { - $row = $rowAsObject->toArray(); - if ($k >= $this->start_row) { + foreach ($sheet->getRowIterator() as $row => $columnsAsObject) { + $columns = $columnsAsObject->toArray(); + $count_columns = count($columns); + + if ($row >= $this->start_row) { if ($this->with_header) { - if ($k == $this->start_row) { - $headers = $this->toStrings($row); + if ($row == $this->start_row) { + $headers = $this->toStrings($columns); $count_header = count($headers); continue; } - if ($count_header > $count_row = count($row)) { - $row = array_merge($row, array_fill(0, $count_header - $count_row, null)); - } elseif ($count_header < $count_row = count($row)) { - $row = array_slice($row, 0, $count_header); + if ($count_header > $count_columns) { + $columns = array_merge($col, array_fill(0, $count_header - $count_columns, null)); + } elseif ($count_header < $count_columns) { + $columns = array_slice($col, 0, $count_header); } } if ($callback) { - if ($result = $callback(empty($headers) ? $row : array_combine($headers, $row))) { + if ($result = $callback(empty($headers) ? $col : array_combine($headers, $columns))) { $collection[] = $result; } } else { - $collection[] = empty($headers) ? $row : array_combine($headers, $row); + $collection[] = $this->checkHeaders($headers, $columns); } } } @@ -187,4 +189,33 @@ private function toStrings($values) return $values; } + + /** + * if not actived => Empty column headers skipped and their values(rows) not appear on your results + * => Duplicate headers replaced + * + * if actived => Empty column headers named by "COL_INDEX" + * => Duplicate headers rename and add "COL_INDEX" to it + * just set third parameter to False and it works as always be + * + * @param $headers + * @param $columns + * @param true $active + * @return array|mixed + */ + private function checkHeaders($headers, $columns, $active = true){ + $result = []; + if($active){ + foreach($headers as $index => $header){ + /* Name COL_XX for empty headers */ + $key = empty($header) ? 'COL_'.$index : $header; + /* Duplicate headers check and add col_index to duplicated ones */ + $key = array_key_exists($key, $result) ? $key."_".$index : $key; + $result[$key] = $columns[$index]; + } + }else{ + $result = empty($headers) ? $col : array_combine($headers, $columns); + } + return $result; + } } From cadf1444604b5a1d77dc74b85e5b13db8f7e3936 Mon Sep 17 00:00:00 2001 From: Masood Date: Mon, 7 Mar 2022 23:28:25 +0330 Subject: [PATCH 2/4] Update Importable.php --- src/Importable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Importable.php b/src/Importable.php index 9fcb503..a6723a1 100644 --- a/src/Importable.php +++ b/src/Importable.php @@ -160,7 +160,7 @@ private function importSheet(SheetInterface $sheet, callable $callback = null) $collection[] = $result; } } else { - $collection[] = $this->checkHeaders($headers, $columns); + $collection[] = $this->with_header ? $this->checkHeaders($headers, $columns) : $columns; } } } From b0c85f1c0c1e44ec9ccb35b2df6fdb0bb68ef8e7 Mon Sep 17 00:00:00 2001 From: Masood Date: Tue, 8 Mar 2022 20:19:36 +0330 Subject: [PATCH 3/4] Update Importable.php --- src/Importable.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Importable.php b/src/Importable.php index a6723a1..3fe281a 100644 --- a/src/Importable.php +++ b/src/Importable.php @@ -150,13 +150,13 @@ private function importSheet(SheetInterface $sheet, callable $callback = null) continue; } if ($count_header > $count_columns) { - $columns = array_merge($col, array_fill(0, $count_header - $count_columns, null)); + $columns = array_merge($columns, array_fill(0, $count_header - $count_columns, null)); } elseif ($count_header < $count_columns) { $columns = array_slice($col, 0, $count_header); } } if ($callback) { - if ($result = $callback(empty($headers) ? $col : array_combine($headers, $columns))) { + if ($result = $callback(empty($headers) ? $columns : array_combine($headers, $columns))) { $collection[] = $result; } } else { From fac652afecff759d0db6d8cc30a3527538cfcf1b Mon Sep 17 00:00:00 2001 From: Masood Date: Tue, 8 Mar 2022 20:28:16 +0330 Subject: [PATCH 4/4] Update Importable.php --- src/Importable.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Importable.php b/src/Importable.php index 3fe281a..474e232 100644 --- a/src/Importable.php +++ b/src/Importable.php @@ -152,7 +152,7 @@ private function importSheet(SheetInterface $sheet, callable $callback = null) if ($count_header > $count_columns) { $columns = array_merge($columns, array_fill(0, $count_header - $count_columns, null)); } elseif ($count_header < $count_columns) { - $columns = array_slice($col, 0, $count_header); + $columns = array_slice($columns, 0, $count_header); } } if ($callback) { @@ -214,7 +214,7 @@ private function checkHeaders($headers, $columns, $active = true){ $result[$key] = $columns[$index]; } }else{ - $result = empty($headers) ? $col : array_combine($headers, $columns); + $result = empty($headers) ? $columns : array_combine($headers, $columns); } return $result; }