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

Excel Blank Rows Calls mutateBeforeCreate (#66) #67

Merged
merged 2 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
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
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