# 数式

- Human documentation: [https://docs.univer.ai/ja-JP/guides/bases/features/core/formulas](https://docs.univer.ai/ja-JP/guides/bases/features/core/formulas)

- Agent Markdown: [https://docs.univer.ai/ja-JP/guides/bases/features/core/formulas.md](https://docs.univer.ai/ja-JP/guides/bases/features/core/formulas.md)

- Requested language: `ja-JP`

- Content language: `ja-JP`

- Documentation version: `1.2.0`

- Source: [bases/features/core/formulas.ja-JP.mdx](https://github.com/dream-num/documentation/blob/dev/content/guides/bases/features/core/formulas.ja-JP.mdx)

---

Bases の数式フィールドは、テーブルデータからレコード値を計算します。`UniverBasesPlugin` は 数式エンジンへの依存関係を自動的に登録します。

## 数式機能を登録する

```ts
import { UniverBasesPlugin } from '@univerjs-pro/bases'
import { UniverBasesUIPlugin } from '@univerjs-pro/bases-ui'

univer.registerPlugin(UniverBasesPlugin)
univer.registerPlugin(UniverBasesUIPlugin)
```

## 数式フィールドを作成する

```ts
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 内のフィールドだけを参照する数式では `[]` を使用します。ユニット間数式では [`buildReference`](https://docs.univer.ai/ja-JP/reference/facade/formula.md#buildreference) を使用し、安定したすべてのソースバインディングをこの配列に渡してください。

## 注意事項

* 数式フィールドの値は計算によって導出されるため、アプリケーションコードから数式セルへ直接書き込まないでください。
* 数式では `Table[[#This Row],[Price]]` のような構造化フィールド参照を使用します。
* 大規模なスナップショットをインポートした後は、結果をユーザーに表示する前に数式を再計算してください。
