\PhpOffice\PhpSpreadsheet\CalculationCalculation

Summary

Methods
Properties
Constants
__construct()
getInstance()
flushInstance()
getDebugLog()
__clone()
getTRUE()
getFALSE()
setArrayReturnType()
getArrayReturnType()
getCalculationCacheEnabled()
setCalculationCacheEnabled()
enableCalculationCache()
disableCalculationCache()
clearCalculationCache()
clearCalculationCacheForWorksheet()
renameCalculationCacheForWorksheet()
setBranchPruningEnabled()
enableBranchPruning()
disableBranchPruning()
clearBranchStore()
getLocale()
setLocale()
translateSeparator()
_translateFormulaToLocale()
_translateFormulaToEnglish()
localeFunc()
wrapResult()
unwrapResult()
calculate()
calculateCellValue()
parseFormula()
calculateFormula()
getValueFromCache()
saveValueToCache()
_calculateFormulaValue()
getMatrixDimensions()
extractCellRange()
extractNamedRange()
isImplemented()
getFunctions()
getImplementedFunctionNames()
$suppressFormulaErrors
$formulaError
$cyclicFormulaCount
$localeBoolean
CALCULATION_REGEXP_NUMBER
CALCULATION_REGEXP_STRING
CALCULATION_REGEXP_OPENBRACE
CALCULATION_REGEXP_FUNCTION
CALCULATION_REGEXP_CELLREF
CALCULATION_REGEXP_CELLREF_RELATIVE
CALCULATION_REGEXP_COLUMNRANGE_RELATIVE
CALCULATION_REGEXP_ROWRANGE_RELATIVE
CALCULATION_REGEXP_DEFINEDNAME
CALCULATION_REGEXP_ERROR
RETURN_ARRAY_AS_ERROR
RETURN_ARRAY_AS_VALUE
RETURN_ARRAY_AS_ARRAY
FORMULA_OPEN_FUNCTION_BRACE
FORMULA_CLOSE_FUNCTION_BRACE
FORMULA_STRING_QUOTE
raiseFormulaError()
No protected properties found
N/A
loadLocales()
translateFormula()
checkMatrixOperands()
resizeMatricesShrink()
resizeMatricesExtend()
showValue()
showTypeDetails()
convertMatrixReferences()
internalParseFormula()
dataTestReference()
processTokenStack()
validateBinaryOperand()
executeBinaryComparisonOperation()
strcmpLowercaseFirst()
strcmpAllowNull()
executeNumericBinaryOperation()
addCellReference()
getUnusedBranchStoreKey()
getTokensAsString()
evaluateDefinedName()
$returnArrayAsType
$instance
$spreadsheet
$calculationCache
$calculationCacheEnabled
$branchStoreKeyCounter
$branchPruningEnabled
$operators
$binaryOperators
$debugLog
$referenceHelper
$cyclicReferenceStack
$cellStack
$cyclicFormulaCounter
$cyclicFormulaCell
$delta
$localeLanguage
$validLocaleLanguages
$localeArgumentSeparator
$localeFunctions
$excelConstants
$phpSpreadsheetFunctions
$controlFunctions
$functionReplaceFromExcel
$functionReplaceToLocale
$functionReplaceFromLocale
$functionReplaceToExcel
$operatorAssociativity
$comparisonOperators
$operatorPrecedence
N/A

Constants

CALCULATION_REGEXP_NUMBER

CALCULATION_REGEXP_NUMBER = '[-+]?\\d*\\.?\\d+(e[-+]?\\d+)?'

Regular Expressions

CALCULATION_REGEXP_STRING

CALCULATION_REGEXP_STRING = '"(?:[^"]|"")*"'

CALCULATION_REGEXP_OPENBRACE

CALCULATION_REGEXP_OPENBRACE = '\\('

CALCULATION_REGEXP_FUNCTION

CALCULATION_REGEXP_FUNCTION = '@?(?:_xlfn\\.)?([\\p{L}][\\p{L}\\p{N}\\.]*)[\\s]*\\('

