Skip to content

Commit 7415278

Browse files
authored
Merge pull request #117 from SpyBott/Spybott/upgrade/fromSheetName
Add function fromSheetName to read on specific spreasheet name
2 parents 999e3d5 + 42168d7 commit 7415278

3 files changed

Lines changed: 77 additions & 32 deletions

File tree

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,22 @@ If your file does not contain a header row, you should also use `noHeaderRow()`,
128128

129129
### Working with multiple sheet documents
130130

131-
Excel files can include multiple spreadsheets. You can select the sheet you want to use with the `fromSheet()` method.
131+
Excel files can include multiple spreadsheets. You can select the sheet you want to use with the `fromSheet()` method to select by index.
132132

133133
```php
134134
$rows = SimpleExcelReader::create($pathToXlsx)
135135
->fromSheet(3)
136136
->getRows();
137137
```
138138

139+
With multiple spreadsheets, you can too select the sheet you want to use with the `fromSheetName()` method to select by name.
140+
141+
```php
142+
$rows = SimpleExcelReader::create($pathToXlsx)
143+
->fromSheetName("sheet1")
144+
->getRows();
145+
```
146+
139147
#### Retrieving header row values
140148

141149
If you would like to retrieve the header row as an array, you can use the `getHeaders()` method.

src/SimpleExcelReader.php

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace Spatie\SimpleExcel;
43

54
use Illuminate\Support\LazyCollection;
@@ -14,36 +13,24 @@
1413

1514
class SimpleExcelReader
1615
{
17-
protected string $path;
1816

17+
protected string $path;
1918
protected string $type;
20-
2119
protected ReaderInterface $reader;
22-
2320
protected IteratorInterface $rowIterator;
24-
2521
protected int $sheetNumber = 1;
26-
22+
protected string $sheetName = "";
23+
protected bool $searchSheetByName = false;
2724
protected bool $processHeader = true;
28-
2925
protected bool $trimHeader = false;
30-
3126
protected bool $headersToSnakeCase = false;
32-
3327
protected ?string $trimHeaderCharacters = null;
34-
3528
protected mixed $formatHeadersUsing = null;
36-
3729
protected ?array $headers = null;
38-
3930
protected int $headerOnRow = 0;
40-
4131
protected ?array $customHeaders = [];
42-
4332
protected int $skip = 0;
44-
4533
protected int $limit = 0;
46-
4734
protected bool $useLimit = false;
4835

4936
public static function create(string $file, string $type = '')
@@ -150,7 +137,15 @@ public function fromSheet(int $sheetNumber): SimpleExcelReader
150137
{
151138
$this->sheetNumber = $sheetNumber;
152139
$this->headers = null;
140+
$this->searchSheetByName = false;
141+
return $this;
142+
}
153143

144+
public function fromSheetName(string $sheetName): SimpleExcelReader
145+
{
146+
$this->searchSheetByName = true;
147+
$this->sheetName = $sheetName;
148+
$this->headers = null;
154149
return $this;
155150
}
156151

@@ -168,22 +163,22 @@ public function getRows(): LazyCollection
168163
}
169164

170165
return LazyCollection::make(function () {
171-
while ($this->rowIterator->valid() && $this->skip && $this->skip--) {
172-
$this->rowIterator->next();
173-
}
174-
while ($this->rowIterator->valid() && (! $this->useLimit || $this->limit--)) {
175-
$row = $this->rowIterator->current();
166+
while ($this->rowIterator->valid() && $this->skip && $this->skip--) {
167+
$this->rowIterator->next();
168+
}
169+
while ($this->rowIterator->valid() && (!$this->useLimit || $this->limit--)) {
170+
$row = $this->rowIterator->current();
176171

177-
yield $this->getValueFromRow($row);
172+
yield $this->getValueFromRow($row);
178173

179-
$this->rowIterator->next();
180-
}
181-
});
174+
$this->rowIterator->next();
175+
}
176+
});
182177
}
183178

184179
public function getHeaders(): ?array
185180
{
186-
if (! $this->processHeader) {
181+
if (!$this->processHeader) {
187182
if ($this->customHeaders) {
188183
return $this->customHeaders;
189184
}
@@ -273,7 +268,7 @@ protected function trim(string $header): string
273268
{
274269
$arguments[] = $header;
275270

276-
if (! is_null($this->trimHeaderCharacters)) {
271+
if (!is_null($this->trimHeaderCharacters)) {
277272
$arguments[] = $this->trimHeaderCharacters;
278273
}
279274

@@ -296,7 +291,7 @@ protected function getValueFromRow(Row $row): array
296291

297292
$headers = $this->customHeaders ?: $this->headers;
298293

299-
if (! $headers) {
294+
if (!$headers) {
300295
return $values;
301296
}
302297

@@ -312,17 +307,33 @@ protected function getValueFromRow(Row $row): array
312307
protected function getSheet(): SheetInterface
313308
{
314309
$this->reader->open($this->path);
310+
$sheet = ($this->searchSheetByName) ? $this->getActiveSheetByName() : $this->getActiveSheetByIndex();
311+
return $sheet;
312+
}
315313

314+
protected function getActiveSheetByName(): SheetInterface
315+
{
316316
foreach ($this->reader->getSheetIterator() as $key => $sheet) {
317-
if ($key === $this->sheetNumber) {
317+
if ($this->sheetName != "" && $this->sheetName === $sheet->getName()) {
318318
break;
319319
}
320320
}
321+
if ($this->sheetName != "" && $this->sheetName !== $sheet->getName()) {
322+
throw new InvalidArgumentException("Sheet name {$this->sheetName} does not exist in {$this->path}.");
323+
}
324+
return $sheet;
325+
}
321326

327+
protected function getActiveSheetByIndex(): SheetInterface
328+
{
329+
foreach ($this->reader->getSheetIterator() as $key => $sheet) {
330+
if ($key === $this->sheetNumber) {
331+
break;
332+
}
333+
}
322334
if ($this->sheetNumber !== $key) {
323-
throw new InvalidArgumentException("Sheet {$key} does not exist in {$this->path}.");
335+
throw new InvalidArgumentException("Sheet Index {$key} does not exist in {$this->path}.");
324336
}
325-
326337
return $sheet;
327338
}
328339

tests/SimpleExcelReaderTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,4 +655,30 @@ public function it_will_not_open_non_existing_sheets()
655655
->fromSheet(3)
656656
->getHeaders();
657657
}
658+
659+
/** @test */
660+
public function it_can_select_the_sheet_of_an_excel_file_by_name()
661+
{
662+
$reader = SimpleExcelReader::create($this->getStubPath('multiple_sheets.xlsx'));
663+
664+
$this->assertEquals([
665+
0 => 'firstname',
666+
1 => 'lastname',
667+
], $reader->fromSheetName("sheet1")->getHeaders());
668+
669+
$this->assertEquals([
670+
0 => 'contact',
671+
1 => 'email',
672+
], $reader->fromSheetName("sheet2")->getHeaders());
673+
}
674+
675+
/** @test */
676+
public function it_will_not_open_non_existing_sheets_by_name()
677+
{
678+
$this->expectException(InvalidArgumentException::class);
679+
680+
SimpleExcelReader::create($this->getStubPath('multiple_sheets.xlsx'))
681+
->fromSheetName("sheetNotExists")
682+
->getHeaders();
683+
}
658684
}

0 commit comments

Comments
 (0)