PhpSpreadsheet/docs/Overview/09-Calculation-Engine.md

53 lines
3.0 KiB
Markdown
Raw Normal View History

2016-11-27 15:51:44 +00:00
# PhpSpreadsheet Developer Documentation
2013-05-18 22:22:27 +00:00
2016-11-27 15:51:44 +00:00
## Using the PhpSpreadsheet calculation engine
2013-05-18 22:22:27 +00:00
### Performing formula calculations
2016-11-27 15:51:44 +00:00
As PhpSpreadsheet represents an in-memory spreadsheet, it also offers formula calculation capabilities. A cell can be of a value type (containing a number or text), or a formula type (containing a formula which can be evaluated). For example, the formula "=SUM(A1:A10)" evaluates to the sum of values in A1, A2, ..., A10.
2013-05-18 22:22:27 +00:00
To calculate a formula, you can call the cell containing the formulas method getCalculatedValue(), for example:
```php
2016-11-27 15:51:44 +00:00
$spreadsheet->getActiveSheet()->getCell('E11')->getCalculatedValue();
2013-05-18 22:22:27 +00:00
```
2016-11-27 15:51:44 +00:00
If you write the following line of code in the invoice demo included with PhpSpreadsheet, it evaluates to the value "64":
2013-05-18 22:22:27 +00:00
![09-command-line-calculation.png](./images/09-command-line-calculation.png "")
2016-11-27 15:51:44 +00:00
Another nice feature of PhpSpreadsheet's formula parser, is that it can automatically adjust a formula when inserting/removing rows/columns. Here's an example:
2013-05-18 22:22:27 +00:00
![09-formula-in-cell-1.png](./images/09-formula-in-cell-1.png "")
You see that the formula contained in cell E11 is "SUM(E4:E9)". Now, when I write the following line of code, two new product lines are added:
```php
2016-11-27 15:51:44 +00:00
$spreadsheet->getActiveSheet()->insertNewRowBefore(7, 2);
2013-05-18 22:22:27 +00:00
```
![09-formula-in-cell-2.png](./images/09-formula-in-cell-2.png "")
Did you notice? The formula in the former cell E11 (now E13, as I inserted 2 new rows), changed to "SUM(E4:E11)". Also, the inserted cells duplicate style information of the previous cell, just like Excel's behaviour. Note that you can both insert rows and columns.
### Known limitations
2016-11-27 15:51:44 +00:00
There are some known limitations to the PhpSpreadsheet calculation engine. Most of them are due to the fact that an Excel formula is converted into PHP code before being executed. This means that Excel formula calculation is subject to PHP's language characteristics.
2013-05-18 22:22:27 +00:00
#### Operator precedence
2016-11-27 15:51:44 +00:00
In Excel '+' wins over '&', just like '*' wins over '+' in ordinary algebra. The former rule is not what one finds using the calculation engine shipped with PhpSpreadsheet.
2013-05-18 22:22:27 +00:00
Reference for operator precedence in Excel: [http://support.microsoft.com/kb/25189][18]
Reference for operator precedence in PHP: [http://www.php.net/operators][19]
#### Formulas involving numbers and text
Formulas involving numbers and text may produce unexpected results or even unreadable file contents. For example, the formula '=3+"Hello "' is expected to produce an error in Excel (#VALUE!). Due to the fact that PHP converts “Hello” to a numeric value (zero), the result of this formula is evaluated as 3 instead of evaluating as an error. This also causes the Excel document being generated as containing unreadable content.
Reference for this behaviour in PHP: [http://be.php.net/manual/en/language.types.string.php#language.types.string.conversion][20]
[18]: http://support.microsoft.com/kb/25189
[19]: http://www.php.net/operators
[20]: http://be.php.net/manual/en/language.types.string.php#language.types.string.conversion