Skip to Content
ClassesFFormula

类: FFormula

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

继承

  • FBase.IFFormulaSheetsMixin

访问器

lexerTreeBuilder

Getter 签名

get lexerTreeBuilder(): LexerTreeBuilder

The tree builder for formula string.

返回

LexerTreeBuilder

方法

calculationEnd()

calculationEnd(callback): IDisposable

Listening calculation ends.

参数

参数类型描述
callback(functionsExecutedState) => voidThe callback function to be called when the formula calculation ends.

返回

IDisposable

The disposable instance.

示例

const formulaEngine = univerAPI.getFormula(); formulaEngine.calculationEnd((functionsExecutedState) => { console.log('Calculation end', functionsExecutedState); });

calculationProcessing()

calculationProcessing(callback): IDisposable

Listening calculation processing.

参数

参数类型描述
callback(stageInfo) => voidThe callback function to be called when the formula calculation is in progress.

返回

IDisposable

The disposable instance.

示例

const formulaEngine = univerAPI.getFormula(); formulaEngine.calculationProcessing((stageInfo) => { console.log('Calculation processing', stageInfo); });

calculationResultApplied()

calculationResultApplied(callback): IDisposable

Listens for the moment when formula-calculation results are applied.

This event fires after the engine completes a calculation cycle and dispatches a SetFormulaCalculationResultMutation. The callback is invoked during an idle frame to avoid blocking UI updates.

参数

参数类型描述
callback(result) => voidA function called with the calculation result payload once the result-application mutation is emitted.

返回

IDisposable

A disposable used to unsubscribe from the event.

示例

const formulaEngine = univerAPI.getFormula(); const dispose = formulaEngine.calculationResultApplied((result) => { console.log('Calculation results applied:', result); }); // Later… dispose.dispose();

calculationStart()

calculationStart(callback): IDisposable

Listening calculation starts.

参数

参数类型描述
callback(forceCalculation) => voidThe callback function to be called when the formula calculation starts.

返回

IDisposable

The disposable instance.

示例

const formulaEngine = univerAPI.getFormula(); formulaEngine.calculationStart((forceCalculation) => { console.log('Calculation start', forceCalculation); });

executeCalculation()

executeCalculation(): void

Start the calculation of the formula.

返回

void

示例

const formulaEngine = univerAPI.getFormula(); formulaEngine.executeCalculation();

executeFormulas()

executeFormulas(formulas, timeout?): Promise<IFormulaExecuteResultMap>

Execute a batch of formulas asynchronously and receive computed results.

Each formula cell is represented as a string array: [fullFormula, …subFormulas]

Where:

  • fullFormula (index 0) is the complete formula expression written in the cell. Example: “=SUM(A1:A10) + SQRT(D7)”.

  • subFormulas (index 1+) are optional decomposed expressions extracted from the full formula. Each of them can be independently computed by the formula engine.

    These sub-expressions can include:

    • Single-cell references: “A2”, “B2”, “C5”
    • Range references: “A1:A10”
    • Function calls: “SQRT(D7)”, “ABS(A2-B2)”
    • Any sub-formula that was parsed out of the original formula and can be evaluated on its own.

    The batch execution engine may use these sub-formulas for dependency resolution, incremental computation, or performance optimizations.

参数

参数类型描述
formulasIFormulaStringMapNested structure (unit → sheet → row → column) describing formulas and their decomposed sub-expressions.
timeout?numberOptional timeout in milliseconds. If no result is received within this period, the promise will be rejected.

返回

Promise<IFormulaExecuteResultMap>

A promise that resolves with the computed value map mirroring the input structure.

示例

