Set formula attributes datatype in Cell

This commit is contained in:
MarkBaker
2022-01-30 20:08:13 +01:00
parent 8156ed9fa4
commit 39a6c29fb5
3 changed files with 29 additions and 41 deletions
-10
View File
@@ -2065,11 +2065,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Cell/Cell.php
-
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\Cell\\:\\:getFormulaAttributes\\(\\) has no return type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Cell/Cell.php
-
message: "#^Parameter \\#1 \\$textValue of static method PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\DataType\\:\\:checkString\\(\\) expects PhpOffice\\\\PhpSpreadsheet\\\\RichText\\\\RichText\\|string\\|null, mixed given\\.$#"
count: 1
@@ -2080,11 +2075,6 @@ parameters:
count: 1
path: src/PhpSpreadsheet/Cell/Cell.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\Cell\\:\\:\\$formulaAttributes has no type specified\\.$#"
count: 1
path: src/PhpSpreadsheet/Cell/Cell.php
-
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Cell\\\\Cell\\:\\:\\$parent \\(PhpOffice\\\\PhpSpreadsheet\\\\Collection\\\\Cells\\) in isset\\(\\) is not nullable\\.$#"
count: 6
+5 -3
View File
@@ -65,6 +65,8 @@ class Cell
/**
* Attributes of the formula.
*
* @var null|array
*/
private $formulaAttributes;
@@ -701,11 +703,11 @@ class Cell
/**
* Set the formula attributes.
*
* @param mixed $attributes
* @param mixed[] $attributes
*
* @return $this
*/
public function setFormulaAttributes($attributes)
public function setFormulaAttributes(array $attributes)
{
$this->formulaAttributes = $attributes;
@@ -715,7 +717,7 @@ class Cell
/**
* Get the formula attributes.
*/
public function getFormulaAttributes()
public function getFormulaAttributes(): ?array
{
return $this->formulaAttributes;
}
+24 -28
View File
@@ -307,28 +307,34 @@ class Xlsx extends BaseReader
return isset($c->v) ? (string) $c->v : null;
}
private function castToFormula($c, $r, &$cellDataType, &$value, &$calculatedValue, &$sharedFormulas, $castBaseType): void
private function castToFormula(Worksheet $docSheet, $c, $r, &$cellDataType, &$value, &$calculatedValue, &$sharedFormulas, $castBaseType): void
{
$attr = $c->f->attributes();
$formulaAttributes = $c->f->attributes();
$cellDataType = 'f';
$value = "={$c->f}";
$calculatedValue = self::$castBaseType($c);
// Shared formula?
if (isset($attr['t']) && strtolower((string) $attr['t']) == 'shared') {
$instance = (string) $attr['si'];
if (isset($formulaAttributes['t'])) {
if (strtolower((string) $formulaAttributes['t']) === 'shared') {
$instance = (string) $formulaAttributes['si'];
if (!isset($sharedFormulas[(string) $attr['si']])) {
$sharedFormulas[$instance] = ['master' => $r, 'formula' => $value];
} else {
$master = Coordinate::indexesFromString($sharedFormulas[$instance]['master']);
$current = Coordinate::indexesFromString($r);
if (!isset($sharedFormulas[(string) $formulaAttributes['si']])) {
$sharedFormulas[$instance] = ['master' => $r, 'formula' => $value];
} else {
$master = Coordinate::indexesFromString($sharedFormulas[$instance]['master']);
$current = Coordinate::indexesFromString($r);
$difference = [0, 0];
$difference[0] = $current[0] - $master[0];
$difference[1] = $current[1] - $master[1];
$difference = [0, 0];
$difference[0] = $current[0] - $master[0];
$difference[1] = $current[1] - $master[1];
$value = $this->referenceHelper->updateFormulaReferences($sharedFormulas[$instance]['formula'], 'A1', $difference[0], $difference[1]);
$value = $this->referenceHelper->updateFormulaReferences($sharedFormulas[$instance]['formula'], 'A1', $difference[0], $difference[1]);
}
} elseif (strtolower((string) $formulaAttributes['t']) === 'array') {
$formulaType = (string) $formulaAttributes['t'];
$formulaRange = $formulaAttributes['ref'] ? (string) $formulaAttributes['ref'] : null;
$docSheet->getCell($r)->setFormulaAttributes(['t' => $formulaType, 'ref' => $formulaRange]);
}
}
}
@@ -747,7 +753,7 @@ class Xlsx extends BaseReader
if (!$this->getReadFilter()->readCell($coordinates[0], (int) $coordinates[1], $docSheet->getTitle())) {
if (isset($cAttr->f)) {
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
$this->castToFormula($docSheet, $c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
}
++$rowIndex;
@@ -779,17 +785,13 @@ class Xlsx extends BaseReader
}
} else {
// Formula
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToBoolean');
if (isset($c->f['t'])) {
$att = $c->f;
$docSheet->getCell($r)->setFormulaAttributes($att);
}
$this->castToFormula($docSheet, $c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToBoolean');
}
break;
case 'inlineStr':
if (isset($c->f)) {
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
$this->castToFormula($docSheet, $c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
} else {
$value = $this->parseRichText($c->is);
}
@@ -800,7 +802,7 @@ class Xlsx extends BaseReader
$value = self::castToError($c);
} else {
// Formula
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
$this->castToFormula($docSheet, $c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError');
}
break;
@@ -809,13 +811,7 @@ class Xlsx extends BaseReader
$value = self::castToString($c);
} else {
// Formula
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToString');
$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]);
}
$this->castToFormula($docSheet, $c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToString');
}
break;