\PhpOffice\PhpSpreadsheet\CalculationCalculation

Summary

Methods
Properties
Constants
__construct()
getInstance()
flushInstance()
getDebugLog()
__clone()
getTRUE()
getFALSE()
setArrayReturnType()
getArrayReturnType()
getCalculationCacheEnabled()
setCalculationCacheEnabled()
enableCalculationCache()
disableCalculationCache()
clearCalculationCache()
clearCalculationCacheForWorksheet()
renameCalculationCacheForWorksheet()
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_NAMEDRANGE
CALCULATION_REGEXP_ERROR
RETURN_ARRAY_AS_ERROR
RETURN_ARRAY_AS_VALUE
RETURN_ARRAY_AS_ARRAY
raiseFormulaError()
No protected properties found
N/A
loadLocales()
translateFormula()
checkMatrixOperands()
resizeMatricesShrink()
resizeMatricesExtend()
showValue()
showTypeDetails()
convertMatrixReferences()
mkMatrix()
_parseFormula()
dataTestReference()
processTokenStack()
validateBinaryOperand()
executeBinaryComparisonOperation()
strcmpLowercaseFirst()
executeNumericBinaryOperation()
addCellReference()
$returnArrayAsType
$instance
$spreadsheet
$calculationCache
$calculationCacheEnabled
$operators
$binaryOperators
$debugLog
$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+)?'

CALCULATION_REGEXP_STRING

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

CALCULATION_REGEXP_OPENBRACE

CALCULATION_REGEXP_OPENBRACE = '\('

CALCULATION_REGEXP_FUNCTION

CALCULATION_REGEXP_FUNCTION = '@?(?:_xlfn\.)?([A-Z][A-Z0-9\.]*)[\s]*\('

CALCULATION_REGEXP_CELLREF

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

CALCULATION_REGEXP_NAMEDRANGE

CALCULATION_REGEXP_NAMEDRANGE = '((([^\s,!&%^\/\*\+<>=-]*)|(\'[^\']*\')|(\"[^\"]*\"))!)?([_A-Z][_A-Z0-9\.]*)'

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'

Properties

$suppressFormulaErrors

$suppressFormulaErrors : boolean

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

boolean

$formulaError

$formulaError : string

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

Type

string

$cyclicFormulaCount

$cyclicFormulaCount : integer

Number of iterations for cyclic formulae.

Type

integer

$localeBoolean

$localeBoolean : array<mixed,string>

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

Type

array<mixed,string>

$returnArrayAsType

$returnArrayAsType : 

Type

$calculationCache

$calculationCache : array

Calculation cache.

Type

array

$calculationCacheEnabled

$calculationCacheEnabled : boolean

Calculation cache enabled.

Type

boolean

$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

$cyclicReferenceStack

$cyclicReferenceStack : array

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

Type

array — of string

$cellStack

$cellStack : 

Type

$cyclicFormulaCounter

$cyclicFormulaCounter : integer

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

integer

$cyclicFormulaCell

$cyclicFormulaCell : 

Type

$delta

$delta : float

Epsilon Precision used for comparisons in calculations.

Type

float

$localeLanguage

$localeLanguage : string

The current locale setting.

Type

string

$validLocaleLanguages

$validLocaleLanguages : array<mixed,string>

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

Type

array<mixed,string>

$localeArgumentSeparator

$localeArgumentSeparator : string

Locale-specific argument separator for function arguments.

Type

string

$localeFunctions

$localeFunctions : 

Type

$excelConstants

$excelConstants : array<mixed,string>

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

Type

array<mixed,string>

$phpSpreadsheetFunctions

$phpSpreadsheetFunctions : 

Type

$controlFunctions

$controlFunctions : 

Type

$functionReplaceFromExcel

$functionReplaceFromExcel : 

Type

$functionReplaceToLocale

$functionReplaceToLocale : 

Type

$functionReplaceFromLocale

$functionReplaceFromLocale : 

Type

$functionReplaceToExcel

$functionReplaceToExcel : 

Type

$operatorAssociativity

$operatorAssociativity : 

Type

$comparisonOperators

$comparisonOperators : 

Type

$operatorPrecedence

$operatorPrecedence : 

Type

Methods

flushInstance()

flushInstance() 

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

__clone()

__clone() 

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

Throws

\PhpOffice\PhpSpreadsheet\Calculation\Exception

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) : boolean

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

Parameters

string $returnType

Array return type

Returns

boolean —

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() : boolean

Is calculation caching enabled?

Returns

boolean

setCalculationCacheEnabled()

setCalculationCacheEnabled(boolean  $pValue) 

Enable/disable calculation cache.

Parameters

boolean $pValue

enableCalculationCache()

enableCalculationCache() 

Enable calculation cache.

disableCalculationCache()

disableCalculationCache() 

Disable calculation cache.

clearCalculationCache()

clearCalculationCache() 

Clear calculation cache.

clearCalculationCacheForWorksheet()

clearCalculationCacheForWorksheet(string  $worksheetName) 

Clear calculation cache for a specified worksheet.

Parameters

