SpartnerNL / Laravel-Excel

🚀 Supercharged Excel exports and imports in Laravel
https://laravel-excel.com
MIT License
12.3k stars 1.93k forks source link

[QUESTION] Import reader spreadsheet null #2634

Closed AmanajasLG closed 3 years ago

AmanajasLG commented 4 years ago

Prerequisites

Versions

Description

Hello, I'm having a problem when importing an uploaded file. In BeforeImport event I try to check the number of lines to check if it's empty but I keep receiving this error:

exception: "Error"
file: "/var/www/html/dashboard-imprensa/app/Imports/DadosImport.php"
line: 95
message: "Call to a member function getActiveSheet() on null"

And when I checked the event reader I got this

Maatwebsite\Excel\Reader {#465
  #spreadsheet: null
  #sheetImports: []
  #currentFile: Maatwebsite\Excel\Files\LocalTemporaryFile {#469
    -filePath: "/tmp/laravel-excel-ROc4pA7Hbcr7m9PArckq66xVGbgfLEi7.xlsx"
  }
  #temporaryFileFactory: Maatwebsite\Excel\Files\TemporaryFileFactory {#463
    -temporaryPath: "/tmp"
    -temporaryDisk: null
  }...

When I remove this verification it works normally, all data is stored perfectly. Do you know why this is happening?

Additional Information

BeforeImport function

BeforeImport::class => function (BeforeImport $event) {
        dd($event->reader);
         $linhas = $event->reader->getDelegate()->getActiveSheet()->getHighestDataRow();
         if ($linhas <= 1) {
               throw new ImportExcelException('O arquivo não pode estar vazio!');
         }

         return $event;
}

Import

$header = (new HeadingRowImport)->toArray($request->file('arquivo'))[0][0];

$file = Storage::putFileAs(
       'temp',
       $request->file('arquivo'),
       $request->input('dashboard_id') . str_replace("/", "-", $request->input('ano_mes')) . '.' . pathinfo($request->file('arquivo')->getClientOriginalName())['extension']
);

if ($request->input('acao') == "import") {
      Excel::import((new DadosImport($request->input('ano_mes'), $request->input('dashboard_id'), $header, $file, true)), $file);
} else {
      Excel::import((new DadosImport($request->input('ano_mes'), $request->input('dashboard_id'), $header, $file)), $file);
}

I suspect that this is also causing validation rules not to work because when I try to upload a file with invalid data the job just fails without any notification of why.

Validation rules and messages

    public function rules(): array
    {
        return [
            'data' =>  'date_format:d/m/Y',
            'titulo' => 'required|string',
            'link' => 'required',
            'veiculo' => 'required|string',
            'uf' => 'required|string',
            'regiao' => 'required|string',
            'tipo_midia' => 'required|string',
            'cobertura' => 'required|string',
            'assunto' => 'required|string',
            'impacto' => 'required|string',
            'assunto_secundario' => 'string|nullable',
            'cita_presidente' => 'string|nullable',
            'porta_voz' => 'string|nullable',
        ];
    }

    /**
     * @return array
     */
    public function customValidationMessages()
    {
        return [
            '*.date_format' => 'A data informada na coluna ":attribute" não está no formato dd/mm/yyyy.',
            '*.required' => 'O dado na coluna ":attribute" é obrigatório.',
            '*.string' => 'O dado na coluna ":attribute" deve ser do tipo "Geral".',
        ];
    }
patrickbrouwers commented 4 years ago

Hey @AmanajasLG I don't think the sheets are available yet in the BeforeImport event, they only get loaded when the sheet import starts. You might need to use BeforeSheet instead.

AmanajasLG commented 4 years ago

Awesome :) that worked, thank you so much

stale[bot] commented 3 years ago

This bug report has been automatically closed because it has not had recent activity. If this is still an active bug, please comment to reopen. Thank you for your contributions.