Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
Add support for empty sheets
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien Loison committed Jan 26, 2015
1 parent 5e19900 commit 6bc9a18
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Spout/Reader/XLSX.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,11 @@ protected function read()
while ($this->xmlReader->read()) {
if ($this->xmlReader->nodeType == \XMLReader::ELEMENT && $this->xmlReader->name === 'dimension') {
// Read dimensions of the worksheet
$dimensionRef = $this->xmlReader->getAttribute('ref'); // returns 'A1:M13' for instance
list(, $lastCellIndex) = explode(':', $dimensionRef);
$this->numberOfColumns = CellHelper::getColumnIndexFromCellIndex($lastCellIndex) + 1;
$dimensionRef = $this->xmlReader->getAttribute('ref'); // returns 'A1:M13' for instance (or 'A1' for empty sheet)
if (preg_match('/[A-Z\d]+:([A-Z\d]+)/', $dimensionRef, $matches)) {
$lastCellIndex = $matches[1];
$this->numberOfColumns = CellHelper::getColumnIndexFromCellIndex($lastCellIndex) + 1;
}
} else if ($this->xmlReader->nodeType == \XMLReader::ELEMENT && $this->xmlReader->name === 'row') {
// Start of the row description
$isInsideRowTag = true;
Expand Down
9 changes: 9 additions & 0 deletions tests/Spout/Reader/XLSXTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ public function testReadShouldBeProtectedAgainstBillionLaughsAttack()
$this->assertEquals($expectedFirstRow, $allRows[0], 'Entities should be ignored when reading XML files.');
}

/**
* @return void
*/
public function testReadShouldBeAbleToProcessEmptySheets()
{
$allRows = $this->getAllRowsForFile('sheet_with_no_cells.xlsx');
$this->assertEquals([], $allRows, 'Sheet with no cells should be correctly processed.');
}

/**
* @param string $fileName
* @return array All the read rows the given file
Expand Down
Binary file added tests/resources/xlsx/sheet_with_no_cells.xlsx
Binary file not shown.

0 comments on commit 6bc9a18

Please sign in to comment.