Skip to Content
ClassesFFormula

Class: FFormula

This interface class provides methods to modify the behavior of the operation formula.

Extends

  • FBase.IFFormulaSheetsMixin

Accessors

lexerTreeBuilder

Get Signature

get lexerTreeBuilder(): LexerTreeBuilder

The tree builder for formula string.

Returns

LexerTreeBuilder

Methods

calculationEnd()

calculationEnd(callback): IDisposable

Listening calculation ends.

Parameters

ParameterTypeDescription
callback(functionsExecutedState) => void

Returns

IDisposable


calculationProcessing()

calculationProcessing(callback): IDisposable

Listening calculation processing.

Parameters

ParameterTypeDescription
callback(stageInfo) => void

Returns

IDisposable


calculationStart()

calculationStart(callback): IDisposable

Listening calculation starts.

Parameters

ParameterTypeDescription
callback(forceCalculation) => void

Returns

IDisposable


executeCalculation()

executeCalculation(): void

Start the calculation of the formula.

Returns

void


moveFormulaRefOffset()

moveFormulaRefOffset( formulaString, refOffsetX, refOffsetY, ignoreAbsolute?): string

Offsets the formula

Parameters

ParameterTypeDescription
formulaStringstring
refOffsetXnumber
refOffsetYnumber
ignoreAbsolute?booleandefault is false

Returns

string

Example

const result = moveFormulaRefOffset('sum(a1,b2)',1,1) // result is 'sum(b2,c3)'

onCalculationEnd()

onCalculationEnd(): Promise<void>

Returns

Promise<void>


registerAsyncFunction()

Call Signature

registerAsyncFunction( name, func, description?): IDisposable

Register a custom asynchronous formula function.

Parameters
ParameterTypeDescription
namestringThe name of the function to register. This will be used in formulas (e.g., =ASYNCFUNC())
funcIRegisterAsyncFunctionThe async implementation of the function
description?string-
Returns

IDisposable

A disposable object that will unregister the function when disposed

Example
univerAPI.getFormula().registerAsyncFunction('RANDOM_DELAYED', async () => { await new Promise(resolve => setTimeout(resolve, 500)); return Math.random(); }, 'Mock a random number generation function' ); // Use in cell univerAPI.getActiveWorkbook().getActiveSheet().getRange('A1').setValue({ f: '=RANDOM_DELAYED()' }); // After 0.5 second, A1 will display a random number

Call Signature

registerAsyncFunction( name, func, options): IDisposable

Register a custom asynchronous formula function with description.

Parameters
ParameterTypeDescription
namestringThe name of the function to register. This will be used in formulas (e.g., =ASYNCFUNC())
funcIRegisterAsyncFunctionThe async implementation of the function
options{ description: any; locales: ILocales; }Object containing locales and description
options.description?anyObject containing description
options.locales?ILocalesObject containing locales
Returns

IDisposable

A disposable object that will unregister the function when disposed

Example
// Mock a user score fetching function univerAPI.getFormula().registerAsyncFunction('FETCH_USER_SCORE', async (userId) => { await new Promise(resolve => setTimeout(resolve, 1000)); // Mock fetching user score from database return userId * 10 + Math.floor(Math.random() * 20); }, { description: 'customFunction.description.FETCH_USER_SCORE', locales: { 'zhCN': { 'customFunction': { 'description': { 'FETCH_USER_SCORE': '从数据库中获取用户分数' } } }, 'enUS': { 'customFunction': { 'description': { 'FETCH_USER_SCORE': 'Mock fetching user score from database' } } } } } ); // Use in cell univerAPI.getActiveWorkbook().getActiveSheet().getRange('A1').setValue({ f: '=FETCH_USER_SCORE(42)' }); // After 1 second, A1 will display a score

registerFunction()

Call Signature

registerFunction( name, func, description?): IDisposable

Register a custom synchronous formula function.

Parameters
ParameterTypeDescription
namestringThe name of the function to register. This will be used in formulas (e.g., =MYFUNC())
funcIRegisterFunctionThe implementation of the function
description?stringA string describing the function’s purpose and usage
Returns

IDisposable

A disposable object that will unregister the function when disposed

Examples
univerAPI.getFormula().registerFunction('HELLO', (name) => `Hello, ${name}!`, 'A simple greeting function'); // Use the function in a cell univerAPI.getActiveWorkbook().getActiveSheet().getRange('A1').setValue('World'); univerAPI.getActiveWorkbook().getActiveSheet().getRange('A2').setValue({ f: '=HELLO(A1)' }); // A2 will display: "Hello, World!"
univerAPI.getFormula().registerFunction( 'DISCOUNT', (price, discountPercent) => price * (1 - discountPercent / 100), 'Calculates final price after discount' ); // Use in cell univerAPI.getActiveWorkbook().getActiveSheet().getRange('A1').setValue(100); univerAPI.getActiveWorkbook().getActiveSheet().getRange('A2').setValue({ f: '=DISCOUNT(A1, 20)' }); // A2 will display: 80
// Registered formulas support lambda functions univerAPI.getFormula().registerFunction('CUSTOMSUM', (...variants) => { let sum = 0; const last = variants[variants.length - 1]; if (last.isLambda && last.isLambda()) { variants.pop(); const variantsList = variants.map((variant) => Array.isArray(variant) ? variant[0][0]: variant); sum += last.executeCustom(...variantsList).getValue(); } for (const variant of variants) { sum += Number(variant) || 0; } return sum; }, 'Adds its arguments');

Call Signature

registerFunction( name, func, options): IDisposable

Register a custom synchronous formula function with localization support.

Parameters
ParameterTypeDescription
namestringThe name of the function to register. This will be used in formulas (e.g., =MYFUNC())
funcIRegisterFunctionThe implementation of the function
options{ description: any; locales: ILocales; }Object containing locales and description
options.description?anyObject containing description
options.locales?ILocalesObject containing locales
Returns

IDisposable

A disposable object that will unregister the function when disposed

Example
univerAPI.getFormula().registerFunction('HELLO', (name) => { return `Hello, ${name}!`; }, { description: 'customFunction.HELLO.description', locales: { 'zhCN': { 'customFunction' : { 'HELLO' : { 'description': '一个简单的问候函数' } } }, 'enUS': { 'customFunction' : { 'HELLO' : { 'description': 'A simple greeting function' } } } } } ); // Use in cell univerAPI.getActiveWorkbook().getActiveSheet().getRange('A1').setValue({ f: '=HELLO("John")' }); // A1 will display: "Hello, John!"

sequenceNodesBuilder()

sequenceNodesBuilder(formulaString): any[]

Resolves the formula string to a ‘node’ node

Parameters

ParameterTypeDescription
formulaStringstring

Returns

any[]

{((string | ISequenceNode)[])}

Memberof

FFormula


setInitialFormulaComputing()

setInitialFormulaComputing(calculationMode): void

Update the calculation mode of the formula.

Parameters

ParameterTypeDescription
calculationModeCalculationMode

Returns

void


setMaxIteration()

setMaxIteration(maxIteration): void

When a formula contains a circular reference, set the maximum number of iterations for the formula calculation.

Parameters

ParameterTypeDescription
maxIterationnumberThe maximum number of iterations. The default value is 1.

Returns

void

Example

// Set the maximum number of iterations for the formula calculation to 5. // The default value is 1. const formulaEngine = univerAPI.getFormula(); formulaEngine.setMaxIteration(5);

stopCalculation()

stopCalculation(): void

Stop the calculation of the formula.

Returns

void