Skip to content

Commit a838c92

Browse files
author
Jeremy Jumeau
committed
feat() formula support see box#612
1 parent 3681a34 commit a838c92

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "box/spout",
2+
"name": "jeremyjumeau/spout",
33
"description": "PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way",
44
"type": "library",
55
"keywords": ["php","read","write","csv","xlsx","ods","odf","open","office","excel","spreadsheet","scale","memory","stream","ooxml"],

src/Spout/Writer/XLSX/Formula.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Box\Spout\Writer\XLSX;
4+
5+
/**
6+
* Class Formula
7+
*
8+
* @package Box\Spout\Reader\XLSX
9+
*/
10+
class Formula
11+
{
12+
13+
14+
/**
15+
* @var string
16+
*/
17+
protected $formula;
18+
19+
/**
20+
* @var string
21+
*/
22+
protected $value = '0';
23+
24+
/**
25+
* Formula constructor.
26+
* @param string $formula
27+
* @param string $value
28+
*/
29+
public function __construct($formula, $value = '0')
30+
{
31+
$this->formula = $formula;
32+
$this->value = $value;
33+
}
34+
35+
public function getXml()
36+
{
37+
return '><f>' . $this->formula . '</f><v>' . $this->value . '</v></c>';
38+
}
39+
40+
}

src/Spout/Writer/XLSX/Internal/Worksheet.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Box\Spout\Common\Exception\InvalidArgumentException;
66
use Box\Spout\Common\Exception\IOException;
77
use Box\Spout\Common\Helper\StringHelper;
8+
use Box\Spout\Writer\XLSX\Formula;
89
use Box\Spout\Writer\Common\Helper\CellHelper;
910
use Box\Spout\Writer\Common\Internal\WorksheetInterface;
1011

@@ -184,7 +185,7 @@ protected function addNonEmptyRow($dataRow, $style)
184185

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

187-
foreach($dataRow as $cellValue) {
188+
foreach ($dataRow as $cellValue) {
188189
$rowXML .= $this->getCellXML($rowIndex, $cellNumber, $cellValue, $style->getId());
189190
$cellNumber++;
190191
}
@@ -227,6 +228,8 @@ protected function getCellXML($rowIndex, $cellNumber, $cellValue, $styleId)
227228
// NOTE: not appending to $cellXML is the right behavior!!
228229
$cellXML = '';
229230
}
231+
} else if ($cellValue instanceof Formula) {
232+
$cellXML .= $cellValue->getXml();
230233
} else {
231234
throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellValue));
232235
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Box\Spout\Writer\XLSX;
4+
5+
6+
use Box\Spout\Common\Type;
7+
use Box\Spout\Reader\ReaderFactory;
8+
use Box\Spout\Reader\XLSX\Sheet;
9+
use Box\Spout\TestUsingResource;
10+
use Box\Spout\Writer\WriterFactory;
11+
12+
class FormulaTest extends \PHPUnit_Framework_TestCase
13+
{
14+
15+
use TestUsingResource;
16+
17+
/**
18+
* @throws \Box\Spout\Common\Exception\IOException
19+
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
20+
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException
21+
* @throws \Box\Spout\Common\Exception\InvalidArgumentException
22+
* @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException
23+
*/
24+
public function testGetXml()
25+
{
26+
$fileName = 'test_formula.xlsx';
27+
$this->createGeneratedFolderIfNeeded($fileName);
28+
$resourcePath = $this->getGeneratedResourcePath($fileName);
29+
30+
$writer = WriterFactory::create(Type::XLSX);
31+
$writer->openToFile($resourcePath);
32+
33+
$writer->addRows([
34+
[1],
35+
[1],
36+
[1],
37+
[1],
38+
]);
39+
40+
$writer->addRow([
41+
new Formula('SUM(A1:A4)', 4)
42+
]);
43+
$writer->close();
44+
45+
46+
$reader = ReaderFactory::create(Type::XLSX);
47+
$reader->open($resourcePath);
48+
49+
$i = 0;
50+
/** @var Sheet $sheet */
51+
foreach ($reader->getSheetIterator() as $sheet) {
52+
foreach ($sheet->getRowIterator() as $item) {
53+
$i++;
54+
if ($i == 5) {
55+
static::assertEquals(4, $item[0]);
56+
}
57+
}
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)