CALCULATION_REGEXP_CELLREF

CALCULATION_REGEXP_CELLREF = '((([^\\s,!&%^\\/\\*\\+<>=-]*)|(\'[^\']*\')|(\\"[^\\"]*\\"))!)?\\$?\\b([a-z]{1,3})\\$?(\\d{1,7})(?![\\w.])'

CALCULATION_REGEXP_CELLREF_RELATIVE

CALCULATION_REGEXP_CELLREF_RELATIVE = '((([^\\s\\(,!&%^\\/\\*\\+<>=-]*)|(\'[^\']*\')|(\\"[^\\"]*\\"))!)?(\\$?\\b[a-z]{1,3})(\\$?\\d{1,7})(?![\\w.])'

CALCULATION_REGEXP_COLUMNRANGE_RELATIVE

CALCULATION_REGEXP_COLUMNRANGE_RELATIVE = '(\\$?[a-z]{1,3}):(\\$?[a-z]{1,3})'

CALCULATION_REGEXP_ROWRANGE_RELATIVE

CALCULATION_REGEXP_ROWRANGE_RELATIVE = '(\\$?\\d{1,7}):(\\$?\\d{1,7})'

CALCULATION_REGEXP_DEFINEDNAME

CALCULATION_REGEXP_DEFINEDNAME = '((([^\\s,!&%^\\/\\*\\+<>=-]*)|(\'[^\']*\')|(\\"[^\\"]*\\"))!)?([_\\p{L}][_\\p{L}\\p{N}\\.]*)'

CALCULATION_REGEXP_ERROR

CALCULATION_REGEXP_ERROR = '\\#[A-Z][A-Z0_\\/]*[!\\?]?'

RETURN_ARRAY_AS_ERROR

RETURN_ARRAY_AS_ERROR = 'error'

constants

RETURN_ARRAY_AS_VALUE

RETURN_ARRAY_AS_VALUE = 'value'

RETURN_ARRAY_AS_ARRAY

RETURN_ARRAY_AS_ARRAY = 'array'

FORMULA_OPEN_FUNCTION_BRACE

FORMULA_OPEN_FUNCTION_BRACE = '{'

FORMULA_CLOSE_FUNCTION_BRACE

FORMULA_CLOSE_FUNCTION_BRACE = '}'

FORMULA_STRING_QUOTE

FORMULA_STRING_QUOTE = '"'

Properties

$suppressFormulaErrors

$suppressFormulaErrors : bool

Flag to determine how formula errors should be handled If true, then a user error will be triggered If false, then an exception will be thrown.

Type

bool

$formulaError

$formulaError : string

Error message for any error that was raised/thrown by the calculation engine.

Type

string

$cyclicFormulaCount

$cyclicFormulaCount : int

Number of iterations for cyclic formulae.

Type

int

$localeBoolean

$localeBoolean : string[]

Locale-specific translations for Excel constants (True, False and Null).

Type

string[]

$returnArrayAsType

$returnArrayAsType

$instance

$instance : \PhpOffice\PhpSpreadsheet\Calculation\Calculation

Instance of this class.

Type

Calculation

$spreadsheet

$spreadsheet : \PhpOffice\PhpSpreadsheet\Spreadsheet

Instance of the spreadsheet this Calculation Engine is using.

Type

Spreadsheet

$calculationCache

$calculationCache : array

Calculation cache.

Type

array

$calculationCacheEnabled

$calculationCacheEnabled : bool

Calculation cache enabled.

Type

bool

$branchStoreKeyCounter

$branchStoreKeyCounter : int

Used to generate unique store keys.

Type

int

$branchPruningEnabled

$branchPruningEnabled

$operators

$operators : array

List of operators that can be used within formulae The true/false value indicates whether it is a binary operator or a unary operator.

Type

array

$binaryOperators

$binaryOperators : array

List of binary operators (those that expect two operands).

Type

array

$debugLog

$debugLog : \PhpOffice\PhpSpreadsheet\Calculation\Engine\Logger

The debug log generated by the calculation engine.

Type

Logger

