Skip to content

Commit 80c2fd1

Browse files
authored
Merge pull request #192 from amit-roy1999/feature/get-sheet-names
Added getSheets function to get a list of avilabel sheet names.
2 parents 0c4dbf1 + fbe9210 commit 80c2fd1

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ $rows = SimpleExcelReader::create($pathToXlsx)
125125
->getRows();
126126
```
127127

128+
You can also retrieve all sheet names in a document using the `getSheetNames()` method.
129+
130+
```php
131+
$sheets = SimpleExcelReader::create($pathToXlsx)
132+
->getSheetNames();
133+
```
134+
135+
128136
With multiple spreadsheets, you can too select the sheet you want to use with the `fromSheetName()` method to select by name.
129137

130138
```php

src/SimpleExcelReader.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,21 @@ public function take(int $count): SimpleExcelReader
187187
return $this;
188188
}
189189

190+
public function getSheetNames(): array
191+
{
192+
$this->setReader();
193+
194+
$this->reader->open($this->path);
195+
196+
$sheets = [];
197+
198+
foreach ($this->reader->getSheetIterator() as $sheet) {
199+
$sheets[] = $sheet->getName();
200+
}
201+
202+
return $sheets;
203+
}
204+
190205
public function hasSheet(string $sheetName): bool
191206
{
192207
$this->setReader();

tests/SimpleExcelReaderTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,17 @@ function () {
566566
]);
567567
});
568568

569+
it('can get multiple sheet names', function () {
570+
$reader = SimpleExcelReader::create(getStubPath('multiple_sheets.xlsx'));
571+
572+
expect(
573+
$reader->getSheetNames()
574+
)->toEqual([
575+
0 => 'sheet1',
576+
1 => 'sheet2',
577+
]);
578+
});
579+
569580
it('will not open non-existing sheets', function () {
570581
SimpleExcelReader::create(getStubPath('multiple_sheets.xlsx'))
571582
->fromSheet(3)

0 commit comments

Comments
 (0)