Skip to content

Commit

Permalink
More explicit error when sheets() method doesn't return any sheets
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickbrouwers committed Jan 16, 2024
1 parent b55a1b4 commit bedd5f6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/Exceptions/NoSheetsFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Maatwebsite\Excel\Exceptions;

use LogicException;

class NoSheetsFoundException extends LogicException implements LaravelExcelException
{

}
16 changes: 11 additions & 5 deletions src/Jobs/QueueExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Exceptions\NoSheetsFoundException;
use Maatwebsite\Excel\Exceptions\NoTypeDetectedException;
use Maatwebsite\Excel\Files\TemporaryFile;
use Maatwebsite\Excel\Jobs\Middleware\LocalizeJob;
use Maatwebsite\Excel\Writer;
Expand All @@ -30,9 +32,9 @@ class QueueExport implements ShouldQueue
private $temporaryFile;

/**
* @param object $export
* @param TemporaryFile $temporaryFile
* @param string $writerType
* @param object $export
* @param TemporaryFile $temporaryFile
* @param string $writerType
*/
public function __construct($export, TemporaryFile $temporaryFile, string $writerType)
{
Expand All @@ -52,7 +54,7 @@ public function middleware()
}

/**
* @param Writer $writer
* @param Writer $writer
*
* @throws \PhpOffice\PhpSpreadsheet\Exception
*/
Expand All @@ -66,6 +68,10 @@ public function handle(Writer $writer)
$sheetExports = $this->export->sheets();
}

if (count($sheetExports) === 0) {
throw new NoSheetsFoundException('Your export did not return any sheet export instances, please make sure your sheets() method always at least returns one instance.');
}

// Pre-create the worksheets
foreach ($sheetExports as $sheetIndex => $sheetExport) {
$sheet = $writer->addNewSheet($sheetIndex);
Expand All @@ -78,7 +84,7 @@ public function handle(Writer $writer)
}

/**
* @param Throwable $e
* @param Throwable $e
*/
public function failed(Throwable $e)
{
Expand Down

0 comments on commit bedd5f6

Please sign in to comment.