diff --git a/src/Exceptions/BadCountSheets.php b/src/Exceptions/BadCountSheets.php new file mode 100644 index 0000000..be6f651 --- /dev/null +++ b/src/Exceptions/BadCountSheets.php @@ -0,0 +1,13 @@ +sheet_name = $sheet_name; + + return $this; + } + /** * @return $this */ diff --git a/src/Importable.php b/src/Importable.php index c294cdf..eb48107 100644 --- a/src/Importable.php +++ b/src/Importable.php @@ -6,7 +6,10 @@ use Illuminate\Support\Str; use OpenSpout\Common\Type; use OpenSpout\Reader\Common\Creator\ReaderEntityFactory; +use OpenSpout\Reader\ReaderInterface; use OpenSpout\Reader\SheetInterface; +use Rap2hpoutre\FastExcel\Exceptions\BadCountSheets; +use Rap2hpoutre\FastExcel\Exceptions\SheetNameMissing; /** * Trait Importable. @@ -22,6 +25,11 @@ trait Importable */ private $sheet_number = 1; + /** + * @var string|null + */ + private $sheet_name = null; + /** * @param \OpenSpout\Reader\ReaderInterface|\OpenSpout\Writer\WriterInterface $reader_or_writer * @@ -33,9 +41,9 @@ abstract protected function setOptions(&$reader_or_writer); * @param string $path * @param callable|null $callback * - * @throws \OpenSpout\Common\Exception\IOException * @throws \OpenSpout\Common\Exception\UnsupportedTypeException * @throws \OpenSpout\Reader\Exception\ReaderNotOpenedException + * @throws \OpenSpout\Common\Exception\IOException * * @return Collection */ @@ -43,6 +51,10 @@ public function import($path, callable $callback = null) { $reader = $this->reader($path); + if ($this->sheet_name !== null) { + $this->setSheetIndexByName($reader); + } + foreach ($reader->getSheetIterator() as $key => $sheet) { if ($this->sheet_number != $key) { continue; @@ -58,9 +70,9 @@ public function import($path, callable $callback = null) * @param string $path * @param callable|null $callback * - * @throws \OpenSpout\Common\Exception\IOException * @throws \OpenSpout\Common\Exception\UnsupportedTypeException * @throws \OpenSpout\Reader\Exception\ReaderNotOpenedException + * @throws \OpenSpout\Common\Exception\IOException * * @return Collection */ @@ -81,11 +93,33 @@ public function importSheets($path, callable $callback = null) return new SheetCollection($collections); } + private function setSheetIndexByName(ReaderInterface $reader) + { + if (iterator_count($reader->getSheetIterator()) < 2) { + throw new BadCountSheets(); + } + + $sheetIndex = -1; + + foreach ($reader->getSheetIterator() as $key => $sheet) { + if ($sheet->getName() == $this->sheet_name) { + $sheetIndex = $key; + break; + } + } + + if ($sheetIndex === -1) { + throw new SheetNameMissing($this->sheet_name); + } + + $this->sheet($sheetIndex); + } + /** * @param $path * - * @throws \OpenSpout\Common\Exception\IOException * @throws \OpenSpout\Common\Exception\UnsupportedTypeException + * @throws \OpenSpout\Common\Exception\IOException * * @return \OpenSpout\Reader\ReaderInterface */ diff --git a/tests/FastExcelTest.php b/tests/FastExcelTest.php index 16b720b..00c2686 100644 --- a/tests/FastExcelTest.php +++ b/tests/FastExcelTest.php @@ -4,6 +4,8 @@ use OpenSpout\Common\Entity\Style\Color; use OpenSpout\Writer\Common\Creator\Style\StyleBuilder; +use Rap2hpoutre\FastExcel\Exceptions\BadCountSheets; +use Rap2hpoutre\FastExcel\Exceptions\SheetNameMissing; use Rap2hpoutre\FastExcel\FastExcel; use Rap2hpoutre\FastExcel\SheetCollection; @@ -216,4 +218,47 @@ public function testExportWithHeaderStyle() unlink($file); } + + /** + * @throws \OpenSpout\Common\Exception\IOException + * @throws \OpenSpout\Common\Exception\InvalidArgumentException + * @throws \OpenSpout\Common\Exception\UnsupportedTypeException + * @throws \OpenSpout\Reader\Exception\ReaderNotOpenedException + * @throws \OpenSpout\Writer\Exception\WriterNotOpenedException + */ + public function testImportBySheetNameXLSX() + { + $collection = (new FastExcel())->sheetName('Sheet 2')->import(__DIR__.'/testMultiSheets.xlsx'); + $this->assertEquals($this->collection(), $collection); + } + + /** + * @throws \OpenSpout\Common\Exception\IOException + * @throws \OpenSpout\Common\Exception\InvalidArgumentException + * @throws \OpenSpout\Common\Exception\UnsupportedTypeException + * @throws \OpenSpout\Reader\Exception\ReaderNotOpenedException + * @throws \OpenSpout\Writer\Exception\WriterNotOpenedException + */ + public function testSheetNameDoesNotExists() + { + $this->expectException(SheetNameMissing::class); + $this->expectExceptionMessage('Sheet name [Sheet C] is missing.'); + + (new FastExcel())->sheetName('Sheet C')->import(__DIR__.'/testMultiSheets.xlsx'); + } + + /** + * @throws \OpenSpout\Common\Exception\IOException + * @throws \OpenSpout\Common\Exception\InvalidArgumentException + * @throws \OpenSpout\Common\Exception\UnsupportedTypeException + * @throws \OpenSpout\Reader\Exception\ReaderNotOpenedException + * @throws \OpenSpout\Writer\Exception\WriterNotOpenedException + */ + public function testBadCountSheets() + { + $this->expectException(BadCountSheets::class); + $this->expectExceptionMessage('You file does not contains more than one sheet.'); + + (new FastExcel())->sheetName('Sheet C')->import(__DIR__.'/test1.xlsx'); + } } diff --git a/tests/testMultiSheets.xlsx b/tests/testMultiSheets.xlsx new file mode 100644 index 0000000..9595f26 Binary files /dev/null and b/tests/testMultiSheets.xlsx differ