SpartnerNL/Laravel-Excel

[Bug]: Exclude specific sheets in multiple sheets

Fechada

#4.299 aberto em 14 de mai. de 2025

 (6 comentários) (0 reação) (0 responsável)PHP (1.971 forks)batch import
help wantedproposal

Métricas do repositório

Stars
 (12.657 estrelas)
Métricas de merge de PR
 (Mesclagem média 7d 17h) (6 fundiu PRs em 30d)

Description

Is the bug applicable and reproducable to the latest version of the package and hasn't it been reported before?

  • Yes, it's still reproducable

What version of Laravel Excel are you using?

3.1.64

What version of Laravel are you using?

12.13.0

What version of PHP are you using?

8.3.21

Describe your issue

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.

How can the issue be reproduced?

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);
                }
            },
        ];
    }
}

What should be the expected behaviour?

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.

Guia do colaborador