Skip to content

Commit

Permalink
Fix for fomulas appearing in XLSX data
Browse files Browse the repository at this point in the history
  • Loading branch information
pilsetnieks committed Sep 4, 2013
1 parent daaec9b commit 4e24a6a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
2 changes: 1 addition & 1 deletion SpreadsheetReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 39 additions & 27 deletions SpreadsheetReader_XLSX.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -981,6 +979,11 @@ public function next()
$CurrentRowColumnCount = 0;
}

if ($CurrentRowColumnCount > 0)
{
$this -> CurrentRow = array_fill(0, $CurrentRowColumnCount, '');
}

$this -> RowOpen = true;
break;
}
Expand All @@ -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)
Expand All @@ -1010,6 +1010,7 @@ public function next()
$this -> RowOpen = false;
break 2;
}
break;
// Cell
case 'c':
// If it is a closing tag, skip it
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 4e24a6a

Please sign in to comment.