Skip to content

Commit

Permalink
Merge pull request #67 from jaetoole/bug/excel-blank-rows
Browse files Browse the repository at this point in the history
Excel Blank Rows Calls mutateBeforeCreate (#66)
  • Loading branch information
frankyso authored Jan 19, 2023
2 parents 0d9e39f + b0bcda6 commit ffa5b65
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ protected function getActions(): array
}
```

### Filter Out Blank Rows
If you have a spreadsheet which includes blank data [click here to see more](https://thesoftwarepro.com/excel-tips-how-to-fill-blank-cells/), you can filter these out:
```php
protected function getActions(): array
{
return [
ImportAction::make()
->handleBlankRows(true)
->fields([
ImportField::make('project')
->label('Project')
->required(),
])
];
}
```

### Field Data Mutation
you can also manipulate data from row spreadsheet before saving to model
```php
Expand Down
10 changes: 10 additions & 0 deletions src/Actions/ImportAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class ImportAction extends Action

protected bool $shouldMassCreate = true;

protected bool $shouldHandleBlankRows = false;

protected array $cachedHeadingOptions = [];

protected null|Closure $handleRecordCreation = null;
Expand Down Expand Up @@ -67,6 +69,7 @@ protected function setUp(): void
->disk('local')
->skipHeader((bool) $data['skipHeader'])
->massCreate($this->shouldMassCreate)
->handleBlankRows($this->shouldHandleBlankRows)
->mutateBeforeCreate($this->mutateBeforeCreate)
->mutateAfterCreate($this->mutateAfterCreate)
->handleRecordCreation($this->handleRecordCreation)
Expand Down Expand Up @@ -106,6 +109,13 @@ public function massCreate($shouldMassCreate = true): static
return $this;
}

public function handleBlankRows($shouldHandleBlankRows = false): static
{
$this->shouldHandleBlankRows = $shouldHandleBlankRows;

return $this;
}

/**
* @param array $fields
* @param int $columns
Expand Down
17 changes: 16 additions & 1 deletion src/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class Import

protected bool $shouldMassCreate = true;

protected bool $shouldHandleBlankRows = false;

protected ?Closure $handleRecordCreation = null;

public static function make(string $spreadsheetFilePath): self
Expand Down Expand Up @@ -93,11 +95,24 @@ public function massCreate($shouldMassCreate = true): static
return $this;
}

public function handleBlankRows($shouldHandleBlankRows = false): static
{
$this->shouldHandleBlankRows = $shouldHandleBlankRows;

return $this;
}

public function getSpreadsheetData(): Collection
{
return $this->toCollection(new UploadedFile(Storage::disk($this->disk)->path($this->spreadsheet), $this->spreadsheet))
$data = $this->toCollection(new UploadedFile(Storage::disk($this->disk)->path($this->spreadsheet), $this->spreadsheet))
->first()
->skip((int) $this->shouldSkipHeader);
if (!$this->shouldHandleBlankRows) {
return $data;
}
return $data->filter(function ($row) {
return $row->filter()->isNotEmpty();
});
}

public function validated($data, $rules, $customMessages, $line)
Expand Down

0 comments on commit ffa5b65

Please sign in to comment.