diff --git a/src/Spout/Reader/XLSX/Helper/SheetHelper.php b/src/Spout/Reader/XLSX/Helper/SheetHelper.php index abb3b0a..d1a070b 100644 --- a/src/Spout/Reader/XLSX/Helper/SheetHelper.php +++ b/src/Spout/Reader/XLSX/Helper/SheetHelper.php @@ -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 diff --git a/src/Spout/Reader/XLSX/SheetIterator.php b/src/Spout/Reader/XLSX/SheetIterator.php index 7ba07d3..395d2b3 100644 --- a/src/Spout/Reader/XLSX/SheetIterator.php +++ b/src/Spout/Reader/XLSX/SheetIterator.php @@ -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