Skip to content

Commit

Permalink
name sheets
Browse files Browse the repository at this point in the history
  • Loading branch information
rap2hpoutre committed Sep 5, 2018
1 parent fd80781 commit 059442e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Exportable.php
Expand Up @@ -109,6 +109,9 @@ private function exportOrDownload($path, $function, callable $callback = null)
}
$writer->addRows($collection->toArray());
}
if (is_string($key)) {
$writer->getCurrentSheet()->setName($key);
}
if ($has_sheets && $data->keys()->last() !== $key) {
$writer->addNewSheetAndMakeItCurrent();
}
Expand Down
23 changes: 23 additions & 0 deletions tests/IssuesTest.php
Expand Up @@ -2,8 +2,11 @@

namespace Rap2hpoutre\FastExcel\Tests;

use Box\Spout\Common\Type;
use Box\Spout\Reader\ReaderFactory;
use Illuminate\Support\Collection;
use Rap2hpoutre\FastExcel\FastExcel;
use Rap2hpoutre\FastExcel\SheetCollection;

/**
* Class IssuesTest.
Expand Down Expand Up @@ -110,4 +113,24 @@ public function testIssue32()
$this->assertEquals($original_collection[1], $res[1]);
unlink(__DIR__.'/test2.xlsx');
}

/**
* @throws \Box\Spout\Common\Exception\IOException
* @throws \Box\Spout\Common\Exception\InvalidArgumentException
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException
*/
public function testIssue40()
{
$c = new SheetCollection(['1st Sheet' => $this->collection(), '2nd Sheet' => $this->collection()]);
(new FastExcel($c))->export(__DIR__.'/test2.xlsx');
$reader = ReaderFactory::create(Type::XLSX);
$reader->open(__DIR__.'/test2.xlsx');
foreach ($reader->getSheetIterator() as $k => $sheet) {
$this->assertEquals($sheet->getName(), $k === 2 ? '2nd Sheet' : '1st Sheet');
}
$reader->close();
unlink(__DIR__.'/test2.xlsx');
}
}

0 comments on commit 059442e

Please sign in to comment.