From 6bc9a18e9b3f9ec08ed8c1d9db917128c542cb8d Mon Sep 17 00:00:00 2001 From: Adrien Loison Date: Mon, 26 Jan 2015 11:22:09 -0800 Subject: [PATCH] Add support for empty sheets --- src/Spout/Reader/XLSX.php | 8 +++++--- tests/Spout/Reader/XLSXTest.php | 9 +++++++++ tests/resources/xlsx/sheet_with_no_cells.xlsx | Bin 0 -> 3543 bytes 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 tests/resources/xlsx/sheet_with_no_cells.xlsx diff --git a/src/Spout/Reader/XLSX.php b/src/Spout/Reader/XLSX.php index bbe9ce61..db467077 100644 --- a/src/Spout/Reader/XLSX.php +++ b/src/Spout/Reader/XLSX.php @@ -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; diff --git a/tests/Spout/Reader/XLSXTest.php b/tests/Spout/Reader/XLSXTest.php index e1cf9940..823490bd 100644 --- a/tests/Spout/Reader/XLSXTest.php +++ b/tests/Spout/Reader/XLSXTest.php @@ -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 diff --git a/tests/resources/xlsx/sheet_with_no_cells.xlsx b/tests/resources/xlsx/sheet_with_no_cells.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..c41e892dcbc09359bb8cbd82f1c4ce3ec45ea1ab GIT binary patch literal 3543 zcmai02{@E%8yUKi2L28_b?tpf zx@~mBBv+5%aJ5{S%M7v~fhffhpzn#G(*X=m4w!JZ4N& zTM7$SnmoG7^ga?9>Fm5{o-_yeqvd9lFIm>*wCtURAJ7Ncvy@d ziYM%&^xuutx%bW_h_TiObgKw_kx@k!YxyMh2iGxIFpGxwIIx-Mt8p&Jg|BJ8 z8gJ1k@XwMQ1ft!u5#O>bNYX*pNis&xgl+OQ2c=D@AU>EPn3e>KKl!0@o$cmpPdaQY zL-;3d8c=#5=UV44gu~O`Vc4b&tNt1`y7zNjQh%#(MyIcM)!xsWTD_y;6{-ib$R91yIHio?`&86jvw{DsE3cS1q@ilJNS|ls zb4y}PUs!DzY)C`yn2%}p!=V<1vr|orus=6jb2O zb@B-zH?QGMRf-kOzx~HtOnj4!8s2BViwi=vo$0B1l~vi`7dudNx_z3D-*|o{sr9Sr zUzHR;X9%d{(il4s<}~my?GM$;#T~a5t8=0XhIj%Hs$g(bWB_fcjedtWbqo5=ZR7h! zOa!N_MYJ+Q7X!YW&F= ze+ss9VVY^Ht9h&`(F3(5lVas}UkJ5ZE3!OStz6xG_pL{w2+<)sf2y*lI~lAd>Kj}YuNW+n0s<*?LepzcL(#LD^%++2&Y ziJ3@mOG{3IOK|zWo?~n36)}-f)n3<>uCyYDEIFVG)`k2~Z;Jy_k}hN>qWVUAkXy0Z zF#)^;u5GgCe281J9K0y*%f`ZVzSOF$#z!?TsiY|kug2o10PZd!=4C0)M>m;SsW{Ru zcv21YN)X;YyRC$pFyeMfQJ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