Is there a way to set the row of the heading beforehand? Let's say I have two Excel files, one has the heading on row 1, on hast the heading on row 25. How do I set the heading row when instantiating (new ExcelImport)->toCollection('filename.xlsx')? Using the class constructor and passing in the desired value does not work as expected.
Pass the heading row via the constructor, put it in a property and return the value in the headingRow method https://docs.laravel-excel.com/3.1/imports/heading-row.html#heading-row-on-different-row
I've tried that:
(new ContactImport(25))->toCollection($file);
<?php
namespace App\Imports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
HeadingRowFormatter::default('none');
class ContactImport implements ToCollection, WithHeadingRow, WithCalculatedFormulas, WithMultipleSheets
{
use Importable;
protected $headingRowNumber;
public function __construct($headingRowNumber)
{
$this->headingRowNumber = $headingRowNumber;
}
public function sheets(): array
{
return [
new self,
];
}
public function collection(Collection $collection)
{
}
public function headingRow(): int
{
return $this->headingRowNumber;
}
}
This throws Too few arguments to function App\Imports\ContactImport::__construct(), 0 passed and exactly 1 expected
When I dd($headingRowNumber) in the class constructor, I can see the value being passed.
public function sheets(): array
{
return [
new self,
];
}
You are not passing the heading row there
public function sheets(): array { return [ new self, ]; }You are not passing the heading row there
Dude, I've just facepalmed myself into a coma. Of course, you're right, sorry for taking up your time - THANKS!!!
Most helpful comment
You are not passing the heading row there