Merge pull request #43 from box/support_files_with_formulas

Add support for files containing formulas
This commit is contained in:
Adrien Loison 2015-05-29 09:08:48 -07:00
commit 03be58b84d
3 changed files with 31 additions and 4 deletions

View File

@ -206,13 +206,25 @@ class XLSX extends AbstractReader
$currentColumnIndex = CellHelper::getColumnIndexFromCellIndex($currentCellIndex);
$node = $this->xmlReader->expand();
$hasInlineString = ($this->xmlReader->getAttribute('t') === 'inlineStr');
$hasSharedString = ($this->xmlReader->getAttribute('t') === 's');
if ($hasSharedString) {
$sharedStringIndex = intval($node->nodeValue);
if ($hasInlineString) {
// inline strings are formatted this way:
// <c r="A1" t="inlineStr"><is><t>[INLINE_STRING]</t></is></c>
$tNode = $node->getElementsByTagName('t')->item(0);
$rowData[$currentColumnIndex] = trim($tNode->nodeValue);
} else if ($hasSharedString) {
// shared strings are formatted this way:
// <c r="A1" t="s"><v>[SHARED_STRING_INDEX]</v></c>
$vNode = $node->getElementsByTagName('v')->item(0);
$sharedStringIndex = intval($vNode->nodeValue);
$rowData[$currentColumnIndex] = $this->sharedStringsHelper->getStringAtIndex($sharedStringIndex);
} else {
// for inline strings or numbers, just get the value
$rowData[$currentColumnIndex] = trim($node->nodeValue);
// other values are formatted this way:
// <c r="A1"><v>[VALUE]</v></c>
$vNode = $node->getElementsByTagName('v')->item(0);
$rowData[$currentColumnIndex] = intval($vNode->nodeValue);
}
} else if ($this->xmlReader->nodeType == \XMLReader::END_ELEMENT && $this->xmlReader->name === 'row') {
// End of the row description

View File

@ -245,6 +245,21 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
$this->assertEquals([], $allRows, 'Sheet with no cells should be correctly processed.');
}
/**
* @return void
*/
public function testReadShouldSkipFormulas()
{
$allRows = $this->getAllRowsForFile('sheet_with_formulas.xlsx');
$expectedRows = [
['val1', 'val2', 'total1', 'total2'],
[10, 20, 30, 21],
[11, 21, 32, 41],
];
$this->assertEquals($expectedRows, $allRows);
}
/**
* @param string $fileName
* @return array All the read rows the given file

Binary file not shown.