Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import by sheet name #285

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Exceptions/BadCountSheets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rap2hpoutre\FastExcel\Exceptions;

use Exception;

class BadCountSheets extends Exception
{
public function __construct()
{
parent::__construct('You file does not contains more than one sheet.');
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/SheetNameMissing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rap2hpoutre\FastExcel\Exceptions;

use Exception;

class SheetNameMissing extends Exception
{
public function __construct(string $sheetName)
{
parent::__construct("Sheet name [$sheetName] is missing.");
}
}
12 changes: 12 additions & 0 deletions src/FastExcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ public function sheet($sheet_number)
return $this;
}

/**
* @param $sheet_name
*
* @return $this
*/
public function sheetName($sheet_name)
{
$this->sheet_name = $sheet_name;

return $this;
}

/**
* @return $this
*/
Expand Down
40 changes: 37 additions & 3 deletions src/Importable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
*
Expand All @@ -33,16 +41,20 @@ 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
*/
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;
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand Down
45 changes: 45 additions & 0 deletions tests/FastExcelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
}
}
Binary file added tests/testMultiSheets.xlsx
Binary file not shown.