Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude heading row when limiting imported rows #4217

Open
wants to merge 3 commits into
base: 3.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Factories/ReaderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Maatwebsite\Excel\Concerns\MapsCsvSettings;
use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithLimit;
use Maatwebsite\Excel\Concerns\WithReadFilter;
use Maatwebsite\Excel\Concerns\WithStartRow;
Expand Down Expand Up @@ -61,8 +62,16 @@ public static function make($import, TemporaryFile $file, string $readerType = n
if ($import instanceof WithReadFilter) {
$reader->setReadFilter($import->readFilter());
} elseif ($import instanceof WithLimit) {
$startRow = 1;

if ($import instanceof WithStartRow) {
$startRow = $import->startRow();
} elseif ($import instanceof WithHeadingRow) {
$startRow = 2;
}

$reader->setReadFilter(new LimitFilter(
$import instanceof WithStartRow ? $import->startRow() : 1,
$startRow,
$import->limit()
));
}
Expand Down
67 changes: 67 additions & 0 deletions tests/Concerns/WithLimitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToArray;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithLimit;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
Expand Down Expand Up @@ -104,6 +105,72 @@ public function limit(): int
$import->import('import-users.xlsx');
}

public function test_can_import_single_with_heading_row()
{
$import = new class implements ToArray, WithLimit, WithHeadingRow
{
use Importable;

/**
* @param array $array
*/
public function array(array $array)
{
Assert::assertEquals([
[
'Patrick Brouwers',
'[email protected]',
],
], $array);
}

/**
* @return int
*/
public function limit(): int
{
return 1;
}
};

$import->import('import-users-with-headings.xlsx');
}

public function test_can_import_multiple_with_heading_row()
{
$import = new class implements ToArray, WithLimit, WithHeadingRow
{
use Importable;

/**
* @param array $array
*/
public function array(array $array)
{
Assert::assertEquals([
[
'Patrick Brouwers',
'[email protected]',
],
[
'Taylor Otwell',
'[email protected]',
],
], $array);
}

/**
* @return int
*/
public function limit(): int
{
return 2;
}
};

$import->import('import-users-with-headings.xlsx');
}

public function test_can_set_limit_bigger_than_row_size()
{
$import = new class implements ToArray, WithLimit
Expand Down
Loading