# Two-way binding

> Use Server SDK to connect HTML applications to spreadsheet data, formula results, and collaborative updates.

- Human documentation: [https://docs.univer.ai/overview/features/shared/binding](https://docs.univer.ai/overview/features/shared/binding)

- Agent Markdown: [https://docs.univer.ai/overview/features/shared/binding.md](https://docs.univer.ai/overview/features/shared/binding.md)

- Requested language: `en-US`

- Content language: `en-US`

- Documentation version: `1.6.1`

- Source: [features/shared/binding.mdx](https://github.com/dream-num/documentation/blob/dev/content/docs/features/shared/binding.mdx)

---

> [!WARNING: Experimental feature]
> Two-way binding is experimental. Future releases will include breaking changes; APIs and integration patterns may change.

Univer Server SDK connects ordinary HTML, CSS, and JavaScript pages to Sheet. The spreadsheet retains the data and formulas, while the page provides forms, metrics, charts, and business interactions. A change to an input on either side updates the other side. The SDK lets an application turn a spreadsheet model into a quote calculator, a dashboard, or an interactive business application.

## Example: an application beside its spreadsheet

The Project Summit example exposes growth, offer price, and the cash-payment mix as controls on the left, with the full financial workbook on the right. A change to an assumption updates the related formulas, metric cards, and charts. Users can also open the spreadsheet to inspect the inputs and the calculations behind them.

![Project Summit: business controls, metrics, and charts beside a linked Univer Sheets financial model](https://raw.githubusercontent.com/dream-num/documentation/dev/public/assets/features/binding/project-summit-app-sheets.png)

The screenshot shows a model running in a local browser. Shared editing and persistence use the collaboration integration described below. Click the image to view it at full size.

## One Sheet, multiple interfaces

The HTML view communicates with its host through the page runtime. Binding Engine reads and writes the actual Sheet content through its own headless Univer instance. Univer SDK handles formulas and collaboration, and the binding layer delivers changed values to the page.

```mermaid
flowchart LR
    P["HTML application<br/>Controls · metrics · charts"]
    E["Host + Binding Engine<br/>Headless Sheet"]
    S["Collaboration service<br/>Same Sheet Unit"]
    T["Sheet editor"]
    P <-->|"Read · write · subscribe"| E
    E <-->|"SDK collaboration"| S
    T <-->|"Edit · synchronize"| S
```

Bound pages also refresh when a user edits the spreadsheet directly or another user makes a collaborative change. A single HTML view can access several Units for which it is authorized. The host loads one Engine per Unit, and each Engine maintains its own headless Univer instance.

## Declare bindings in HTML

A data reference on an element keeps its text or control synchronized automatically. Cell references use the form `unitId:sheetId:A1`. Each ID is encoded separately with `encodeURIComponent`, and the worksheet ID must remain stable.

| Attribute | Purpose |
| --- | --- |
| `data-univer-cell-text` | Displays a cell value as the text of the element. |
| `data-univer-cell-model` | Synchronizes a cell and a form control in both directions. |
| `data-univer-cell-subscribe` | Declares a cell subscription for custom JavaScript rendering. |
| `data-univer-range-subscribe` | Declares a rectangular-range subscription for charts, lists, and other consumers of a matrix. |

For example, the unit price is in `B2`, the quantity is in `B3`, and `B4` holds `=B2*B3`. The following template fragment connects the two inputs and the total to those cells:

```html
<label>
  Unit price
  <input type="number" min="0" step="0.01"
    data-univer-cell-model="book:sheet:B2" />
</label>
<label>
  Quantity
  <input type="number" min="0" step="1"
    data-univer-cell-model="book:sheet:B3" />
</label>
<p>Total: <span data-univer-cell-text="book:sheet:B4"></span></p>
```

Replace `book` and `sheet` with real, authorized IDs, and then load the template through Renderer. The formulas are defined in Sheet, and the page only reads the results. Text inputs, number inputs, range inputs, checkboxes, textareas, and single-select controls bind according to their value types.

## Extend charts and interactions with JavaScript

The page runtime initializes `window.univerBinding` before any template script runs. Declare the chart range with `data-univer-range-subscribe`, and then call `subscribeRangeById(elementId, callback)` to receive the matrix and update the chart. Components that display a single value use `subscribeCellById()`.

Use `getCellState()` / `getRangeState()` for one-time reads, `setCellValue()` to write a scalar, and `insertRowsWithValues()` to insert rows and fill them in one operation. A subscription delivers the current state first and then every actual value change. Local edits, collaborative edits, and applied formula results can all refresh the subscription.

References read fixed coordinates and therefore do not follow the original record when rows or columns are inserted or deleted. A scalar write replaces any formula or rich text already present in the cell, so bind only cells that hold business input.

## Connect the host and Agent

The application provides a container, an HTML template, and `loadEngine`. The loader authorizes access to the Unit, configures collaboration, and returns a loaded Engine. Renderer owns the isolated page, the messaging between that page and the host, and the lifecycle of the Engines it delivers. This entry point accepts those three inputs:

```ts
import {
  renderHtmlView,
  type RenderHtmlViewOptions,
} from '@univerjs-labs/html-view-renderer/render';

export function createPreview(
  container: HTMLElement,
  html: string,
  loadEngine: RenderHtmlViewOptions['loadEngine'],
) {
  return renderHtmlView({ container, html, loadEngine });
}
```

Template generation, source editing, file storage, and business navigation remain the responsibility of the application. An Agent can generate HTML with binding declarations that reuse the Sheet data and calculation rules. Call `view.inspect.open()` to inspect the correspondence between a Unit, a Sheet, a reference, and a page element.

The host holds the collaboration credentials, and the server enforces access. Keep the Sheet UI and the Headless Engine in separate JavaScript environments, such as separate pages or iframes. The current binding packages cover Sheet.

## Separate local updates from save acknowledgement

| Operation | Meaning |
| --- | --- |
| Page write completes | The local operation has executed, but the server may not have acknowledged it yet. |
| `await view.flush()` | Submits draft values from controls, waits for earlier writes and the collaboration acknowledgement, and then allows editing to continue. |
| `await view.prepareToLeave()` | Pauses editing and saves. Leave once the save succeeds, or call `view.resume()` if the navigation is canceled. |
| `view.dispose()` | Releases connections, Engines, and subscriptions. It does not save any data. |

Subscriptions refresh the values on screen once formula results are applied. If a save fails, keep the page open and retry the save. A completed page write does not by itself mean that the data has been saved.

## Continue integrating

- [SDK repository and overview](https://github.com/dream-num/univer-data-binding-sdk)
- [Binding Engine API](https://github.com/dream-num/univer-data-binding-sdk/blob/main/packages/binding-engine/README.md)
- [HTML binding attributes](https://github.com/dream-num/univer-data-binding-sdk/blob/main/packages/html-view/README.md)
- [Renderer integration](https://github.com/dream-num/univer-data-binding-sdk/blob/main/packages/html-view-renderer/README.md)
- [Page JavaScript API](https://github.com/dream-num/univer-data-binding-sdk/blob/main/packages/html-view-renderer/docs/javascript-api.md)

Related capabilities: [headless and server calculation](https://docs.univer.ai/overview/features/shared/server-calculation.md) · [realtime collaboration](https://docs.univer.ai/overview/features/shared/collaboration.md) · [Agents and automation](https://docs.univer.ai/overview/features/shared/agents.md).

[Back to all features](https://docs.univer.ai/overview/features.md)