$referenceHelper

$referenceHelper : \PhpOffice\PhpSpreadsheet\ReferenceHelper

Reference Helper.

Type

ReferenceHelper

$cyclicReferenceStack

$cyclicReferenceStack : \PhpOffice\PhpSpreadsheet\Calculation\Engine\CyclicReferenceStack

An array of the nested cell references accessed by the calculation engine, used for the debug log.

Type

CyclicReferenceStack

$cellStack

$cellStack

$cyclicFormulaCounter

$cyclicFormulaCounter : int

Current iteration counter for cyclic formulae If the value is 0 (or less) then cyclic formulae will throw an exception, otherwise they will iterate to the limit defined here before returning a result.

Type

int

$cyclicFormulaCell

$cyclicFormulaCell

$delta

$delta : float

Epsilon Precision used for comparisons in calculations.

Type

float

$localeLanguage

$localeLanguage : string

The current locale setting.

Type

string

$validLocaleLanguages

$validLocaleLanguages : string[]

List of available locale settings Note that this is read for the locale subdirectory only when requested.

Type

string[]

$localeArgumentSeparator

$localeArgumentSeparator : string

Locale-specific argument separator for function arguments.

Type

string

$localeFunctions

$localeFunctions

$excelConstants

$excelConstants : string[]

Excel constant string translations to their PHP equivalents Constant conversion from text name/value to actual (datatyped) value.

Type

string[]

$phpSpreadsheetFunctions

$phpSpreadsheetFunctions

$controlFunctions

$controlFunctions

$functionReplaceFromExcel

$functionReplaceFromExcel

$functionReplaceToLocale

$functionReplaceToLocale

$functionReplaceFromLocale

$functionReplaceFromLocale

$functionReplaceToExcel

$functionReplaceToExcel

$operatorAssociativity

$operatorAssociativity

$comparisonOperators

$comparisonOperators

$operatorPrecedence

$operatorPrecedence

Methods

__construct()

__construct(?\PhpOffice\PhpSpreadsheet\Spreadsheet  $spreadsheet = null) : mixed

Parameters

?\PhpOffice\PhpSpreadsheet\Spreadsheet $spreadsheet

Returns

mixed —

getInstance()

getInstance(?\PhpOffice\PhpSpreadsheet\Spreadsheet  $spreadsheet = null) : self

Get an instance of this class.

Parameters

?\PhpOffice\PhpSpreadsheet\Spreadsheet $spreadsheet

Injected spreadsheet for working with a PhpSpreadsheet Spreadsheet object, or NULL to create a standalone claculation engine

Returns

self —

flushInstance()

flushInstance() : void

Flush the calculation cache for any existing instance of this class but only if a Calculation instance exists.

getDebugLog()

getDebugLog() : \PhpOffice\PhpSpreadsheet\Calculation\Engine\Logger

Get the Logger for this calculation engine instance.

Returns

\PhpOffice\PhpSpreadsheet\Calculation\Engine\Logger —

__clone()

__clone() : mixed

__clone implementation. Cloning should not be allowed in a Singleton!

Returns

mixed —

getTRUE()

getTRUE() : string

Return the locale-specific translation of TRUE.

Returns

string —

locale-specific translation of TRUE

getFALSE()

getFALSE() : string

Return the locale-specific translation of FALSE.

Returns

string —

locale-specific translation of FALSE

setArrayReturnType()

setArrayReturnType(string  $returnType) : bool

Set the Array Return Type (Array or Value of first element in the array).

Parameters

string $returnType

Array return type

Returns

bool —

Success or failure

getArrayReturnType()

getArrayReturnType() : string

Return the Array Return Type (Array or Value of first element in the array).

Returns

string —

$returnType Array return type

getCalculationCacheEnabled()

getCalculationCacheEnabled() : bool

Is calculation caching enabled?

Returns

bool —

setCalculationCacheEnabled()

setCalculationCacheEnabled(bool  $pValue) : void

Enable/disable calculation cache.

Parameters

bool $pValue

enableCalculationCache()

