Skip to content

Commit feffb76

Browse files
committed
Added Column Formula
Option to add column formula that applied automatically for any new rows added to the table range
1 parent bc6ec19 commit feffb76

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

samples/Table/03_Column_Formula.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\IOFactory;
4+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
5+
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
6+
7+
require __DIR__ . '/../Header.php';
8+
9+
// Create new Spreadsheet object
10+
$helper->log('Create new Spreadsheet object');
11+
$spreadsheet = new Spreadsheet();
12+
13+
// Set document properties
14+
$helper->log('Set document properties');
15+
$spreadsheet->getProperties()->setCreator('aswinkumar863')
16+
->setLastModifiedBy('aswinkumar863')
17+
->setTitle('PhpSpreadsheet Table Test Document')
18+
->setSubject('PhpSpreadsheet Table Test Document')
19+
->setDescription('Test document for PhpSpreadsheet, generated using PHP classes.')
20+
->setKeywords('office PhpSpreadsheet php')
21+
->setCategory('Table');
22+
23+
// Create the worksheet
24+
$helper->log('Add data');
25+
26+
$spreadsheet->setActiveSheetIndex(0);
27+
28+
$columnFormula = '=SUM(Sales_Data[[#This Row],[Q1]:[Q4]])';
29+
30+
$dataArray = [
31+
['Year', 'Country', 'Q1', 'Q2', 'Q3', 'Q4', 'Sales'],
32+
[2010, 'Belgium', 380, 390, 420, 460, $columnFormula],
33+
[2010, 'France', 510, 490, 460, 590, $columnFormula],
34+
[2010, 'Germany', 720, 680, 640, 660, $columnFormula],
35+
[2010, 'Italy', 440, 410, 420, 450, $columnFormula],
36+
[2010, 'Spain', 510, 490, 470, 420, $columnFormula],
37+
[2010, 'UK', 690, 610, 620, 600, $columnFormula],
38+
[2010, 'United States', 790, 730, 860, 850, $columnFormula],
39+
[2011, 'Belgium', 400, 350, 450, 500, $columnFormula],
40+
[2011, 'France', 620, 650, 415, 570, $columnFormula],
41+
[2011, 'Germany', 680, 620, 710, 690, $columnFormula],
42+
[2011, 'Italy', 430, 370, 350, 335, $columnFormula],
43+
[2011, 'Spain', 460, 390, 430, 415, $columnFormula],
44+
[2011, 'UK', 720, 650, 580, 510, $columnFormula],
45+
[2011, 'United States', 800, 700, 900, 950, $columnFormula],
46+
];
47+
48+
$spreadsheet->getActiveSheet()->fromArray($dataArray, null, 'A1');
49+
50+
// Create Table
51+
$helper->log('Create Table');
52+
$table = new Table();
53+
$table->setName('Sales_Data');
54+
$table->setRange('A1:G15');
55+
56+
// Set Column Formula
57+
$table->getColumn('G')->setColumnFormula($columnFormula);
58+
59+
// Add Table to Worksheet
60+
$helper->log('Add Table to Worksheet');
61+
$spreadsheet->getActiveSheet()->addTable($table);
62+
63+
// Save
64+
$path = $helper->getFilename(__FILE__);
65+
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
66+
67+
// Disable precalculation to add table's total row
68+
$writer->setPreCalculateFormulas(false);
69+
$callStartTime = microtime(true);
70+
$writer->save($path);
71+
$helper->logWrite($writer, $path, $callStartTime);
72+
$helper->logEndingNotes();

src/PhpSpreadsheet/Worksheet/Table/Column.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ class Column
4141
*/
4242
private $totalsRowFormula;
4343

44+
/**
45+
* Column Formula.
46+
*
47+
* @var string
48+
*/
49+
private $columnFormula;
50+
4451
/**
4552
* Table.
4653
*
@@ -178,6 +185,28 @@ public function setTotalsRowFormula(string $totalsRowFormula)
178185
return $this;
179186
}
180187

188+
/**
189+
* Get column Formula.
190+
*
191+
* @return string
192+
*/
193+
public function getColumnFormula()
194+
{
195+
return $this->columnFormula;
196+
}
197+
198+
/**
199+
* Set column Formula.
200+
*
201+
* @return $this
202+
*/
203+
public function setColumnFormula(string $columnFormula)
204+
{
205+
$this->columnFormula = $columnFormula;
206+
207+
return $this;
208+
}
209+
181210
/**
182211
* Get this Column's Table.
183212
*

src/PhpSpreadsheet/Writer/Xlsx/Table.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ public function writeTable(WorksheetTable $table, $tableRef)
8686
$objWriter->writeAttribute('totalsRowFunction', $column->getTotalsRowFunction());
8787
}
8888
}
89+
if ($column->getColumnFormula()) {
90+
$objWriter->writeElement('calculatedColumnFormula', $column->getColumnFormula());
91+
}
92+
8993
$objWriter->endElement();
9094
}
9195
$objWriter->endElement();

tests/PhpSpreadsheetTests/Worksheet/Table/ColumnTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public function testVariousSets(): void
7474
$result = $column->setTotalsRowFunction($function);
7575
self::assertInstanceOf(Column::class, $result);
7676
self::assertEquals($function, $column->getTotalsRowFunction());
77+
78+
$formula = '=SUM(Sales_Data[[#This Row],[Q1]:[Q4]])';
79+
$result = $column->setColumnFormula($formula);
80+
self::assertInstanceOf(Column::class, $result);
81+
self::assertEquals($formula, $column->getColumnFormula());
7782
}
7883

7984
public function testTable(): void

0 commit comments

Comments
 (0)