Skip to content

Commit 39a6c29

Browse files
author
MarkBaker
committed
Set formula attributes datatype in Cell
1 parent 8156ed9 commit 39a6c29

File tree

3 files changed

+29
-41
lines changed

3 files changed

+29
-41
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,11 +2065,6 @@ parameters:
20652065
count: 1
20662066
path: src/PhpSpreadsheet/Cell/Cell.php
20672067

2068-
-
2069-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\Cell\\:\\:getFormulaAttributes\\(\\) has no return type specified\\.$#"
2070-
count: 1
2071-
path: src/PhpSpreadsheet/Cell/Cell.php
2072-
20732068
-
20742069
message: "#^Parameter \\#1 \\$textValue of static method PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\DataType\\:\\:checkString\\(\\) expects PhpOffice\\\\PhpSpreadsheet\\\\RichText\\\\RichText\\|string\\|null, mixed given\\.$#"
20752070
count: 1
@@ -2080,11 +2075,6 @@ parameters:
20802075
count: 1
20812076
path: src/PhpSpreadsheet/Cell/Cell.php
20822077

2083-
-
2084-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\Cell\\:\\:\\$formulaAttributes has no type specified\\.$#"
2085-
count: 1
2086-
path: src/PhpSpreadsheet/Cell/Cell.php
2087-
20882078
-
20892079
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\Cell\\:\\:\\$parent \\(PhpOffice\\\\PhpSpreadsheet\\\\Collection\\\\Cells\\) in isset\\(\\) is not nullable\\.$#"
20902080
count: 6

src/PhpSpreadsheet/Cell/Cell.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class Cell
6565

6666
/**
6767
* Attributes of the formula.
68+
*
69+
* @var null|array
6870
*/
6971
private $formulaAttributes;
7072

@@ -701,11 +703,11 @@ public function setXfIndex($indexValue)
701703
/**
702704
* Set the formula attributes.
703705
*
704-
* @param mixed $attributes
706+
* @param mixed[] $attributes
705707
*
706708
* @return $this
707709
*/
708-
public function setFormulaAttributes($attributes)
710+
public function setFormulaAttributes(array $attributes)
709711
{
710712
$this->formulaAttributes = $attributes;
711713

@@ -715,7 +717,7 @@ public function setFormulaAttributes($attributes)
715717
/**
716718
* Get the formula attributes.
717719
*/
718-
public function getFormulaAttributes()
720+
public function getFormulaAttributes(): ?array
719721
{
720722
return $this->formulaAttributes;
721723
}

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 24 additions & 28 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' => $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,17 +785,13 @@ public function load(string $filename, int $flags = 0): Spreadsheet
779785
}
780786
} else {
781787
// Formula
782-
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToBoolean');
783-
if (isset($c->f['t'])) {
784-
$att = $c->f;
785-
$docSheet->getCell($r)->setFormulaAttributes($att);
786-
}
788+
$this->castToFormula($docSheet, $c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToBoolean');
787789
}
788790

789791
break;
790792
case 'inlineStr':
791793
if (isset($c->f)) {
792-
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
794+
$this->castToFormula($docSheet, $c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
793795
} else {
794796
$value = $this->parseRichText($c->is);
795797
}
@@ -800,7 +802,7 @@ public function load(string $filename, int $flags = 0): Spreadsheet
800802
$value = self::castToError($c);
801803
} else {
802804
// Formula
803-
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
805+
$this->castToFormula($docSheet, $c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
804806
}
805807

806808
break;
@@ -809,13 +811,7 @@ public function load(string $filename, int $flags = 0): Spreadsheet
809811
$value = self::castToString($c);
810812
} else {
811813
// 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-
}
814+
$this->castToFormula($docSheet, $c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToString');
819815
}
820816

821817
break;

0 commit comments

Comments
 (0)