enableCalculationCache() : void

Enable calculation cache.

disableCalculationCache()

disableCalculationCache() : void

Disable calculation cache.

clearCalculationCache()

clearCalculationCache() : void

Clear calculation cache.

clearCalculationCacheForWorksheet()

clearCalculationCacheForWorksheet(string  $worksheetName) : void

Clear calculation cache for a specified worksheet.

Parameters

string $worksheetName

renameCalculationCacheForWorksheet()

renameCalculationCacheForWorksheet(string  $fromWorksheetName, string  $toWorksheetName) : void

Rename calculation cache for a specified worksheet.

Parameters

string $fromWorksheetName
string $toWorksheetName

setBranchPruningEnabled()

setBranchPruningEnabled(mixed  $enabled) : void

Enable/disable calculation cache.

Parameters

mixed $enabled

enableBranchPruning()

enableBranchPruning() : void

disableBranchPruning()

disableBranchPruning() : void

clearBranchStore()

clearBranchStore() : void

getLocale()

getLocale() : string

Get the currently defined locale code.

Returns

string —

setLocale()

setLocale(string  $locale) : bool

Set the locale code.

Parameters

string $locale

The locale to use for formula translation, eg: 'en_us'

Returns

bool —

translateSeparator()

translateSeparator(string  $fromSeparator, string  $toSeparator, string  $formula, bool  $inBraces) : string

Parameters

string $fromSeparator
string $toSeparator
string $formula
bool $inBraces

Returns

string —

_translateFormulaToLocale()

_translateFormulaToLocale(mixed  $formula) : mixed

Parameters

mixed $formula

Returns

mixed —

_translateFormulaToEnglish()

_translateFormulaToEnglish(mixed  $formula) : mixed

Parameters

mixed $formula

Returns

mixed —

localeFunc()

localeFunc(mixed  $function) : mixed

Parameters

mixed $function

Returns

mixed —

wrapResult()

wrapResult(mixed  $value) : mixed

Wrap string values in quotes.

Parameters

mixed $value

Returns

mixed —

unwrapResult()

unwrapResult(mixed  $value) : mixed

Remove quotes used as a wrapper to identify string values.

Parameters

mixed $value

Returns

mixed —

calculate()

calculate(\PhpOffice\PhpSpreadsheet\Cell\Cell  $pCell = null) : mixed

Calculate cell value (using formula from a cell ID) Retained for backward compatibility.

Parameters

\PhpOffice\PhpSpreadsheet\Cell\Cell $pCell

Cell to calculate

Returns

mixed —

calculateCellValue()

calculateCellValue(\PhpOffice\PhpSpreadsheet\Cell\Cell  $pCell = null, bool  $resetLog = true) : mixed

Calculate the value of a cell formula.

Parameters

\PhpOffice\PhpSpreadsheet\Cell\Cell $pCell

Cell to calculate

bool $resetLog

Flag indicating whether the debug log should be reset or not

Returns

mixed —

parseFormula()

parseFormula(string  $formula) : array|bool

Validate and parse a formula string.

Parameters

string $formula

Formula to parse

Returns

array|bool —

calculateFormula()

calculateFormula(string  $formula, string  $cellID = null, \PhpOffice\PhpSpreadsheet\Cell\Cell  $pCell = null) : mixed

Calculate the value of a formula.

Parameters

string $formula

Formula to parse

string $cellID

Address of the cell to calculate

\PhpOffice\PhpSpreadsheet\Cell\Cell $pCell

Cell to calculate

Returns

mixed —

getValueFromCache()

getValueFromCache(string  $cellReference, mixed  $cellValue) : bool

Parameters

string $cellReference
mixed $cellValue

Returns

bool —

saveValueToCache()

saveValueToCache(string  $cellReference, mixed  $cellValue) : void

Parameters

string $cellReference
mixed $cellValue

_calculateFormulaValue()

_calculateFormulaValue(string  $formula, string  $cellID = null, \PhpOffice\PhpSpreadsheet\Cell\Cell  $pCell = null) : mixed

