Add formula in v3.3.0

This commit is contained in:
David Bonting 2022-02-21 13:09:25 +01:00
parent cc42c1d29f
commit 71b800468f
3 changed files with 27 additions and 4 deletions

View File

@ -139,6 +139,9 @@ class Cell
*/ */
protected function detectType($value) protected function detectType($value)
{ {
if (CellTypeHelper::isFormula($value)) {
return self::TYPE_FORMULA;
}
if (CellTypeHelper::isBoolean($value)) { if (CellTypeHelper::isBoolean($value)) {
return self::TYPE_BOOLEAN; return self::TYPE_BOOLEAN;
} }
@ -198,6 +201,14 @@ class Cell
return $this->type === self::TYPE_DATE; return $this->type === self::TYPE_DATE;
} }
/**
* @return bool
*/
public function isFormula()
{
return $this->type === self::TYPE_FORMULA;
}
/** /**
* @return bool * @return bool
*/ */

View File

@ -60,9 +60,19 @@ class CellTypeHelper
*/ */
public static function isDateTimeOrDateInterval($value) public static function isDateTimeOrDateInterval($value)
{ {
return ( return ($value instanceof \DateTimeInterface ||
$value instanceof \DateTimeInterface ||
$value instanceof \DateInterval $value instanceof \DateInterval
); );
} }
/**
* Returns whether the given value looks like a formula
*
* @param $value
* @return bool whether the given value looks like a formula
*/
public static function isFormula($value)
{
return (strpos($value, '=') === 0);
}
} }

View File

@ -179,7 +179,7 @@ EOD;
* @throws InvalidArgumentException If the given value cannot be processed * @throws InvalidArgumentException If the given value cannot be processed
* @return RegisteredStyle * @return RegisteredStyle
*/ */
private function applyStyleAndRegister(Cell $cell, Style $rowStyle) : RegisteredStyle private function applyStyleAndRegister(Cell $cell, Style $rowStyle): RegisteredStyle
{ {
$isMatchingRowStyle = false; $isMatchingRowStyle = false;
if ($cell->getStyle()->isEmpty()) { if ($cell->getStyle()->isEmpty()) {
@ -228,7 +228,9 @@ EOD;
$cellXML = '<c r="' . $columnLetters . $rowIndexOneBased . '"'; $cellXML = '<c r="' . $columnLetters . $rowIndexOneBased . '"';
$cellXML .= ' s="' . $styleId . '"'; $cellXML .= ' s="' . $styleId . '"';
if ($cell->isString()) { if ($cell->isFormula()) {
$cellXML .= ' ><f>' . substr($cell->getValue(), 1) . '</f></c>';
} elseif ($cell->isString()) {
$cellXML .= $this->getCellXMLFragmentForNonEmptyString($cell->getValue()); $cellXML .= $this->getCellXMLFragmentForNonEmptyString($cell->getValue());
} elseif ($cell->isBoolean()) { } elseif ($cell->isBoolean()) {
$cellXML .= ' t="b"><v>' . (int) ($cell->getValue()) . '</v></c>'; $cellXML .= ' t="b"><v>' . (int) ($cell->getValue()) . '</v></c>';