Skip to content

Commit

Permalink
updating: adding possibility to make sheet active
Browse files Browse the repository at this point in the history
adding: unit test for active sheet
updating: unit tests for add sheet now also handling names and counts for spout
  • Loading branch information
koddistortion committed Feb 3, 2021
1 parent f2ae848 commit 9eac9c2
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/StingerSoft/ExcelCreator/ConfiguredExcelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace StingerSoft\ExcelCreator;

use Doctrine\Common\Collections\Collection;
use Exception;

interface ConfiguredExcelInterface {

Expand All @@ -33,6 +34,14 @@ public function addSheet(string $title): ConfiguredSheetInterface;
*/
public function getSheets(): Collection;

/**
* Set the given sheet to be the active one.
*
* @param ConfiguredSheetInterface $sheet the sheet to make active
* @throws Exception in case the sheet cannot be found or cannot be made active
*/
public function setActiveSheet(ConfiguredSheetInterface $sheet): void;

/**
* Returns the title of this excel file
*
Expand Down
4 changes: 4 additions & 0 deletions src/StingerSoft/ExcelCreator/ConfiguredSheetInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ public function getGroupByBinding(): ?ColumnBinding;
*/
public function setGroupByBinding(?ColumnBinding $groupByBinding = null): ConfiguredSheetInterface;

/**
* @return mixed
*/
public function getSourceSheet();
}
20 changes: 20 additions & 0 deletions src/StingerSoft/ExcelCreator/Spout/ConfiguredExcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
use Box\Spout\Writer\Exception\SheetNotFoundException;
use Box\Spout\Writer\Exception\WriterNotOpenedException;
use Box\Spout\Writer\WriterMultiSheetsAbstract;
use Box\Spout\Writer\XLSX\Writer;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Exception;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use StingerSoft\ExcelCreator\ConfiguredExcelInterface;
use StingerSoft\ExcelCreator\ConfiguredSheetInterface;
use StingerSoft\ExcelCreator\Helper;
Expand Down Expand Up @@ -85,6 +88,13 @@ public function addSheet(string $title): ConfiguredSheetInterface {
return $sheet;
}

public function setActiveSheet(ConfiguredSheetInterface $sheet): void {
$sourceSheet = $sheet->getSourceSheet();
if($sourceSheet instanceof \Box\Spout\Writer\Common\Entity\Sheet) {
$this->phpExcel->setCurrentSheet($sourceSheet);
}
}

/**
* @param Sheet $sheet
* @param array $rowData
Expand Down Expand Up @@ -168,6 +178,16 @@ public function setCompany(?string $company = null): ConfiguredExcelInterface {
return $this;
}


/**
* Returns the underlying Writer object
*
* @return Writer The underlying Writer object
*/
public function getPhpExcel(): Writer {
return $this->phpExcel;
}

/**
* @inheritDoc
*/
Expand Down
7 changes: 7 additions & 0 deletions src/StingerSoft/ExcelCreator/Spout/ConfiguredSheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ public function setData($data): ConfiguredSheetInterface {
return $this;
}

/**
* @inheritDoc
*/
public function getSourceSheet() : Sheet {
return $this->sheet;
}

/**
* @inheritDoc
* @throws IOException
Expand Down
11 changes: 11 additions & 0 deletions src/StingerSoft/ExcelCreator/Spreadsheet/ConfiguredExcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ public function getSheets(): Collection {
return $this->sheets;
}

/**
* @inheritDoc
*/
public function setActiveSheet(ConfiguredSheetInterface $sheet): void {
$index = $this->sheets->indexOf($sheet);
if($index === false) {
return;
}
$this->phpExcel->setActiveSheetIndex($index);
}

/**
* @inheritDoc
*/
Expand Down
8 changes: 8 additions & 0 deletions src/StingerSoft/ExcelCreator/Spreadsheet/ConfiguredSheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace StingerSoft\ExcelCreator\Spreadsheet;

use Box\Spout\Writer\Common\Entity\Sheet;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
Expand Down Expand Up @@ -174,6 +175,13 @@ public function setData($data): ConfiguredSheetInterface {
return $this;
}

/**
* @inheritDoc
*/
public function getSourceSheet(): Worksheet {
return $this->sheet;
}

/**
* @inheritDoc
* @throws Exception
Expand Down
36 changes: 34 additions & 2 deletions tests/StingerSoft/ExcelCreator/ConfiguredExcelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,52 @@ public function testAddSheet(): void {
self::assertCount(0, $excel->getSheets());
if($this->getImplementation() === ExcelFactory::TYPE_PHP_SPREADSHEET) {
self::assertCount(1, $excel->getPhpExcel()->getAllSheets());
} elseif($this->getImplementation() === ExcelFactory::TYPE_SPOUT) {
self::assertCount(1, $excel->getPhpExcel()->getSheets());
}

$sheet = $excel->addSheet('TestSheet');
self::assertCount(1, $excel->getSheets());
if($this->getImplementation() === ExcelFactory::TYPE_PHP_SPREADSHEET) {
self::assertCount(1, $excel->getPhpExcel()->getAllSheets());
self::assertEquals('TestSheet', $sheet->getSheet()->getTitle());
self::assertEquals('TestSheet', $sheet->getSourceSheet()->getTitle());
} elseif($this->getImplementation() === ExcelFactory::TYPE_SPOUT) {
self::assertCount(1, $excel->getPhpExcel()->getSheets());
self::assertEquals('TestSheet', $sheet->getSourceSheet()->getName());
}

$sheet = $excel->addSheet('TestSheet2');
self::assertCount(2, $excel->getSheets());
if($this->getImplementation() === ExcelFactory::TYPE_PHP_SPREADSHEET) {
self::assertCount(2, $excel->getPhpExcel()->getAllSheets());
self::assertEquals('TestSheet2', $sheet->getSheet()->getTitle());
self::assertEquals('TestSheet2', $sheet->getSourceSheet()->getTitle());
} elseif($this->getImplementation() === ExcelFactory::TYPE_SPOUT) {
self::assertCount(2, $excel->getPhpExcel()->getSheets());
self::assertEquals('TestSheet2', $sheet->getSourceSheet()->getName());
}
}

public function testSetActiveSheet() : void {
$excel = ExcelFactory::createConfiguredExcel($this->getImplementation());

self::assertCount(0, $excel->getSheets());

$sheet1 = $excel->addSheet('TestSheet');
self::assertCount(1, $excel->getSheets());

$sheet2 = $excel->addSheet('TestSheet2');
self::assertCount(2, $excel->getSheets());

if($this->getImplementation() === ExcelFactory::TYPE_PHP_SPREADSHEET) {
// in spreadsheet newly added sheets are NOT marked as active
self::assertSame($excel->getPhpExcel()->getActiveSheet(), $sheet1->getSourceSheet());
$excel->setActiveSheet($sheet2);
self::assertSame($excel->getPhpExcel()->getActiveSheet(), $sheet2->getSourceSheet());
} elseif($this->getImplementation() === ExcelFactory::TYPE_SPOUT) {
// in spout newly added sheets are marked as active
self::assertSame($excel->getPhpExcel()->getCurrentSheet(), $sheet2->getSourceSheet());
$excel->setActiveSheet($sheet1);
self::assertSame($excel->getPhpExcel()->getCurrentSheet(), $sheet1->getSourceSheet());
}
}
}

0 comments on commit 9eac9c2

Please sign in to comment.