const formulaEngine = univerAPI.getFormula(); const formulas = { Book1: { Sheet1: { 2: { 3: [ // Full formula: "=SUM(A1:A10) + SQRT(D7)", // Decomposed sub-formulas (each one can be evaluated independently): "SUM(A1:A10)", // sub-formula 1 "SQRT(D7)", // sub-formula 2 "A1:A10", // range reference "D7", // single-cell reference ], }, 4: { 5: [ "=A2 + B2 + SQRT(C5)", "A2", "B2", "SQRT(C5)", ], } }, }, }; const result = await formulaEngine.executeFormulas(formulas); console.log(result);

getAllDependencyTrees()

getAllDependencyTrees(timeout?): Promise<...[]>

Retrieve all formula dependency trees that were produced during the latest dependency-analysis run. This triggers a local dependency-calculation command and returns the complete set of dependency trees once the calculation finishes.

参数

参数类型描述
timeout?numberOptional timeout in milliseconds. If no result is received within this period, the promise will be rejected.

返回

Promise<…[]>

A promise that resolves with the array of dependency trees.

示例

const formulaEngine = univerAPI.getFormula(); // Fetch all dependency trees generated for the current workbook. const trees = await formulaEngine.getAllDependencyTrees(); console.log('All dependency trees:', trees);

getCellDependencyTree()

getCellDependencyTree(param, timeout?): Promise<any>

Retrieve the dependency tree of a specific cell. This triggers a local dependency-calculation command for the given unit, sheet, and cell location, and returns the computed dependency tree when the calculation is completed.

参数

参数类型描述
param{ column: number; row: number; sheetId: string; unitId: string; }The target cell location: - unitId The workbook ID. - sheetId The sheet ID. - row The zero-based row index. - column The zero-based column index.
param.columnnumber-
param.row?number-
param.sheetId?string-
param.unitId?string-
timeout?numberOptional timeout in milliseconds. If no result is received within this period, the promise will be rejected.

返回

Promise<any>

A promise that resolves with the dependency tree or undefined if no tree exists for that cell.

示例

const formulaEngine = univerAPI.getFormula(); // Query the dependency tree for cell B2 in a specific sheet. const tree = await formulaEngine.getCellDependencyTree({ unitId: 'workbook1', sheetId: 'sheet1', row: 1, column: 1, }); console.log('Cell dependency tree:', tree);

getFormulaExpressTree()

getFormulaExpressTree(formulaString, unitId): any

Parse a formula string and return its formula expression tree.

This API analyzes the syntactic structure of a formula and builds an expression tree that reflects how the formula is composed (functions, operators, ranges, and nested expressions), without performing calculation or dependency evaluation.

The returned tree is suitable for:

  • Formula structure visualization
  • Explaining complex formulas (e.g. LET / LAMBDA)
  • Debugging or inspecting formula composition
  • Building advanced formula tooling

参数

参数类型描述
formulaStringstringThe formula string to parse (with or without leading =)
unitIdstring-

返回

any

A formula expression tree describing the hierarchical structure of the formula

示例

const formulaEngine = univerAPI.getFormula(); const formula = '=LET(x,SUM(A1,B1,A1:B10),y,OFFSET(A1:B10,0,1),SUM(x,y)+x)+1'; const exprTree = formulaEngine.getFormulaExpressTree(formula); console.log(exprTree);

Example output (simplified):

{ "value": "let(x,sum(A1,B1,A1:B10),y,offset(A1:B10,0,1),sum(x,y)+x)+1", "children": [ { "value": "let(x,sum(A1,B1,A1:B10),y,offset(A1:B10,0,1),sum(x,y)+x)", "children": [ { "value": "sum(A1,B1,A1:B10)", "children": [ { "value": "A1:B10", "children": [] } ] }, { "value": "offset(A1:B10,0,1)", "children": [ { "value": "A1:B10", "children": [] } ] } ] } ] }

getInRangeFormulas()

getInRangeFormulas(unitRanges, timeout?): Promise<...[]>

Retrieve the dependency trees of all formulas inside the specified ranges. Unlike getRangeDependents, this API only returns formulas whose definitions physically reside within the queried ranges.

Internally this triggers the same dependency-calculation command but with isInRange = true, and the promise resolves when the results are ready.

参数

参数类型描述
unitRangesIUnitRange[]An array of workbook/sheet ranges defining the lookup boundaries: - unitId The workbook ID. - sheetId The sheet ID. - range The zero-based grid range.
timeout?numberOptional timeout in milliseconds. If no result is received within this period, the promise will be rejected.

返回

Promise<…[]>

A promise that resolves with an array of IFormulaDependencyTreeJson describing every formula found in the provided ranges along with their parent/child relationships.

示例

const formulaEngine = univerAPI.getFormula(); // Query all formulas that lie within A1:D20 in Sheet1. const formulasInRange = await formulaEngine.getInRangeFormulas([ { unitId: 'workbook1', sheetId: 'sheet1', range: { startRow: 0, endRow: 19, startColumn: 0, endColumn: 3 } } ]); console.log('Formulas inside range:', formulasInRange);

getRangeDependents()

getRangeDependents(unitRanges, timeout?): Promise<...[]>

Retrieve the full dependency trees for all formulas that depend on the specified ranges. This triggers a local dependency-calculation command and resolves once the calculation completes.

参数

参数类型描述
unitRangesIUnitRange[]An array of workbook/sheet ranges to query. Each range includes: - unitId The workbook ID. - sheetId The sheet ID. - range The row/column boundaries.
timeout?numberOptional timeout in milliseconds. If no result is received within this period, the promise will be rejected.

返回

Promise<…[]>

A promise that resolves with an array of IFormulaDependencyTreeJson representing formulas and their relationships within the dependency graph.

示例

const formulaEngine = univerAPI.getFormula(); // Query all formulas that depend on A1:B10 in Sheet1. const dependents = await formulaEngine.getRangeDependents([ { unitId: 'workbook1', sheetId: 'sheet1', range: { startRow: 0, endRow: 9, startColumn: 0, endColumn: 1 } } ]); console.log('Dependent formulas:', dependents);

getRangeDependentsAndInRangeFormulas()

getRangeDependentsAndInRangeFormulas(unitRanges, timeout?): Promise<IFormulaDependentsAndInRangeResults>

Retrieve both:

  1. the full dependency trees of all formulas that depend on the specified ranges, and
  2. the dependency trees of all formulas that physically reside inside the specified ranges.

This is a convenience API that combines the behaviors of getRangeDependents and getInRangeFormulas into a single call.

Internally, it triggers a local dependency-calculation command once and resolves when both result sets are available, avoiding duplicate calculations and event listeners.

参数

参数类型描述
unitRangesIUnitRange[]An array of workbook/sheet ranges to query. Each range includes: - unitId The workbook ID. - sheetId The sheet ID. - range The zero-based row/column boundaries.
timeout?numberOptional timeout in milliseconds. If the dependency calculation does not complete within this period, the promise will be rejected.

返回

Promise<IFormulaDependentsAndInRangeResults>

A promise that resolves with an object containing: - dependents: Dependency trees of all formulas that depend on the specified ranges (upstream consumers). - inRanges: Dependency trees of all formulas whose definitions are located inside the specified ranges.

示例

const formulaEngine = univerAPI.getFormula(); const result = await formulaEngine.getRangeDependentsAndInRangeFormulas([ { unitId: 'workbook1', sheetId: 'sheet1', range: { startRow: 0, endRow: 9, startColumn: 0, endColumn: 1 }, }, ]); console.log('Dependent formulas:', result.dependents); console.log('Formulas inside range:', result.inRanges);

moveFormulaRefOffset()

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

Offsets the formula

参数

参数类型描述
formulaStringstringThe formula string to offset
refOffsetXnumberThe offset column
refOffsetYnumberThe offset row
ignoreAbsolute?booleanWhether to ignore the absolute reference

返回

string

The offset formula string

示例

const formulaEngine = univerAPI.getFormula(); const result = formulaEngine.moveFormulaRefOffset('=SUM(A1,B2)', 1, 1); console.log(result);

onCalculationEnd()

onCalculationEnd(): Promise<void>

Waits for the formula calculation to complete.

返回

Promise<void>

This method returns a promise that resolves when the calculation is complete.


onCalculationResultApplied()

onCalculationResultApplied(): Promise<void>

Waits for formula-calculation results to be applied.

This method resolves under three conditions:

  1. A real calculation runs and the engine emits a “calculation started” signal, followed by a “calculation result applied” signal.
  2. No calculation actually starts within 500 ms — the method assumes there is nothing to wait for and resolves automatically.
  3. A global 30 s timeout triggers, in which case the promise rejects.

The API internally listens to both “calculation in progress” events and “calculation result applied” events, ensuring it behaves correctly whether formulas are recalculated or skipped due to cache/state.

返回

Promise<void>

A promise that resolves when calculation results are applied or when no calculation occurs within the start-detection window.

示例

const formulaEngine = univerAPI.getFormula(); // Wait for formula updates to apply before reading values. await formulaEngine.onCalculationResultApplied(); const value = sheet.getRange("C24").getValue(); console.log("Updated value:", value);

registerAsyncFunction()

调用签名

registerAsyncFunction( name, func, description?): IDisposable

Register a custom asynchronous formula function.

参数
参数类型描述
namestringThe name of the function to register. This will be used in formulas (e.g., =ASYNCFUNC()).
funcIRegisterAsyncFunctionThe async implementation of the function.
description?stringA string describing the function’s purpose and usage.
返回

IDisposable

A disposable object that will unregister the function when disposed.

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

调用签名

registerAsyncFunction( name, func, options?): IDisposable

Register a custom asynchronous formula function with description.

参数
参数类型描述
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.
返回

IDisposable

A disposable object that will unregister the function when disposed.

示例
// Mock a user score fetching function const formulaEngine = univerAPI.getFormula(); formulaEngine.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.FETCH_USER_SCORE.description', locales: { 'zhCN': { 'customFunction': { 'FETCH_USER_SCORE': { 'description': '从数据库中获取用户分数' } } }, 'enUS': { 'customFunction': { 'FETCH_USER_SCORE': { 'description': 'Mock fetching user score from database' } } } } } ); // Use the function in a cell const fWorkbook = univerAPI.getActiveWorkbook(); const fWorksheet = fWorkbook.getActiveSheet(); const cellA1 = fWorksheet.getRange('A1'); cellA1.setValue({ f: '=FETCH_USER_SCORE(42)' }); // After 1 second, A1 will display a score

registerFunction()

调用签名

registerFunction( name, func, description?): IDisposable

Register a custom synchronous formula function.

参数
参数类型描述
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.
返回

IDisposable

A disposable object that will unregister the function when disposed.

Examples
// Register a simple greeting function const formulaEngine = univerAPI.getFormula(); formulaEngine.registerFunction( 'HELLO', (name) => `Hello, ${name}!`, 'A simple greeting function' ); // Use the function in a cell const fWorkbook = univerAPI.getActiveWorkbook(); const fWorksheet = fWorkbook.getActiveSheet(); const cellA1 = fWorksheet.getRange('A1'); cellA1.setValue('World'); const cellA2 = fWorksheet.getRange('A2'); cellA2.setValue({ f: '=HELLO(A1)' }); // A2 will display: "Hello, World!" formulaEngine.calculationEnd((functionsExecutedState) => { if (functionsExecutedState === 3) { console.log(cellA2.getValue()); // Hello, World! } })
// Register a discount calculation function const formulaEngine = univerAPI.getFormula(); formulaEngine.registerFunction( 'DISCOUNT', (price, discountPercent) => price * (1 - discountPercent / 100), 'Calculates final price after discount' ); // Use the function in a cell const fWorkbook = univerAPI.getActiveWorkbook(); const fWorksheet = fWorkbook.getActiveSheet(); const cellA1 = fWorksheet.getRange('A1'); cellA1.setValue(100); const cellA2 = fWorksheet.getRange('A2'); cellA2.setValue({ f: '=DISCOUNT(A1, 20)' }); // A2 will display: 80 formulaEngine.calculationEnd((functionsExecutedState) => { if (functionsExecutedState === 3) { console.log(cellA2.getValue()); // 80 } })
// Registered formulas support lambda functions const formulaEngine = univerAPI.getFormula(); formulaEngine.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' ); // Use the function in a cell const fWorkbook = univerAPI.getActiveWorkbook(); const fWorksheet = fWorkbook.getActiveSheet(); const cellA1 = fWorksheet.getRange('A1'); cellA1.setValue(1); const cellA2 = fWorksheet.getRange('A2'); cellA2.setValue(2); const cellA3 = fWorksheet.getRange('A3'); cellA3.setValue({ f: '=CUSTOMSUM(A1,A2,LAMBDA(x,y,x*y))' }); // A3 will display: 5 formulaEngine.calculationEnd((functionsExecutedState) => { if (functionsExecutedState === 3) { console.log(cellA3.getValue()); // 5 } })

调用签名

registerFunction( name, func, options?): IDisposable

Register a custom synchronous formula function with localization support.

参数
参数类型描述
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.
返回

IDisposable

A disposable object that will unregister the function when disposed.

示例
// Register a simple greeting function const formulaEngine = univerAPI.getFormula(); formulaEngine.registerFunction( 'HELLO', (name) => `Hello, ${name}!`, { description: 'customFunction.HELLO.description', locales: { 'zhCN': { 'customFunction': { 'HELLO': { 'description': '一个简单的问候函数' } } }, 'enUS': { 'customFunction': { 'HELLO': { 'description': 'A simple greeting function' } } } } } ); // Use the function in a cell const fWorkbook = univerAPI.getActiveWorkbook(); const fWorksheet = fWorkbook.getActiveSheet(); const cellA1 = fWorksheet.getRange('A1'); cellA1.setValue('World'); const cellA2 = fWorksheet.getRange('A2'); cellA2.setValue({ f: '=HELLO(A1)' }); // A2 will display: "Hello, World!" formulaEngine.calculationEnd((functionsExecutedState) => { if (functionsExecutedState === 3) { console.log(cellA2.getValue()); // Hello, World! } })

sequenceNodesBuilder()

sequenceNodesBuilder(formulaString): any[]

Resolves the formula string to a ‘node’ node

参数

参数类型描述
formulaStringstringThe formula string to resolve

返回

any[]

The nodes of the formula string

示例

const formulaEngine = univerAPI.getFormula(); const nodes = formulaEngine.sequenceNodesBuilder('=SUM(A1,B2)'); console.log(nodes);

setFormulaReturnDependencyTree()

setFormulaReturnDependencyTree(value): void

Enable or disable emitting formula dependency trees after each formula calculation.

When enabled, the formula engine will emit the dependency trees produced by each completed formula calculation through the internal command system. Consumers can obtain the result by listening for the corresponding calculation-result command.

When disabled, dependency trees will not be emitted.

This option only controls whether dependency trees are exposed. It does not affect formula calculation behavior.

参数

参数类型描述
valuebooleanWhether to emit formula dependency trees after calculation. - true: Emit dependency trees after each calculation. - false: Do not emit dependency trees (default behavior).

返回

void

示例

const formulaEngine = univerAPI.getFormula(); // Enable dependency tree emission formulaEngine.setFormulaReturnDependencyTree(true); // Listen for dependency trees produced by formula calculation const trees = await new Promise<IFormulaDependencyTreeJson[]>((resolve, reject) => { const timer = setTimeout(() => { disposable.dispose(); reject(new Error('Timeout waiting for formula dependency trees')); }, 30_000); const disposable = commandService.onCommandExecuted((command) => { if (command.id !== SetFormulaDependencyCalculationResultMutation.id) { return; } clearTimeout(timer); disposable.dispose(); const params = command.params as ISetFormulaDependencyCalculationResultMutation; resolve(params.result ?? []); }); }); console.log('Dependency trees:', trees);

setInitialFormulaComputing()

setInitialFormulaComputing(calculationMode): void

Update the calculation mode of the formula. It will take effect the next time the Univer Sheet is constructed. The calculation mode only handles formulas data when the workbook initializes data.

参数

参数类型描述
calculationModeCalculationModeThe calculation mode of the formula.

返回

void

示例

const formulaEngine = univerAPI.getFormula(); formulaEngine.setInitialFormulaComputing(0);

setMaxIteration()

setMaxIteration(maxIteration): void

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

参数

参数类型描述
maxIterationnumberThe maximum number of iterations. The default value is 1.

返回

void

示例

// 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.

返回

void

示例

const formulaEngine = univerAPI.getFormula(); formulaEngine.stopCalculation();

whenComputingCompleteAsync()

whenComputingCompleteAsync(timeout?): Promise<boolean>

参数

参数类型描述
timeout?numberThe maximum time to wait for the computing to complete, in milliseconds. The default value is 30,000 milliseconds.

返回

Promise<boolean>

This method returns true if the computing is complete. If the timeout is reached, this method returns false.

已被弃用

Use onCalculationEnd instead. Wait for computing in the Univer instance to complete. Please note that this does not only include formula calculation, but also other computing tasks, e.g. pivot table calculation.

示例

const formulaEngine = univerAPI.getFormula(); formulaEngine.whenComputingCompleteAsync(3000).then((isComplete) => { console.log('Computing complete:', isComplete); });