Skip to content

Commit 8156ed9

Browse files
author
MarkBaker
committed
First steps to handling array formulae with the Xlsx Reader and Writer
Only supports the basic happy path (no handling for Error results when reading) No functionality yet for setting array formulae in code
1 parent 2607917 commit 8156ed9

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/PhpSpreadsheet/Reader/Xlsx.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -810,9 +810,11 @@ public function load(string $filename, int $flags = 0): Spreadsheet
810810
} else {
811811
// Formula
812812
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToString');
813-
if (isset($c->f['t'])) {
814-
$attributes = $c->f['t'];
815-
$docSheet->getCell($r)->setFormulaAttributes(['t' => (string) $attributes]);
813+
$formulaAttributes = $c->f->attributes();
814+
if (isset($formulaAttributes['t'])) {
815+
$formulaType = $formulaAttributes['t'];
816+
$formulaRange = $formulaAttributes['ref'] ? (string) $formulaAttributes['ref'] : null;
817+
$docSheet->getCell($r)->setFormulaAttributes(['t' => (string) $formulaType, 'ref' => $formulaRange]);
816818
}
817819
}
818820

src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php

+13-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Writer\Xlsx;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
56
use PhpOffice\PhpSpreadsheet\Cell\Cell;
67
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
78
use PhpOffice\PhpSpreadsheet\RichText\RichText;
@@ -1246,9 +1247,11 @@ private function writeCellError(XMLWriter $objWriter, string $mappedType, string
12461247

12471248
private function writeCellFormula(XMLWriter $objWriter, string $cellValue, Cell $cell): void
12481249
{
1249-
$calculatedValue = $this->getParentWriter()->getPreCalculateFormulas() ? $cell->getCalculatedValue() : $cellValue;
1250+
$calculatedValue = $this->getParentWriter()->getPreCalculateFormulas()
1251+
? $cell->getCalculatedValue() : $cellValue;
1252+
12501253
if (is_string($calculatedValue)) {
1251-
if (\PhpOffice\PhpSpreadsheet\Calculation\Functions::isError($calculatedValue)) {
1254+
if (Functions::isError($calculatedValue)) {
12521255
$this->writeCellError($objWriter, 'e', $cellValue, $calculatedValue);
12531256

12541257
return;
@@ -1271,14 +1274,15 @@ private function writeCellFormula(XMLWriter $objWriter, string $cellValue, Cell
12711274
$objWriter->endElement();
12721275
} else {
12731276
$objWriter->writeElement('f', Xlfn::addXlfnStripEquals($cellValue));
1274-
self::writeElementIf(
1275-
$objWriter,
1276-
$this->getParentWriter()->getOffice2003Compatibility() === false,
1277-
'v',
1278-
($this->getParentWriter()->getPreCalculateFormulas() && !is_array($calculatedValue) && substr($calculatedValue, 0, 1) !== '#')
1279-
? StringHelper::formatNumber($calculatedValue) : '0'
1280-
);
12811277
}
1278+
1279+
self::writeElementIf(
1280+
$objWriter,
1281+
$this->getParentWriter()->getOffice2003Compatibility() === false,
1282+
'v',
1283+
($this->getParentWriter()->getPreCalculateFormulas() && !is_array($calculatedValue) && substr($calculatedValue, 0, 1) !== '#')
1284+
? StringHelper::formatNumber($calculatedValue) : '0'
1285+
);
12821286
}
12831287

12841288
/**

0 commit comments

Comments
 (0)