Sheet visibility - XLSX writer and reader

This commit is contained in:
Adrien Loison committed 2017-11-11 11:11:47 +01:00
1 parent ddfa40e8b3
commit b9206fcb4b
8 files changed
+102 -8

No files matched your search

@@ -53,6 +53,7 @@ class EntityFactory implements EntityFactoryInterface
* @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based)
* @param string $sheetName Name of the sheet
* @param bool $isSheetActive Whether the sheet was defined as active
* @param bool $isSheetVisible Whether the sheet is visible
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param SharedStringsManager $sharedStringsManager Manages shared strings
* @return Sheet
@@ -63,12 +64,13 @@ class EntityFactory implements EntityFactoryInterface
$sheetIndex,
$sheetName,
$isSheetActive,
$isSheetVisible,
$optionsManager,
$sharedStringsManager
) {
$rowIterator = $this->createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsManager);
return new Sheet($rowIterator, $sheetIndex, $sheetName, $isSheetActive);
return new Sheet($rowIterator, $sheetIndex, $sheetName, $isSheetActive, $isSheetVisible);
}
/**
@@ -29,9 +29,13 @@ class SheetManager
const XML_ATTRIBUTE_ACTIVE_TAB = 'activeTab';
const XML_ATTRIBUTE_R_ID = 'r:id';
const XML_ATTRIBUTE_NAME = 'name';
const XML_ATTRIBUTE_STATE = 'state';
const XML_ATTRIBUTE_ID = 'Id';
const XML_ATTRIBUTE_TARGET = 'Target';
/** State value to represent a hidden sheet */
const SHEET_STATE_HIDDEN = 'hidden';
/** @var string Path of the XLSX file being read */
protected $filePath;
@@ -163,6 +167,10 @@ class SheetManager
protected function getSheetFromSheetXMLNode($xmlReaderOnSheetNode, $sheetIndexZeroBased, $isSheetActive)
{
$sheetId = $xmlReaderOnSheetNode->getAttribute(self::XML_ATTRIBUTE_R_ID);
$sheetState = $xmlReaderOnSheetNode->getAttribute(self::XML_ATTRIBUTE_STATE);
$isSheetVisible = ($sheetState !== self::SHEET_STATE_HIDDEN);
$escapedSheetName = $xmlReaderOnSheetNode->getAttribute(self::XML_ATTRIBUTE_NAME);
$sheetName = $this->escaper->unescape($escapedSheetName);
@@ -174,6 +182,7 @@ class SheetManager
$sheetIndexZeroBased,
$sheetName,
$isSheetActive,
$isSheetVisible,
$this->optionsManager,
$this->sharedStringsManager
);
+14 -1
View File
@@ -22,18 +22,23 @@ class Sheet implements SheetInterface
/** @var bool Whether the sheet was the active one */
protected $isActive;
/** @var bool Whether the sheet is visible */
protected $isVisible;
/**
* @param RowIterator $rowIterator The corresponding row iterator
* @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based)
* @param string $sheetName Name of the sheet
* @param bool $isSheetActive Whether the sheet was defined as active
* @param bool $isSheetVisible Whether the sheet is visible
*/
public function __construct($rowIterator, $sheetIndex, $sheetName, $isSheetActive)
public function __construct($rowIterator, $sheetIndex, $sheetName, $isSheetActive, $isSheetVisible)
{
$this->rowIterator = $rowIterator;
$this->index = $sheetIndex;
$this->name = $sheetName;
$this->isActive = $isSheetActive;
$this->isVisible = $isSheetVisible;
}
/**
@@ -67,4 +72,12 @@ class Sheet implements SheetInterface
{
return $this->isActive;
}
/**
* @return bool Whether the sheet is visible
*/
public function isVisible()
{
return $this->isVisible;
}
}
+23
View File
@@ -21,6 +21,9 @@ class Sheet
/** @var string Name of the sheet */
private $name;
/** @var bool Visibility of the sheet */
private $isVisible;
/** @var SheetManager Sheet manager */
private $sheetManager;
@@ -38,6 +41,7 @@ class Sheet
$this->sheetManager->markWorkbookIdAsUsed($associatedWorkbookId);
$this->setName(self::DEFAULT_SHEET_NAME_PREFIX . ($sheetIndex + 1));
$this->setIsVisible(true);
}
/**
@@ -85,4 +89,23 @@ class Sheet
return $this;
}
/**
* @return bool isVisible Visibility of the sheet
*/
public function isVisible()
{
return $this->isVisible;
}
/**
* @param bool $isVisible Visibility of the sheet
* @return Sheet
*/
public function setIsVisible($isVisible)
{
$this->isVisible = $isVisible;
return $this;
}
}
@@ -307,8 +307,9 @@ EOD;
/** @var Worksheet $worksheet */
foreach ($worksheets as $worksheet) {
$worksheetName = $worksheet->getExternalSheet()->getName();
$worksheetVisibility = $worksheet->getExternalSheet()->isVisible() ? 'visible' : 'hidden';
$worksheetId = $worksheet->getId();
$workbookXmlFileContents .= '<sheet name="' . $this->escaper->escape($worksheetName) . '" sheetId="' . $worksheetId . '" r:id="rIdSheet' . $worksheetId . '"/>';
$workbookXmlFileContents .= '<sheet name="' . $this->escaper->escape($worksheetName) . '" sheetId="' . $worksheetId . '" r:id="rIdSheet' . $worksheetId . '" state="' . $worksheetVisibility. '"/>';
}
$workbookXmlFileContents .= <<<'EOD'