Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Add formula functionality in v3.3.0 #870

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Spout/Common/Entity/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ public function setType($type)
*/
protected function detectType($value)
{
if (CellTypeHelper::isFormula($value)) {
return self::TYPE_FORMULA;
}
if (CellTypeHelper::isBoolean($value)) {
return self::TYPE_BOOLEAN;
}
Expand Down Expand Up @@ -198,6 +201,14 @@ public function isDate()
return $this->type === self::TYPE_DATE;
}

/**
* @return bool
*/
public function isFormula()
{
return $this->type === self::TYPE_FORMULA;
}

/**
* @return bool
*/
Expand Down
14 changes: 12 additions & 2 deletions src/Spout/Common/Helper/CellTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,19 @@ public static function isBoolean($value)
*/
public static function isDateTimeOrDateInterval($value)
{
return (
$value instanceof \DateTimeInterface ||
return ($value instanceof \DateTimeInterface ||
$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 (is_array($value) || strpos($value, '=') === 0);
}
}
10 changes: 8 additions & 2 deletions src/Spout/Writer/XLSX/Manager/WorksheetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private function addNonEmptyRow(Worksheet $worksheet, Row $row)
* @throws InvalidArgumentException If the given value cannot be processed
* @return RegisteredStyle
*/
private function applyStyleAndRegister(Cell $cell, Style $rowStyle) : RegisteredStyle
private function applyStyleAndRegister(Cell $cell, Style $rowStyle): RegisteredStyle
{
$isMatchingRowStyle = false;
if ($cell->getStyle()->isEmpty()) {
Expand Down Expand Up @@ -228,7 +228,13 @@ private function getCellXML($rowIndexOneBased, $columnIndexZeroBased, Cell $cell
$cellXML = '<c r="' . $columnLetters . $rowIndexOneBased . '"';
$cellXML .= ' s="' . $styleId . '"';

if ($cell->isString()) {
if ($cell->isFormula()) {
if (is_array($cell->getValue())) {
$cellXML .= ' ><f>' . substr($cell->getValue()[0], 1) . '</f><v>' . $cell->getValue()[1] . '</v></c>';
} else {
$cellXML .= ' ><f>' . substr($cell->getValue(), 1) . '</f></c>';
}
} elseif ($cell->isString()) {
$cellXML .= $this->getCellXMLFragmentForNonEmptyString($cell->getValue());
} elseif ($cell->isBoolean()) {
$cellXML .= ' t="b"><v>' . (int) ($cell->getValue()) . '</v></c>';
Expand Down