Overview
PhpSpreadsheet includes a powerful calculation engine that can evaluate Excel formulas. As PhpSpreadsheet represents an in-memory spreadsheet, it offers formula calculation capabilities that mirror Excel’s behavior.Basic Formula Evaluation
Getting Calculated Values
When you have a formula in a cell, you can retrieve its calculated value:getValue() on a cell containing a formula returns the formula itself, while getCalculatedValue() evaluates the formula and returns the result.
Checking if a Cell Contains a Formula
You can determine whether a cell contains a formula:Writing Formulas
Formula Syntax Rules
When writing formulas to cells, they must follow these rules:- Decimal separator is
.(period) - Function argument separator is
,(comma) - Matrix row separator is
;(semicolon) - English function names must be used
- Formulas must start with
=
Dynamic Formula Adjustment
PhpSpreadsheet automatically adjusts formulas when inserting or removing rows/columns:Cell Anchoring
Use$ to anchor cell references:
Calculation Cache
How Caching Works
Once the calculation engine evaluates a formula, the result is cached. Subsequent calls togetCalculatedValue() for the same cell return the cached result without re-evaluation.
Managing the Cache
Disable calculation caching:Date and Time Functions
Return Date Types
Date functions can return values in different formats:RETURNDATE_PHP_NUMERIC.
Excel Timestamps
Excel timestamps are stored as floating-point numbers:- Integer portion = number of days since base date
- Fraction = time of day (0 = midnight, 0.5 = noon)
Setting the Excel Calendar
Helper Methods
Convert between Excel and PHP date formats:Database Functions
PhpSpreadsheet supports Excel’s database functions like DAVERAGE, DCOUNT, DSUM, etc.DAVERAGE Example
Supported Functions
Function Categories
PhpSpreadsheet supports functions in these categories:- Database Functions (DAVERAGE, DCOUNT, DGET, DMAX, DMIN, etc.)
- Date and Time Functions (DATE, DATEVALUE, DAY, HOUR, MINUTE, etc.)
- Engineering Functions
- Financial Functions
- Logical Functions (IF, AND, OR, NOT, etc.)
- Lookup and Reference Functions (VLOOKUP, HLOOKUP, INDEX, MATCH, etc.)
- Math and Trigonometry Functions (SUM, AVERAGE, ROUND, etc.)
- Statistical Functions
- Text Functions (CONCATENATE, LEFT, RIGHT, MID, etc.)
For a complete list of supported functions, see the function list by name in the source documentation.
Known Limitations
Operator Precedence
In Excel,+ wins over &, just like * wins over +. PhpSpreadsheet’s calculation engine may not follow this precedence exactly.
Formulas with Mixed Types
Formulas involving numbers and text may produce unexpected results due to PHP’s type coercion:Array Arguments
While most Excel function implementations support array arguments, some functions may not handle arrays correctly and may return a single value or a#VALUE! error.
Precision As Displayed (PAD)
Precision As Displayed is not supported and there are no plans to support it.Best Practices
Use Calculation Cache Wisely
Use Calculation Cache Wisely
Leave caching enabled for better performance. Only disable or clear the cache when you’ve changed underlying data and need to re-evaluate formulas.
Follow Excel Formula Syntax
Follow Excel Formula Syntax
Always use English function names and Excel’s standard format. PhpSpreadsheet handles all formulas internally in this format.
Be Careful with Date Functions
Be Careful with Date Functions
Remember that date functions work with UST (Universal Standard Time) and don’t perform internal timezone conversions.
Test Formula Results
Test Formula Results
Due to differences between Excel and PHP, always test your formulas to ensure they produce expected results.

