diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ccf48b..f524d89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### v.0.5.6 2013-09-04 + +- Fix for formulas being returned along with values in XLSX files. (Thanks to [marktag](https://github.com/marktag)) + ### v.0.5.5 2013-08-23 - Fix for macro sheets appearing when parsing XLS files. (Thanks to [osuwariboy](https://github.com/osuwariboy)) diff --git a/SpreadsheetReader.php b/SpreadsheetReader.php index 0913c74..31a8af4 100644 --- a/SpreadsheetReader.php +++ b/SpreadsheetReader.php @@ -2,7 +2,7 @@ /** * Main class for spreadsheet reading * - * @version 0.5.4 + * @version 0.5.6 * @author Martins Pilsetnieks */ class SpreadsheetReader implements Iterator, Countable diff --git a/SpreadsheetReader_XLSX.php b/SpreadsheetReader_XLSX.php index 4a7dc34..581a87a 100644 --- a/SpreadsheetReader_XLSX.php +++ b/SpreadsheetReader_XLSX.php @@ -99,8 +99,6 @@ class SpreadsheetReader_XLSX implements Iterator, Countable private $LastSharedStringValue = null; private $RowOpen = false; - private $CellOpen = false; - private $ValueOpen = false; private $SSOpen = false; private $SSForwarded = false; @@ -981,6 +979,11 @@ public function next() $CurrentRowColumnCount = 0; } + if ($CurrentRowColumnCount > 0) + { + $this -> CurrentRow = array_fill(0, $CurrentRowColumnCount, ''); + } + $this -> RowOpen = true; break; } @@ -990,15 +993,12 @@ public function next() // Reading the necessary row, if found if ($this -> RowOpen) { - if ($CurrentRowColumnCount > 0) - { - $this -> CurrentRow = array_fill(0, $CurrentRowColumnCount, ''); - } - // These two are needed to control for empty cells $MaxIndex = 0; $CellCount = 0; + $CellHasSharedString = false; + while ($this -> Valid = $this -> Worksheet -> read()) { switch ($this -> Worksheet -> name) @@ -1010,6 +1010,7 @@ public function next() $this -> RowOpen = false; break 2; } + break; // Cell case 'c': // If it is a closing tag, skip it @@ -1018,42 +1019,53 @@ public function next() continue; } - $this -> CellOpen = !$this -> CellOpen; + $StyleId = (int)$this -> Worksheet -> getAttribute('s'); + + // Get the index of the cell + $Index = $this -> Worksheet -> getAttribute('r'); + $Letter = preg_replace('{[^[:alpha:]]}S', '', $Index); + $Index = self::IndexFromColumnLetter($Letter); - // Determine cell type and get value + // Determine cell type if ($this -> Worksheet -> getAttribute('t') == self::CELL_TYPE_SHARED_STR) { - $SharedStringIndex = $this -> Worksheet -> readString(); - $Value = $this -> GetSharedString($SharedStringIndex); + $CellHasSharedString = true; } else { - $Value = $this -> Worksheet -> readString(); + $CellHasSharedString = false; } - // Format value if necessary - if ($Value !== '') + $this -> CurrentRow[$Index] = ''; + + $CellCount++; + if ($Index > $MaxIndex) + { + $MaxIndex = $Index; + } + + break; + // Cell value + case 'v': + if ($this -> Worksheet -> nodeType == XMLReader::END_ELEMENT) { - $StyleId = (int)$this -> Worksheet -> getAttribute('s'); - if ($StyleId && isset($this -> Styles[$StyleId])) - { - $Value = $this -> FormatValue($Value, $StyleId); - } + continue; } - // Get the index of the cell - $Index = $this -> Worksheet -> getAttribute('r'); - $Letter = preg_replace('{[^[:alpha:]]}S', '', $Index); - $Index = self::IndexFromColumnLetter($Letter); + $Value = $this -> Worksheet -> readString(); - $this -> CurrentRow[$Index] = $Value; + if ($CellHasSharedString) + { + $Value = $this -> GetSharedString($Value); + } - $CellCount++; - if ($Index > $MaxIndex) + // Format value if necessary + if ($Value !== '' && $StyleId && isset($this -> Styles[$StyleId])) { - $MaxIndex = $Index; + $Value = $this -> FormatValue($Value, $StyleId); } + $this -> CurrentRow[$Index] = $Value; break; } }