When I try to work with multiple sheets I can't delete some specific sheets and use a dynamic importer for the remaining sheets.
Ex: I want to remove the first 2 sheets and the remaining sheets are dynamic, they can vary between 1 and 100 for example.
And also problems when validating multiple sheets. In general, more robust support for dynamic multiple sheets.
Trying to dynamically obtain and process sheets, ignoring the first 2 sheets for example (Remembering that the total number of remaining sheets is dynamic). And try to validate with errors on 2 sheets, only the first sheet will get Exception.
<?php
namespace App\Imports;
use App\Models\SpreadsheetImport;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\BeforeImport;
class ProductDemoImport implements WithMultipleSheets, WithEvents
{
private array $dynamicSheets = [];
private SpreadsheetImport $spreadsheetImport;
public function __construct(SpreadsheetImport $spreadsheetImport)
{
$this->spreadsheetImport = $spreadsheetImport;
}
public function sheets(): array
{
return [
'RESUMO' => new ProductDemoImport($this->spreadsheetImport),
...$this->dynamicSheets,
];
}
public function registerEvents(): array
{
return [
// This code trying to get dynamic sheets, but executed after sheets, I need this code before sheets
BeforeImport::class => function (BeforeImport $event) {
$spreadsheet = $event->getReader()->getSpreadsheet();
$sheetNames = $spreadsheet->getSheetNames();
foreach ($sheetNames as $sheetName) {
$this->dynamicSheets[$sheetName] = new ProductSheetImport($this->spreadsheetImport);
}
},
];
}
}
Be able to ignore unwanted sheets and dynamically import the rest. It would also be interesting to have better support for exceptions. Once a sheet fails, it checks the others, maintains the transaction and, at the end, in ValidateException, returns the result of all validations. Finally, more complete support for multiple sheets (such as bringing the names of the sheets). Import (Success) should only occur after the entire spreadsheet has been validated and not after each sheet has been validated.