Streamline multi-sheet imports with advanced conditional loading
There are a few quirks with Multiple Sheet importing that can be solved with Laravel Excel. If the creator changes the format, you’re out of luck of course, and will have to make the changes to your code. However, there are a couple of more things that may be beneficial to understand, like:
- creating specific imports but only using some of them
- and understanding how to handle formulas
We’ll cover Conditional Sheet Loading in this article and handling formulas in the next one.
Using Specific Importers
You might have an excel sheet with multiple sheets that all require to be imported. So you spend the necessary time crafting the code necessary to import each sheet. Everything seems to work great. However, what happens when the data changes only on one sheet? Do you still want to import all of the other sheets or only import that one? Can that even be done? Yes it can.
First, let’s review our importer that defines the imports for all of our sheets.
<?php
namespace App\Imports;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class MultiSheetSelectSpecificSheet implements WithMultipleSheets
{
public function sheets(): array
{
return [
'user_data_1' => new FirstSheetImporter,
'user_data_2' => new SecondSheetImporter,
'user_data_3' => new ThirdSheetImporter,
];
}
}
This importer expects three different sheets to be loaded, user_data_1
, user_data_2
, and user_data_3
.
If there were changes only on user_data_1
, we could specify to only use that one. There will need to be some modifications to our code. First, we need to use the WithConditionalSheets
trait in our importer. Next, we’ll need to replace our sheets()
method with the required conditionalSheets()
method.
<?php
namespace App\Imports;
use Maatwebsite\Excel\Concerns\WithConditionalSheets;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class MultiSheetSelectSpecificSheet implements WithMultipleSheets
{
use WithConditionalSheets;
public function conditionalSheets(): array
{
return [
'user_data_1' => new FirstSheetImporter,
'user_data_2' => new SecondSheetImporter,
'user_data_3' => new ThirdSheetImporter,
];
}
}
Let’s create our route and add method to our UserController
in order to call the importer.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Artisan;
// ...
Route::get(
'/import-multiple-sheets-with-conditional-selection',
[UserController::class, 'import_multiple_sheet_conditional_selection']
);
<?php
namespace App\Http\Controllers;
//...
use App\Imports\MultiSheetSelectSpecificSheet;
use App\Models\User;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
class UserController extends Controller
{
//...
public function import_multiple_sheet_conditional_selection() {
Excel::import(
(new MultiSheetSelectSpecificSheet())->onlySheets('user_data_1'),
'mock_data/MOCK_DATA_3.xlsx'
);
return redirect('/')->with('success', 'Users Imported Successfully!');
}
}
If you’ve been following the previous articles, you’ll notice a difference in the first argument for the Excel::import
method. We needed to specify that we’re only going to be using specific sheets this time. In this example, we stated that we only wanted the user_data_1
sheet to be passed, but we could have specified multiple sheets like this: ->onlySheets('user_data_1', 'user_data_2')
.
Also, you could have written the following example like this:
$import = new MultiSheetSelectSpecificSheet();
$import->onlySheets('user_data_1');
Excel::import($import, 'mock_data/MOCK_DATA_3.xlsx');
All good to go on my end. Hopefully everything is functioning properly on yours too.
Laravel Series
Continue your Laravel Learning.
Effortlessly handle multi-sheet imports in Laravel-Excel
Laravel Excel – P3: Importing Multiple Sheets & Handling Unknown Sheets
In part three of our Laravel-Excel series, discover how to import multiple sheets while gracefully handling unknown or undefined sheets. Learn practical techniques for adapting your import process, ensuring data accuracy, and preventing import errors.
Streamline multi-sheet imports with advanced conditional loading
Laravel-Excel — P4: Importing Multiple Sheets Conditional Loading
In part four of our Laravel-Excel series, explore advanced techniques for importing multiple sheets with conditional loading. Learn how to selectively process data based on custom criteria, reducing resource usage, and optimizing overall performance.
Seamlessly process spreadsheet formulas with Laravel-Excel
Laravel-Excel — P5: Importing Sheets With Formulas
In the fifth installment of our Laravel-Excel series, dive into importing spreadsheets containing formulas. Discover how to parse and evaluate formula-based data, ensuring accurate calculations, robust data handling, and streamlined application workflows.