mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-15 12:36:32 +00:00
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
This commit is contained in:
@@ -810,9 +810,11 @@ class Xlsx extends BaseReader
|
||||
} else {
|
||||
// Formula
|
||||
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToString');
|
||||
if (isset($c->f['t'])) {
|
||||
$attributes = $c->f['t'];
|
||||
$docSheet->getCell($r)->setFormulaAttributes(['t' => (string) $attributes]);
|
||||
$formulaAttributes = $c->f->attributes();
|
||||
if (isset($formulaAttributes['t'])) {
|
||||
$formulaType = $formulaAttributes['t'];
|
||||
$formulaRange = $formulaAttributes['ref'] ? (string) $formulaAttributes['ref'] : null;
|
||||
$docSheet->getCell($r)->setFormulaAttributes(['t' => (string) $formulaType, 'ref' => $formulaRange]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\Cell;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
||||
use PhpOffice\PhpSpreadsheet\RichText\RichText;
|
||||
@@ -1246,9 +1247,11 @@ class Worksheet extends WriterPart
|
||||
|
||||
private function writeCellFormula(XMLWriter $objWriter, string $cellValue, Cell $cell): void
|
||||
{
|
||||
$calculatedValue = $this->getParentWriter()->getPreCalculateFormulas() ? $cell->getCalculatedValue() : $cellValue;
|
||||
$calculatedValue = $this->getParentWriter()->getPreCalculateFormulas()
|
||||
? $cell->getCalculatedValue() : $cellValue;
|
||||
|
||||
if (is_string($calculatedValue)) {
|
||||
if (\PhpOffice\PhpSpreadsheet\Calculation\Functions::isError($calculatedValue)) {
|
||||
if (Functions::isError($calculatedValue)) {
|
||||
$this->writeCellError($objWriter, 'e', $cellValue, $calculatedValue);
|
||||
|
||||
return;
|
||||
@@ -1271,14 +1274,15 @@ class Worksheet extends WriterPart
|
||||
$objWriter->endElement();
|
||||
} else {
|
||||
$objWriter->writeElement('f', Xlfn::addXlfnStripEquals($cellValue));
|
||||
self::writeElementIf(
|
||||
$objWriter,
|
||||
$this->getParentWriter()->getOffice2003Compatibility() === false,
|
||||
'v',
|
||||
($this->getParentWriter()->getPreCalculateFormulas() && !is_array($calculatedValue) && substr($calculatedValue, 0, 1) !== '#')
|
||||
? StringHelper::formatNumber($calculatedValue) : '0'
|
||||
);
|
||||
}
|
||||
|
||||
self::writeElementIf(
|
||||
$objWriter,
|
||||
$this->getParentWriter()->getOffice2003Compatibility() === false,
|
||||
'v',
|
||||
($this->getParentWriter()->getPreCalculateFormulas() && !is_array($calculatedValue) && substr($calculatedValue, 0, 1) !== '#')
|
||||
? StringHelper::formatNumber($calculatedValue) : '0'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user