how to skip empty sheets when import excel file? #3417
Replies: 3 comments
-
I have this issue too. in ServiceProvider public function boot()
{
Reader::macro('getSheetImports', function (Reader $reader) {
return $reader->sheetImports;
});
Reader::macro('unsetSheetImport', function (Reader $reader, string $index) {
unset($this->sheetImports[$index]);
});
Reader::macro('getImportSheet', function (Reader $reader, $import, $sheetImport, $index) {
return $this->getSheet($import, $sheetImport, $index);
});
} in Import implement WithMultipleSheets, WithEvents public function registerEvents(): array
{
return [
// preload sheets and filter out empty sheets
BeforeImport::class => function (BeforeImport $event) {
$reader = $event->getReader();
$importer = $event->getConcernable();
foreach ($reader->getSheetImports() as $index => $sheetImport) {
$sheet = $reader->getImportSheet($importer, $sheetImport, $index);
if ($sheet && $sheet->getHighestRow() <= $this->sheets[$index]->headingRow()) {
$reader->unsetSheetImport($index);
}
}
}
];
} But I think the better way is catching exception of each import in |
Beta Was this translation helpful? Give feedback.
-
Solution that worked for me:
` In Import Class use
|
Beta Was this translation helpful? Give feedback.
-
This solution is for those who are referencing sheets by name and not an index.
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Prerequisites
[X] Have read and understood: https://github.com/Maatwebsite/Laravel-Excel/blob/3.1/.github/SUPPORT.md
[X] Checked if your Laravel Excel version is still supported: https://docs.laravel-excel.com/3.1/getting-started/support.html#supported-versions
[X] Checked that your question isn't already asked before.
[X] Filled in the entire issue template
Versions
PHP version: v7.4.11
Laravel version: 7.30.4
Package version: 3.1.19
So I have the following problem:
when I import a excel file with 3 sheets , and one of them or more is empty , I get the following error message:
this is my importer:
``
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
use Maatwebsite\Excel\Row;
use Maatwebsite\Excel\Concerns\OnEachRow;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\WithBatchInserts;//
use Maatwebsite\Excel\Concerns\WithChunkReading;//
use Maatwebsite\Excel\Validators\Failure;
use Maatwebsite\Excel\Concerns\SkipsOnError;
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\SkipsErrors;
use Maatwebsite\Excel\Concerns\RemembersRowNumber;//
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithMappedCells;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\WithConditionalSheets;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Illuminate\Support\Collection;
use Carbon\Carbon;
use DB;
class UploadInventarioPrediosSVD implements
WithHeadingRow,WithValidation,SkipsOnFailure
,OnEachRow,WithMultipleSheets,SkipsEmptyRows
{
is there a way to skip empty sheets on onRow method?
Beta Was this translation helpful? Give feedback.
All reactions