Merge de6f477c9cb4e9b7c9adcbde3510af33777a6cee into 3681a3421a868ab9a65da156c554f756541f452b

This commit is contained in:
Alexis Parrón 2017-09-25 19:44:41 +00:00 committed by GitHub
commit 9f3ccce1ad
7 changed files with 110 additions and 5 deletions

View File

@ -27,9 +27,12 @@ class SheetHelper
const XML_ATTRIBUTE_ACTIVE_TAB = 'activeTab'; const XML_ATTRIBUTE_ACTIVE_TAB = 'activeTab';
const XML_ATTRIBUTE_R_ID = 'r:id'; const XML_ATTRIBUTE_R_ID = 'r:id';
const XML_ATTRIBUTE_NAME = 'name'; const XML_ATTRIBUTE_NAME = 'name';
const XML_ATTRIBUTE_STATE = 'state';
const XML_ATTRIBUTE_ID = 'Id'; const XML_ATTRIBUTE_ID = 'Id';
const XML_ATTRIBUTE_TARGET = 'Target'; const XML_ATTRIBUTE_TARGET = 'Target';
const SHEET_STATE_VISIBLE = 'visible';
/** @var string Path of the XLSX file being read */ /** @var string Path of the XLSX file being read */
protected $filePath; protected $filePath;
@ -105,16 +108,18 @@ class SheetHelper
{ {
$sheetId = $xmlReaderOnSheetNode->getAttribute(self::XML_ATTRIBUTE_R_ID); $sheetId = $xmlReaderOnSheetNode->getAttribute(self::XML_ATTRIBUTE_R_ID);
$escapedSheetName = $xmlReaderOnSheetNode->getAttribute(self::XML_ATTRIBUTE_NAME); $escapedSheetName = $xmlReaderOnSheetNode->getAttribute(self::XML_ATTRIBUTE_NAME);
$sheetState = $xmlReaderOnSheetNode->getAttribute(self::XML_ATTRIBUTE_STATE);
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$escaper = \Box\Spout\Common\Escaper\XLSX::getInstance(); $escaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
$sheetName = $escaper->unescape($escapedSheetName); $sheetName = $escaper->unescape($escapedSheetName);
$isSheetVisible = ($sheetState === self::SHEET_STATE_VISIBLE);
$sheetDataXMLFilePath = $this->getSheetDataXMLFilePathForSheetId($sheetId); $sheetDataXMLFilePath = $this->getSheetDataXMLFilePathForSheetId($sheetId);
return new Sheet( return new Sheet(
$this->filePath, $sheetDataXMLFilePath, $this->filePath, $sheetDataXMLFilePath,
$sheetIndexZeroBased, $sheetName, $isSheetActive, $sheetIndexZeroBased, $sheetName, $isSheetActive, $isSheetVisible,
$this->options, $this->sharedStringsHelper $this->options, $this->sharedStringsHelper
); );
} }

View File

@ -24,21 +24,26 @@ class Sheet implements SheetInterface
/** @var bool Whether the sheet was the active one */ /** @var bool Whether the sheet was the active one */
protected $isActive; protected $isActive;
/** @var bool Whether the sheet is visible or not */
protected $isVisible;
/** /**
* @param string $filePath Path of the XLSX file being read * @param string $filePath Path of the XLSX file being read
* @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml * @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml
* @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based) * @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based)
* @param string $sheetName Name of the sheet * @param string $sheetName Name of the sheet
* @param bool $isSheetVisible Whether the sheet is visible or not
* @param bool $isSheetActive Whether the sheet was defined as active * @param bool $isSheetActive Whether the sheet was defined as active
* @param \Box\Spout\Reader\XLSX\ReaderOptions $options Reader's current options * @param \Box\Spout\Reader\XLSX\ReaderOptions $options Reader's current options
* @param Helper\SharedStringsHelper Helper to work with shared strings * @param Helper\SharedStringsHelper Helper to work with shared strings
*/ */
public function __construct($filePath, $sheetDataXMLFilePath, $sheetIndex, $sheetName, $isSheetActive, $options, $sharedStringsHelper) public function __construct($filePath, $sheetDataXMLFilePath, $sheetIndex, $sheetName, $isSheetActive, $isSheetVisible, $options, $sharedStringsHelper)
{ {
$this->rowIterator = new RowIterator($filePath, $sheetDataXMLFilePath, $options, $sharedStringsHelper); $this->rowIterator = new RowIterator($filePath, $sheetDataXMLFilePath, $options, $sharedStringsHelper);
$this->index = $sheetIndex; $this->index = $sheetIndex;
$this->name = $sheetName; $this->name = $sheetName;
$this->isActive = $isSheetActive; $this->isActive = $isSheetActive;
$this->isVisible = $isSheetVisible;
} }
/** /**
@ -76,4 +81,13 @@ class Sheet implements SheetInterface
{ {
return $this->isActive; return $this->isActive;
} }
/**
* @api
* @return bool Whether the sheet is visible or not
*/
public function isVisible()
{
return $this->isVisible;
}
} }

View File

