diff --git a/composer.json b/composer.json index 76485c963..5d8b42574 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "box/spout", + "name": "jeremyjumeau/spout", "description": "PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way", "type": "library", "keywords": ["php","read","write","csv","xlsx","ods","odf","open","office","excel","spreadsheet","scale","memory","stream","ooxml"], diff --git a/src/Spout/Writer/XLSX/Formula.php b/src/Spout/Writer/XLSX/Formula.php new file mode 100644 index 000000000..5f7accf7f --- /dev/null +++ b/src/Spout/Writer/XLSX/Formula.php @@ -0,0 +1,40 @@ +formula = $formula; + $this->value = $value; + } + + public function getXml() + { + return '>' . $this->formula . '' . $this->value . ''; + } + +} \ No newline at end of file diff --git a/src/Spout/Writer/XLSX/Internal/Worksheet.php b/src/Spout/Writer/XLSX/Internal/Worksheet.php index 0bd909d18..fc5d39e57 100644 --- a/src/Spout/Writer/XLSX/Internal/Worksheet.php +++ b/src/Spout/Writer/XLSX/Internal/Worksheet.php @@ -5,6 +5,7 @@ use Box\Spout\Common\Exception\InvalidArgumentException; use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Helper\StringHelper; +use Box\Spout\Writer\XLSX\Formula; use Box\Spout\Writer\Common\Helper\CellHelper; use Box\Spout\Writer\Common\Internal\WorksheetInterface; @@ -184,7 +185,7 @@ protected function addNonEmptyRow($dataRow, $style) $rowXML = ''; - foreach($dataRow as $cellValue) { + foreach ($dataRow as $cellValue) { $rowXML .= $this->getCellXML($rowIndex, $cellNumber, $cellValue, $style->getId()); $cellNumber++; } @@ -227,6 +228,8 @@ protected function getCellXML($rowIndex, $cellNumber, $cellValue, $styleId) // NOTE: not appending to $cellXML is the right behavior!! $cellXML = ''; } + } else if ($cellValue instanceof Formula) { + $cellXML .= $cellValue->getXml(); } else { throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellValue)); } diff --git a/tests/Spout/Writer/XLSX/FormulaTest.php b/tests/Spout/Writer/XLSX/FormulaTest.php new file mode 100644 index 000000000..9c4b15fb4 --- /dev/null +++ b/tests/Spout/Writer/XLSX/FormulaTest.php @@ -0,0 +1,60 @@ +createGeneratedFolderIfNeeded($fileName); + $resourcePath = $this->getGeneratedResourcePath($fileName); + + $writer = WriterFactory::create(Type::XLSX); + $writer->openToFile($resourcePath); + + $writer->addRows([ + [1], + [1], + [1], + [1], + ]); + + $writer->addRow([ + new Formula('SUM(A1:A4)', 4) + ]); + $writer->close(); + + + $reader = ReaderFactory::create(Type::XLSX); + $reader->open($resourcePath); + + $i = 0; + /** @var Sheet $sheet */ + foreach ($reader->getSheetIterator() as $sheet) { + foreach ($sheet->getRowIterator() as $item) { + $i++; + if ($i == 5) { + static::assertEquals(4, $item[0]); + } + } + } + } +} \ No newline at end of file