Parse a cell formula and calculate its value.

Parameters

string $formula

The formula to parse and calculate

string $cellID

The ID (e.g. A3) of the cell that we are calculating

\PhpOffice\PhpSpreadsheet\Cell\Cell $pCell

Cell to calculate

Returns

mixed —

getMatrixDimensions()

getMatrixDimensions(array  $matrix) : int[]

Read the dimensions of a matrix, and re-index it with straight numeric keys starting from row 0, column 0.

Parameters

array $matrix

matrix operand

Returns

int[] —

An array comprising the number of rows, and number of columns

extractCellRange()

extractCellRange(string  $pRange = 'A1', \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet  $pSheet = null, bool  $resetLog = true) : mixed

Extract range values.

Parameters

string $pRange

String based range representation

\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $pSheet

Worksheet

bool $resetLog

Flag indicating whether calculation log should be reset or not

Returns

mixed —

Array of values in range if range contains more than one element. Otherwise, a single value is returned.

extractNamedRange()

extractNamedRange(string  $pRange = 'A1', \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet  $pSheet = null, bool  $resetLog = true) : mixed

Extract range values.

Parameters

string $pRange

String based range representation

\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $pSheet

Worksheet

bool $resetLog

Flag indicating whether calculation log should be reset or not

Returns

mixed —

Array of values in range if range contains more than one element. Otherwise, a single value is returned.

isImplemented()

isImplemented(string  $pFunction) : bool

Is a specific function implemented?

Parameters

string $pFunction

Function Name

Returns

bool —

getFunctions()

getFunctions() : array

Get a list of all implemented functions as an array of function objects.

Returns

array —

of Category

getImplementedFunctionNames()

getImplementedFunctionNames() : array

Get a list of implemented Excel function names.

Returns

array —

raiseFormulaError()

raiseFormulaError(mixed  $errorMessage) : mixed

Parameters

mixed $errorMessage

Returns

mixed —

loadLocales()

loadLocales() : void

translateFormula()

translateFormula(string[]  $from, string[]  $to, string  $formula, string  $fromSeparator, string  $toSeparator) : string

Parameters

string[] $from
string[] $to
string $formula
string $fromSeparator
string $toSeparator

Returns

string —

checkMatrixOperands()

checkMatrixOperands(mixed  $operand1, mixed  $operand2, int  $resize = 1) : array

Ensure that paired matrix operands are both matrices and of the same size.

Parameters

mixed $operand1

First matrix operand

mixed $operand2

Second matrix operand

int $resize

Flag indicating whether the matrices should be resized to match and (if so), whether the smaller dimension should grow or the larger should shrink. 0 = no resize 1 = shrink to fit 2 = extend to fit

Returns

array —

resizeMatricesShrink()

resizeMatricesShrink(mixed  $matrix1, mixed  $matrix2, int  $matrix1Rows, int  $matrix1Columns, int  $matrix2Rows, int  $matrix2Columns) : void

Ensure that paired matrix operands are both matrices of the same size.

Parameters

mixed $matrix1

First matrix operand

mixed $matrix2

Second matrix operand

int $matrix1Rows

Row size of first matrix operand

int $matrix1Columns

Column size of first matrix operand

int $matrix2Rows

Row size of second matrix operand

int $matrix2Columns

Column size of second matrix operand

resizeMatricesExtend()

resizeMatricesExtend(mixed  $matrix1, mixed  $matrix2, int  $matrix1Rows, int  $matrix1Columns, int  $matrix2Rows, int  $matrix2Columns) : void

Ensure that paired matrix operands are both matrices of the same size.

Parameters

mixed $matrix1

First matrix operand

mixed $matrix2

Second matrix operand

int $matrix1Rows

Row size of first matrix operand

int $matrix1Columns

Column size of first matrix operand

int $matrix2Rows

Row size of second matrix operand

int $matrix2Columns

Column size of second matrix operand

showValue()

showValue(mixed  $value) : mixed

Format details of an operand for display in the log (based on operand type).

Parameters

mixed $value

First matrix operand

Returns

