11<?php
2-
32namespace Spatie \SimpleExcel ;
43
54use Illuminate \Support \LazyCollection ;
1413
1514class 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
0 commit comments