string $worksheetName

renameCalculationCacheForWorksheet()

renameCalculationCacheForWorksheet(string  $fromWorksheetName, string  $toWorksheetName) 

Rename calculation cache for a specified worksheet.

Parameters

string $fromWorksheetName
string $toWorksheetName

getLocale()

getLocale() : string

Get the currently defined locale code.

Returns

string

setLocale()

setLocale(string  $locale) : boolean

Set the locale code.

Parameters

string $locale

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

Returns

boolean

translateSeparator()

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

Parameters

string $fromSeparator
string $toSeparator
string $formula
boolean $inBraces

Returns

string

_translateFormulaToLocale()

_translateFormulaToLocale(  $formula) 

Parameters

$formula

_translateFormulaToEnglish()

_translateFormulaToEnglish(  $formula) 

Parameters

$formula

localeFunc()

localeFunc(  $function) 

Parameters

$function

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

Throws

\PhpOffice\PhpSpreadsheet\Calculation\Exception

Returns

mixed

calculateCellValue()

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

Calculate the value of a cell formula.

Parameters

\PhpOffice\PhpSpreadsheet\Cell\Cell $pCell

Cell to calculate

boolean $resetLog

Flag indicating whether the debug log should be reset or not

Throws

\PhpOffice\PhpSpreadsheet\Calculation\Exception

Returns

mixed

parseFormula()

parseFormula(string  $formula) : array

Validate and parse a formula string.

Parameters

string $formula

Formula to parse

Returns

array

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

Throws

\PhpOffice\PhpSpreadsheet\Calculation\Exception

Returns

mixed

getValueFromCache()

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

Parameters

string $cellReference
mixed $cellValue

Returns

boolean

saveValueToCache()

saveValueToCache(string  $cellReference, mixed  $cellValue) 

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

Throws

\PhpOffice\PhpSpreadsheet\Calculation\Exception

Returns

mixed

getMatrixDimensions()

getMatrixDimensions(array  $matrix) : array<mixed,integer>

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

Parameters

array $matrix

Returns

array<mixed,integer> —

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

extractCellRange()

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

Extract range values.

Parameters

$pRange
\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $pSheet

Worksheet

boolean $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(  $pRange = 'A1', \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet  $pSheet = null, boolean  $resetLog = true) : mixed

Extract range values.

Parameters

$pRange
\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $pSheet

Worksheet

boolean $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) : boolean

Is a specific function implemented?

Parameters

string $pFunction

Function Name

Returns

boolean

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(  $errorMessage) 

Parameters

$errorMessage

loadLocales()

loadLocales() 

translateFormula()

translateFormula(array<mixed,string>  $from, array<mixed,string>  $to, string  $formula, string  $fromSeparator, string  $toSeparator) : string

Parameters

array<mixed,string> $from
array<mixed,string> $to
string $formula
string $fromSeparator
string $toSeparator

Returns

string

checkMatrixOperands()

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

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

Parameters

$operand1
$operand2
integer $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(  $matrix1,   $matrix2, integer  $matrix1Rows, integer  $matrix1Columns, integer  $matrix2Rows, integer  $matrix2Columns) 

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

Parameters

$matrix1
$matrix2
integer $matrix1Rows

Row size of first matrix operand

integer $matrix1Columns

Column size of first matrix operand

integer $matrix2Rows

Row size of second matrix operand

integer $matrix2Columns

Column size of second matrix operand

resizeMatricesExtend()

resizeMatricesExtend(  $matrix1,   $matrix2, integer  $matrix1Rows, integer  $matrix1Columns, integer  $matrix2Rows, integer  $matrix2Columns) 

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

Parameters

$matrix1
$matrix2
integer $matrix1Rows

Row size of first matrix operand

integer $matrix1Columns

Column size of first matrix operand

integer $matrix2Rows

Row size of second matrix operand

integer $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) : string

Parameters

string $formula

Returns

string

mkMatrix()

mkMatrix(  $args) 

Parameters

$args

_parseFormula()

_parseFormula(string  $formula, null|\PhpOffice\PhpSpreadsheet\Cell\Cell  $pCell = null) : boolean

Parameters

string $formula
null|\PhpOffice\PhpSpreadsheet\Cell\Cell $pCell

Returns

boolean

dataTestReference()

dataTestReference(  $operandData) 

Parameters

$operandData

processTokenStack()

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

Parameters

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

Returns

boolean

validateBinaryOperand()

validateBinaryOperand(  $operand,   $stack) 

Parameters

$operand
$stack

executeBinaryComparisonOperation()

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

Parameters

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

Returns

boolean

strcmpLowercaseFirst()

strcmpLowercaseFirst(string  $str1, string  $str2) : integer

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

Parameters

string $str1

First string value for the comparison

string $str2

Second string value for the comparison

Returns

integer

executeNumericBinaryOperation()

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

Parameters

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

Returns

boolean

addCellReference()

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

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

Parameters

array $args
boolean $passCellReference
array|string $functionCall
null|\PhpOffice\PhpSpreadsheet\Cell\Cell $pCell

Returns

array