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

Commit

Permalink
Added Formula support
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-amazing committed Aug 8, 2018
1 parent 799ad93 commit 9276844
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
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 = '';
}
} elseif ($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 9276844

Please sign in to comment.