Skip to content

Commit 29a967c

Browse files
author
MarkBaker
committed
Set formula attributes datatype in Cell
1 parent a1932a0 commit 29a967c

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -307,28 +307,34 @@ private static function castToString($c)
307307
return isset($c->v) ? (string) $c->v : null;
308308
}
309309

310-
private function castToFormula($c, $r, &$cellDataType, &$value, &$calculatedValue, &$sharedFormulas, $castBaseType): void
310+
private function castToFormula(Worksheet $docSheet, $c, $r, &$cellDataType, &$value, &$calculatedValue, &$sharedFormulas, $castBaseType): void
311311
{
312-
$attr = $c->f->attributes();
312+
$formulaAttributes = $c->f->attributes();
313313
$cellDataType = 'f';
314314
$value = "={$c->f}";
315315
$calculatedValue = self::$castBaseType($c);
316316

317317
// Shared formula?
318-
if (isset($attr['t']) && strtolower((string) $attr['t']) == 'shared') {
319-
$instance = (string) $attr['si'];
318+
if (isset($formulaAttributes['t'])) {
319+
if (strtolower((string) $formulaAttributes['t']) === 'shared') {
320+
$instance = (string) $formulaAttributes['si'];
320321

321-
if (!isset($sharedFormulas[(string) $attr['si']])) {
322-
$sharedFormulas[$instance] = ['master' => $r, 'formula' => $value];
323-
} else {
324-
$master = Coordinate::indexesFromString($sharedFormulas[$instance]['master']);
325-
$current = Coordinate::indexesFromString($r);
322+
if (!isset($sharedFormulas[(string) $formulaAttributes['si']])) {
323+
$sharedFormulas[$instance] = ['master' => $r, 'formula' => $value];
324+
} else {
325+
$master = Coordinate::indexesFromString($sharedFormulas[$instance]['master']);
326+
$current = Coordinate::indexesFromString($r);
326327

327-
$difference = [0, 0];
328-
$difference[0] = $current[0] - $master[0];
329-
$difference[1] = $current[1] - $master[1];
328+
$difference = [0, 0];
329+
$difference[0] = $current[0] - $master[0];
330+
$difference[1] = $current[1] - $master[1];
330331

331-
$value = $this->referenceHelper->updateFormulaReferences($sharedFormulas[$instance]['formula'], 'A1', $difference[0], $difference[1]);
332+
$value = $this->referenceHelper->updateFormulaReferences($sharedFormulas[$instance]['formula'], 'A1', $difference[0], $difference[1]);
333+
}
334+
} elseif (strtolower((string) $formulaAttributes['t']) === 'array') {
335+
$formulaType = (string) $formulaAttributes['t'];
336+
$formulaRange = $formulaAttributes['ref'] ? (string) $formulaAttributes['ref'] : null;
337+
$docSheet->getCell($r)->setFormulaAttributes(['t' => (string) $formulaType, 'ref' => $formulaRange]);
332338
}
333339
}
334340
}
@@ -747,7 +753,7 @@ public function load(string $filename, int $flags = 0): Spreadsheet
747753

748754
if (!$this->getReadFilter()->readCell($coordinates[0], (int) $coordinates[1], $docSheet->getTitle())) {
749755
if (isset($cAttr->f)) {
750-
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
756+
$this->castToFormula($docSheet, $c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
751757
}
752758
++$rowIndex;
753759

@@ -779,7 +785,7 @@ public function load(string $filename, int $flags = 0): Spreadsheet
779785
}
780786
} else {
781787
// Formula
782-
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToBoolean');
788+
$this->castToFormula($docSheet, $c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToBoolean');
783789
if (isset($c->f['t'])) {
784790
$att = $c->f;
785791
$docSheet->getCell($r)->setFormulaAttributes($att);
@@ -789,7 +795,7 @@ public function load(string $filename, int $flags = 0): Spreadsheet
789795
break;
790796
case 'inlineStr':
791797
if (isset($c->f)) {
792-
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
798+
$this->castToFormula($docSheet, $c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
793799
} else {
794800
$value = $this->parseRichText($c->is);
795801
}
@@ -800,7 +806,7 @@ public function load(string $filename, int $flags = 0): Spreadsheet
800806
$value = self::castToError($c);
801807
} else {
802808
// Formula
803-
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
809+
$this->castToFormula($docSheet, $c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
804810
}
805811

806812
break;
@@ -809,13 +815,7 @@ public function load(string $filename, int $flags = 0): Spreadsheet
809815
$value = self::castToString($c);
810816
} else {
811817
// Formula
812-
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToString');
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]);
818-
}
818+
$this->castToFormula($docSheet, $c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToString');
819819
}
820820

821821
break;

0 commit comments

Comments
 (0)