Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Headers (empty or duplicated header) #259

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions src/Importable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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($columns, array_fill(0, $count_header - $count_columns, null));
} elseif ($count_header < $count_columns) {
$columns = array_slice($columns, 0, $count_header);
}
}
if ($callback) {
if ($result = $callback(empty($headers) ? $row : array_combine($headers, $row))) {
if ($result = $callback(empty($headers) ? $columns : array_combine($headers, $columns))) {
$collection[] = $result;
}
} else {
$collection[] = empty($headers) ? $row : array_combine($headers, $row);
$collection[] = $this->with_header ? $this->checkHeaders($headers, $columns) : $columns;
}
}
}
Expand Down Expand Up @@ -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) ? $columns : array_combine($headers, $columns);
}
return $result;
}
}