Skip to content

Commit 203d0cd

Browse files
authored
Exclude heading row when limiting imported rows (#4217)
* Exclude heading row when limiting imported rows * Compare array contents instead of length * Add test for multiple limit import with heading row
1 parent 17f2d27 commit 203d0cd

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/Factories/ReaderFactory.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Maatwebsite\Excel\Concerns\MapsCsvSettings;
66
use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
7+
use Maatwebsite\Excel\Concerns\WithHeadingRow;
78
use Maatwebsite\Excel\Concerns\WithLimit;
89
use Maatwebsite\Excel\Concerns\WithReadFilter;
910
use Maatwebsite\Excel\Concerns\WithStartRow;
@@ -61,8 +62,16 @@ public static function make($import, TemporaryFile $file, ?string $readerType =
6162
if ($import instanceof WithReadFilter) {
6263
$reader->setReadFilter($import->readFilter());
6364
} elseif ($import instanceof WithLimit) {
65+
$startRow = 1;
66+
67+
if ($import instanceof WithStartRow) {
68+
$startRow = $import->startRow();
69+
} elseif ($import instanceof WithHeadingRow) {
70+
$startRow = 2;
71+
}
72+
6473
$reader->setReadFilter(new LimitFilter(
65-
$import instanceof WithStartRow ? $import->startRow() : 1,
74+
$startRow,
6675
$import->limit()
6776
));
6877
}

tests/Concerns/WithLimitTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Maatwebsite\Excel\Concerns\Importable;
77
use Maatwebsite\Excel\Concerns\ToArray;
88
use Maatwebsite\Excel\Concerns\ToModel;
9+
use Maatwebsite\Excel\Concerns\WithHeadingRow;
910
use Maatwebsite\Excel\Concerns\WithLimit;
1011
use Maatwebsite\Excel\Concerns\WithStartRow;
1112
use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
@@ -104,6 +105,72 @@ public function limit(): int
104105
$import->import('import-users.xlsx');
105106
}
106107

108+
public function test_can_import_single_with_heading_row()
109+
{
110+
$import = new class implements ToArray, WithLimit, WithHeadingRow
111+
{
112+
use Importable;
113+
114+
/**
115+
* @param array $array
116+
*/
117+
public function array(array $array)
118+
{
119+
Assert::assertEquals([
120+
[
121+
'Patrick Brouwers',
122+
'patrick@maatwebsite.nl',
123+
],
124+
], $array);
125+
}
126+
127+
/**
128+
* @return int
129+
*/
130+
public function limit(): int
131+
{
132+
return 1;
133+
}
134+
};
135+
136+
$import->import('import-users-with-headings.xlsx');
137+
}
138+
139+
public function test_can_import_multiple_with_heading_row()
140+
{
141+
$import = new class implements ToArray, WithLimit, WithHeadingRow
142+
{
143+
use Importable;
144+
145+
/**
146+
* @param array $array
147+
*/
148+
public function array(array $array)
149+
{
150+
Assert::assertEquals([
151+
[
152+
'Patrick Brouwers',
153+
'patrick@maatwebsite.nl',
154+
],
155+
[
156+
'Taylor Otwell',
157+
'taylor@laravel.com',
158+
],
159+
], $array);
160+
}
161+
162+
/**
163+
* @return int
164+
*/
165+
public function limit(): int
166+
{
167+
return 2;
168+
}
169+
};
170+
171+
$import->import('import-users-with-headings.xlsx');
172+
}
173+
107174
public function test_can_set_limit_bigger_than_row_size()
108175
{
109176
$import = new class implements ToArray, WithLimit

0 commit comments

Comments
 (0)