feat() formula support see #612

This commit is contained in:
Jeremy Jumeau 2022-04-29 09:58:18 +02:00
parent 3681a3421a
commit a838c92c90
4 changed files with 105 additions and 2 deletions

View File

@ -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"],

View File

@ -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>';
}
}

View File

@ -5,6 +5,7 @@ namespace Box\Spout\Writer\XLSX\Internal;
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 @@ EOD;
$rowXML = '<row r="' . $rowIndex . '" spans="1:' . $numCells . '">';
foreach($dataRow as $cellValue) {
foreach ($dataRow as $cellValue) {
$rowXML .= $this->getCellXML($rowIndex, $cellNumber, $cellValue, $style->getId());
$cellNumber++;
}
@ -227,6 +228,8 @@ EOD;
// 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));
}

View File

@ -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]);
}
}
}
}
}