公式
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]]这类结构化字段引用。 - 导入大型快照后,应先重新计算公式,再向用户展示结果。
你觉得这篇文档如何?