Univer Bases API
Learn how to use Facade API in a Univer Bases application.
Facade API is the application-facing layer built on the Bases model. It covers the common operations for creating bases, tables, fields, records, and views, as well as filters, sorts, groups, selections, and UI navigation.
Caution
Changes to the Base model are synchronous. UI activation methods return promises, so the caller must await them.
Enable facade entries
Import the facade entry for each Bases capability that the application uses:
import { FUniver } from '@univerjs/core/facade'import '@univerjs-pro/bases/facade'import '@univerjs-pro/bases-ui/facade'Create the API object only after the Univer instance and the required plugins are registered:
const univerAPI = FUniver.newAPI(univer)Create and read a base
createBase creates a Base unit through the facade layer. Code that operates at a lower level can create the unit directly with univer.createUnit(UniverInstanceType.UNIVER_BASE, snapshot).
const base = univerAPI.createBase({ id: 'base-demo', name: 'Base Demo',})const activeBase = univerAPI.getActiveBase()const sameBase = univerAPI.getBase(base.getId())Create a table
const table = base.insertTable('Tasks', { primaryFieldName: 'Title' })const status = table.addField('Status', univerAPI.Enum.BaseFieldType.Text)Work with records
const record = table.addRecord({ Title: 'Write docs', Status: 'In progress',}, univerAPI.Enum.BaseFieldKeyEnum.Name)record.setValue(status.getId(), 'Done')Work with UI state
@univerjs-pro/bases-ui/facade adds UI APIs for activating tables and views, managing selection, scrolling, editing, and panels.
const baseUI = univerAPI.getBaseUI()const view = table.getViews()[0]if (!view) throw new Error('No table view')await baseUI.activateTable(table.getId())await baseUI.activateView(view.getId())baseUI.setSelection({ type: 'grid-record', tableId: table.getId(), viewId: view.getId(), recordId: record.getId(),})Reference
How is this guide?