mixed —

showTypeDetails()

showTypeDetails(mixed  $value) : null|string

Format type and details of an operand for display in the log (based on operand type).

Parameters

mixed $value

First matrix operand

Returns

null|string —

convertMatrixReferences()

convertMatrixReferences(string  $formula) : false|string

Parameters

string $formula

Returns

false|string —

False indicates an error

internalParseFormula()

internalParseFormula(string  $formula, ?\PhpOffice\PhpSpreadsheet\Cell\Cell  $pCell = null) : bool

Parameters

string $formula
?\PhpOffice\PhpSpreadsheet\Cell\Cell $pCell

Returns

bool —

dataTestReference()

dataTestReference(mixed  $operandData) : mixed

Parameters

mixed $operandData

Returns

mixed —

processTokenStack()

processTokenStack(mixed  $tokens, null|string  $cellID = null, ?\PhpOffice\PhpSpreadsheet\Cell\Cell  $pCell = null) : bool

Parameters

mixed $tokens
null|string $cellID
?\PhpOffice\PhpSpreadsheet\Cell\Cell $pCell

Returns

bool —

validateBinaryOperand()

validateBinaryOperand(mixed  $operand, mixed  $stack) : mixed

Parameters

mixed $operand
mixed $stack

Returns

mixed —

executeBinaryComparisonOperation()

executeBinaryComparisonOperation(null|string  $cellID, mixed  $operand1, mixed  $operand2, string  $operation, \PhpOffice\PhpSpreadsheet\Calculation\Token\Stack  $stack, bool  $recursingArrays = false) : mixed

Parameters

null|string $cellID
mixed $operand1
mixed $operand2
string $operation
\PhpOffice\PhpSpreadsheet\Calculation\Token\Stack $stack
bool $recursingArrays

Returns

mixed —

strcmpLowercaseFirst()

strcmpLowercaseFirst(null|string  $str1, null|string  $str2) : int

Compare two strings in the same way as strcmp() except that lowercase come before uppercase letters.

Parameters

null|string $str1

First string value for the comparison

null|string $str2

Second string value for the comparison

Returns

int —

strcmpAllowNull()

strcmpAllowNull(null|string  $str1, null|string  $str2) : int

PHP8.1 deprecates passing null to strcmp.

Parameters

null|string $str1

First string value for the comparison

null|string $str2

Second string value for the comparison

Returns

int —

executeNumericBinaryOperation()

executeNumericBinaryOperation(mixed  $operand1, mixed  $operand2, mixed  $operation, string  $matrixFunction, mixed  $stack) : bool|mixed

Parameters

mixed $operand1
mixed $operand2
mixed $operation
string $matrixFunction
mixed $stack

Returns

bool|mixed —

addCellReference()

addCellReference(array  $args, bool  $passCellReference, array|string  $functionCall, ?\PhpOffice\PhpSpreadsheet\Cell\Cell  $pCell = null) : array

Add cell reference if needed while making sure that it is the last argument.

Parameters

array $args
bool $passCellReference
array|string $functionCall
?\PhpOffice\PhpSpreadsheet\Cell\Cell $pCell

Returns

array —

getUnusedBranchStoreKey()

getUnusedBranchStoreKey() : mixed

Returns

mixed —

getTokensAsString()

getTokensAsString(mixed  $tokens) : mixed

Parameters

mixed $tokens

Returns

mixed —

evaluateDefinedName()

evaluateDefinedName(\PhpOffice\PhpSpreadsheet\Cell\Cell  $pCell, \PhpOffice\PhpSpreadsheet\DefinedName  $namedRange, \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet  $pCellWorksheet, \PhpOffice\PhpSpreadsheet\Calculation\Token\Stack  $stack) : mixed|string

Parameters

\PhpOffice\PhpSpreadsheet\Cell\Cell $pCell
\PhpOffice\PhpSpreadsheet\DefinedName $namedRange
\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $pCellWorksheet
\PhpOffice\PhpSpreadsheet\Calculation\Token\Stack $stack

Returns

mixed|string —