公式
Bases 公式字段根据表数据计算记录值。UniverBasesPlugin 会自动注册 公式引擎依赖。
注册公式支持
TypeScript
import { UniverBasesPlugin } from '@univerjs-pro/bases'import { UniverBasesUIPlugin } from '@univerjs-pro/bases-ui'univer.registerPlugin(UniverBasesPlugin)univer.registerPlugin(UniverBasesUIPlugin)创建公式字段
TypeScript
const formulaTableName = table.getFormulaName()const total = table.addField('Total', univerAPI.Enum.BaseFieldType.Formula, { field: { config: { formula: `=SUM(${formulaTableName}[[#This Row],[Price]],${formulaTableName}[[#This Row],[Tax]])`, }, }, externalReferences: [],})每次写入公式字段都必须提供 externalReferences。仅当公式只引用当前 Base 的字段时使用 []。跨 Unit 公式应使用 buildReference,并在该数组中传入所有稳定的来源绑定。
注意事项
- 公式字段的值由计算得出;请避免从应用代码直接写入公式单元格。
- 公式表达式使用
Table[[#This Row],[Price]]这样的结构化字段引用。 - 导入大型快照后,请先重新计算公式,再向用户显示结果。
你觉得这篇文档如何?