Added Formula support
This commit is contained in:
parent
799ad93d23
commit
927684486e
40
src/Spout/Writer/XLSX/Formula.php
Normal file
40
src/Spout/Writer/XLSX/Formula.php
Normal 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>';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -5,6 +5,7 @@ namespace Box\Spout\Writer\XLSX\Internal;
|
|||||||
use Box\Spout\Common\Exception\InvalidArgumentException;
|
use Box\Spout\Common\Exception\InvalidArgumentException;
|
||||||
use Box\Spout\Common\Exception\IOException;
|
use Box\Spout\Common\Exception\IOException;
|
||||||
use Box\Spout\Common\Helper\StringHelper;
|
use Box\Spout\Common\Helper\StringHelper;
|
||||||
|
use Box\Spout\Writer\XLSX\Formula;
|
||||||
use Box\Spout\Writer\Common\Helper\CellHelper;
|
use Box\Spout\Writer\Common\Helper\CellHelper;
|
||||||
use Box\Spout\Writer\Common\Internal\WorksheetInterface;
|
use Box\Spout\Writer\Common\Internal\WorksheetInterface;
|
||||||
|
|
||||||
@ -184,7 +185,7 @@ EOD;
|
|||||||
|
|
||||||
$rowXML = '<row r="' . $rowIndex . '" spans="1:' . $numCells . '">';
|
$rowXML = '<row r="' . $rowIndex . '" spans="1:' . $numCells . '">';
|
||||||
|
|
||||||
foreach($dataRow as $cellValue) {
|
foreach ($dataRow as $cellValue) {
|
||||||
$rowXML .= $this->getCellXML($rowIndex, $cellNumber, $cellValue, $style->getId());
|
$rowXML .= $this->getCellXML($rowIndex, $cellNumber, $cellValue, $style->getId());
|
||||||
$cellNumber++;
|
$cellNumber++;
|
||||||
}
|
}
|
||||||
@ -227,6 +228,8 @@ EOD;
|
|||||||
// NOTE: not appending to $cellXML is the right behavior!!
|
// NOTE: not appending to $cellXML is the right behavior!!
|
||||||
$cellXML = '';
|
$cellXML = '';
|
||||||
}
|
}
|
||||||
|
} elseif ($cellValue instanceof Formula) {
|
||||||
|
$cellXML .= $cellValue->getXml();
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellValue));
|
throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellValue));
|
||||||
}
|
}
|
||||||
|
60
tests/Spout/Writer/XLSX/FormulaTest.php
Normal file
60
tests/Spout/Writer/XLSX/FormulaTest.php
Normal 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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user