@ -33,6 +33,9 @@ class Sheet
/** @var string Name of the sheet */ /** @var string Name of the sheet */
protected $name; protected $name;
/** @var bool Visibility of the sheet */
protected $isVisible;
/** @var \Box\Spout\Common\Helper\StringHelper */ /** @var \Box\Spout\Common\Helper\StringHelper */
protected $stringHelper; protected $stringHelper;
@ -50,6 +53,7 @@ class Sheet
$this->stringHelper = new StringHelper(); $this->stringHelper = new StringHelper();
$this->setName(self::DEFAULT_SHEET_NAME_PREFIX . ($sheetIndex + 1)); $this->setName(self::DEFAULT_SHEET_NAME_PREFIX . ($sheetIndex + 1));
$this->setIsVisible(true);
} }
/** /**
@ -70,6 +74,28 @@ class Sheet
return $this->name; return $this->name;
} }
/**
* @api
* @return bool isVisible visibility of the sheet
*/
public function isVisible()
{
return $this->isVisible;
}
/**
* @api
* @param string $visibility Visibility of the sheet
* @return Sheet
*/
public function setIsVisible($isVisible = true)
{
$this->isVisible = $isVisible;
return $this;
}
/** /**
* Sets the name of the sheet. Note that Excel has some restrictions on the name: * Sets the name of the sheet. Note that Excel has some restrictions on the name:
* - it should not be blank * - it should not be blank

View File

@ -289,8 +289,10 @@ EOD;
/** @var Worksheet $worksheet */ /** @var Worksheet $worksheet */
foreach ($worksheets as $worksheet) { foreach ($worksheets as $worksheet) {
$worksheetName = $worksheet->getExternalSheet()->getName(); $worksheetName = $worksheet->getExternalSheet()->getName();
$worksheetState = ( $worksheet->getExternalSheet()->isVisible() ) ? 'visible' : 'hidden';
$worksheetId = $worksheet->getId(); $worksheetId = $worksheet->getId();
$workbookXmlFileContents .= '<sheet name="' . $escaper->escape($worksheetName) . '" sheetId="' . $worksheetId . '" r:id="rIdSheet' . $worksheetId . '"/>'; $workbookXmlFileContents .= '<sheet name="' . $escaper->escape($worksheetName) . '" sheetId="' . $worksheetId . '" state="' . $worksheetState. '" r:id="rIdSheet' . $worksheetId . '"/>';
} }
$workbookXmlFileContents .= <<<EOD $workbookXmlFileContents .= <<<EOD

View File

@ -51,4 +51,17 @@ class SheetTest extends \PHPUnit_Framework_TestCase
return $sheets; return $sheets;
} }
/**
* @return void
*/
public function testReaderShouldReturnCorrectSheetVisibility()
{
$sheets = $this->openFileAndReturnSheets('two_sheets_one_hidden_one_not.xlsx');
$this->assertFalse($sheets[0]->isVisible());
$this->assertTrue($sheets[1]->isVisible());
}
} }

View File

@ -47,11 +47,12 @@ class SheetTest extends \PHPUnit_Framework_TestCase
{ {
$fileName = 'test_set_name_should_create_sheet_with_custom_name.xlsx'; $fileName = 'test_set_name_should_create_sheet_with_custom_name.xlsx';
$customSheetName = 'CustomName'; $customSheetName = 'CustomName';
$this->writeDataAndReturnSheetWithCustomName($fileName, $customSheetName); $this->writeDataToSheetWithCustomName($fileName, $customSheetName);
$this->assertSheetNameEquals($customSheetName, $fileName, "The sheet name should have been changed to '$customSheetName'"); $this->assertSheetNameEquals($customSheetName, $fileName, "The sheet name should have been changed to '$customSheetName'");
} }
/** /**
* @expectedException \Box\Spout\Writer\Exception\InvalidSheetNameException * @expectedException \Box\Spout\Writer\Exception\InvalidSheetNameException
* @return void * @return void
@ -75,12 +76,23 @@ class SheetTest extends \PHPUnit_Framework_TestCase
$sheet->setName($customSheetName); $sheet->setName($customSheetName);
} }
/**
* @return void
*/
public function testSetSheetVisibilityShouldCreateSheetHidden()
{
$fileName = 'test_set_visibility_should_create_sheet_hidden.xlsx';
$this->writeDataToHiddenSheet($fileName);
$this->assertIsHiddenSheet( $fileName, "The sheet visibility should have been changed to 'hidden'");
}
/** /**
* @param string $fileName * @param string $fileName
* @param string $sheetName * @param string $sheetName
* @return Sheet * @return Sheet
*/ */
private function writeDataAndReturnSheetWithCustomName($fileName, $sheetName) private function writeDataToSheetWithCustomName($fileName, $sheetName)
{ {
$this->createGeneratedFolderIfNeeded($fileName); $this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
@ -117,6 +129,25 @@ class SheetTest extends \PHPUnit_Framework_TestCase
return $writer->getSheets(); return $writer->getSheets();
} }
/**
* @param string $fileName
* @return void
*/
private function writeDataToHiddenSheet($fileName)
{
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
$writer = WriterFactory::create(Type::XLSX);
$writer->openToFile($resourcePath);
$sheet = $writer->getCurrentSheet();
$sheet->setIsVisible(false);
$writer->addRow(['xlsx--11', 'xlsx--12']);
$writer->close();
}
/** /**
* @param string $expectedName * @param string $expectedName
* @param string $fileName * @param string $fileName
@ -131,4 +162,18 @@ class SheetTest extends \PHPUnit_Framework_TestCase
$this->assertContains("<sheet name=\"$expectedName\"", $xmlContents, $message); $this->assertContains("<sheet name=\"$expectedName\"", $xmlContents, $message);
} }
/**
* @param string $fileName
* @param string $message
* @return void
*/
private function assertIsHiddenSheet( $fileName, $message = '')
{
$resourcePath = $this->getGeneratedResourcePath($fileName);
$pathToWorkbookFile = $resourcePath . '#xl/workbook.xml';
$xmlContents = file_get_contents('zip://' . $pathToWorkbookFile);
$this->assertContains(" state=\"hidden\"", $xmlContents, $message);
}
} }