Skip to content

Commit

Permalink
feat() formula support see box#612
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Jumeau committed Apr 29, 2022
1 parent 3681a34 commit a838c92
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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"],
Expand Down
40 changes: 40 additions & 0 deletions src/Spout/Writer/XLSX/Formula.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Box\Spout\Writer\XLSX;

/**
* Class Formula
*
* @package Box\Spout\Reader\XLSX
*/
class Formula
{


/**
* @var string
*/
protected $formula;

/**
* @var string
*/
protected $value = '0';

/**
* Formula constructor.
* @param string $formula
* @param string $value
*/
public function __construct($formula, $value = '0')
{
$this->formula = $formula;
$this->value = $value;
}

public function getXml()
{
return '><f>' . $this->formula . '</f><v>' . $this->value . '</v></c>';
}

}
5 changes: 4 additions & 1 deletion src/Spout/Writer/XLSX/Internal/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -184,7 +185,7 @@ protected function addNonEmptyRow($dataRow, $style)

$rowXML = '<row r="' . $rowIndex . '" spans="1:' . $numCells . '">';

foreach($dataRow as $cellValue) {
foreach ($dataRow as $cellValue) {
$rowXML .= $this->getCellXML($rowIndex, $cellNumber, $cellValue, $style->getId());
$cellNumber++;
}
Expand Down Expand Up @@ -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));
}
Expand Down
60 changes: 60 additions & 0 deletions tests/Spout/Writer/XLSX/FormulaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Box\Spout\Writer\XLSX;


use Box\Spout\Common\Type;
use Box\Spout\Reader\ReaderFactory;
use Box\Spout\Reader\XLSX\Sheet;
use Box\Spout\TestUsingResource;
use Box\Spout\Writer\WriterFactory;

class FormulaTest extends \PHPUnit_Framework_TestCase
{

use TestUsingResource;

/**
* @throws \Box\Spout\Common\Exception\IOException
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException
* @throws \Box\Spout\Common\Exception\InvalidArgumentException
* @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException
*/
public function testGetXml()
{
$fileName = 'test_formula.xlsx';
$this->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]);
}
}
}
}
}

0 comments on commit a838c92

Please sign in to comment.