Merge pull request #1 from lijupm/active_sheet_support

Adding support for 'getActiveSheet' method
This commit is contained in:
Liju P M 2017-02-22 15:19:50 +05:30 committed by GitHub
commit 4610714005
2 changed files with 36 additions and 0 deletions

View File

@ -72,6 +72,28 @@ class SheetHelper
return $sheets;
}
/**
* Get active sheet index
*
* @return int index of active sheet
*/
public function getActiveSheetIndex()
{
$activeSheetIndex = 0;
$xmlReader = new XMLReader();
if ($xmlReader->openFileInZip($this->filePath, self::WORKBOOK_XML_FILE_PATH)) {
while ($xmlReader->read()) {
if ($xmlReader->isPositionedOnStartingNode('workbookView')) {
$activeSheetIndex = $xmlReader->getAttribute('activeTab');
}
}
$xmlReader->close();
}
return $activeSheetIndex;
}
/**
* Returns an instance of a sheet, given the XML node describing the sheet - from "workbook.xml".
* We can find the XML file path describing the sheet inside "workbook.xml.res", by mapping with the sheet ID

View File

@ -20,6 +20,9 @@ class SheetIterator implements IteratorInterface
/** @var int The index of the sheet being read (zero-based) */
protected $currentSheetIndex;
/** @var int The index of the sheet which is active */
protected $activeSheetIndex;
/**
* @param string $filePath Path of the file to be read
* @param \Box\Spout\Reader\XLSX\ReaderOptions $options Reader's current options
@ -32,12 +35,23 @@ class SheetIterator implements IteratorInterface
// Fetch all available sheets
$sheetHelper = new SheetHelper($filePath, $options, $sharedStringsHelper, $globalFunctionsHelper);
$this->sheets = $sheetHelper->getSheets();
$this->activeSheetIndex = $sheetHelper->getActiveSheetIndex();
if (count($this->sheets) === 0) {
throw new NoSheetsFoundException('The file must contain at least one sheet.');
}
}
/**
* Retrieve active sheet
*
* @return Sheet
*/
public function getActiveSheet()
{
return $this->sheets[$this->activeSheetIndex];
}
/**
* Rewind the Iterator to the first element
* @link http://php.net/manual/en/iterator.rewind.php