Add support for files containing formulas
Formulas will be skipped on reading. The result of the formulas will be kept though.
This commit is contained in:
parent
8a45cec220
commit
b21bb86682
@ -206,13 +206,25 @@ class XLSX extends AbstractReader
|
|||||||
$currentColumnIndex = CellHelper::getColumnIndexFromCellIndex($currentCellIndex);
|
$currentColumnIndex = CellHelper::getColumnIndexFromCellIndex($currentCellIndex);
|
||||||
$node = $this->xmlReader->expand();
|
$node = $this->xmlReader->expand();
|
||||||
|
|
||||||
|
$hasInlineString = ($this->xmlReader->getAttribute('t') === 'inlineStr');
|
||||||
$hasSharedString = ($this->xmlReader->getAttribute('t') === 's');
|
$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);
|
$rowData[$currentColumnIndex] = $this->sharedStringsHelper->getStringAtIndex($sharedStringIndex);
|
||||||
} else {
|
} else {
|
||||||
// for inline strings or numbers, just get the value
|
// other values are formatted this way:
|
||||||
$rowData[$currentColumnIndex] = trim($node->nodeValue);
|
// <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') {
|
} else if ($this->xmlReader->nodeType == \XMLReader::END_ELEMENT && $this->xmlReader->name === 'row') {
|
||||||
// End of the row description
|
// End of the row description
|
||||||
|
@ -245,6 +245,21 @@ class XLSXTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals([], $allRows, 'Sheet with no cells should be correctly processed.');
|
$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
|
* @param string $fileName
|
||||||
* @return array All the read rows the given file
|
* @return array All the read rows the given file
|
||||||
|
BIN
tests/resources/xlsx/sheet_with_formulas.xlsx
Normal file
BIN
tests/resources/xlsx/sheet_with_formulas.xlsx
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user