Univer Docs API

GitHubEdit on GitHub

Univer Docs provides professional document typesetting capabilities, with concepts closely aligned with Microsoft Word.

Operate Documents

In the command system, unitId corresponds to the unique identifier of the document, and documents do not have a subUnitId.

The text content of the document is stored in the body.dataStream string, which does not contain style information.

Style information is stored separately in another data structure and is associated with the text content through index references.

For elements such as line breaks, page breaks, sections, paragraphs, and tables, different special characters are used to mark them in the text content. These special characters are converted to corresponding elements during rendering.

Want to learn more about the design of document data structures? We recommend reading Univer Document Architecture and Module Design and An Initial Exploration of Univer Document Typesetting Design.

Create Document

The univer.createUnit method is used to create a new document instance. When the first parameter is UniverInstanceType.UNIVER_DOC, it indicates that a document instance is being created.

The second parameter is an optional DocumentDataModel object that contains the initial data for the document. If {} is passed, an empty document is created.

The creation returns a DocumentDataModel object that contains the unique identifier unitId of the document and other related information.

univer.createUnit(UniverInstanceType.UNIVER_DOC, data)

Get Document unitId

const doc = univerAPI.getActiveDocument()
const unitId = doc?.getId()

Get Document Data

const doc = univerAPI.getActiveDocument()
const saveData = doc.getSnapshot()

Dispose Document

When we no longer need the document, we can call the dispose method of the Univer instance to destroy the instance.

univer.dispose()

Operate Text

Modify text elements in the rich text area

Insert Text

To append specified text to the end of this text area.

const doc = univerAPI.getActiveDocument()
doc?.appendText('Univer')

Delete Text

Delete the character before the cursor

univerAPI.executeCommand('doc.command.delete-left')

Delete the character after the cursor

univerAPI.executeCommand('doc.command.delete-right')

Modify Style

Set the font color for the selected text

univerAPI.executeCommand('doc.command.set-inline-format-text-color', { value: '#ff0000' })

Insert Page Break

The \f character is a page break, used to insert a page break in the document.

const doc = univerAPI.getActiveDocument()
doc?.appendText('\f')

Insert List

Insert an unordered list at the cursor's current paragraph; repeated calls will toggle the unordered list off.

univerAPI.executeCommand('doc.command.list-operation', { listType: 'BULLET_LIST' })

Insert an ordered list at the cursor's current paragraph; repeated calls will toggle the ordered list off.

univerAPI.executeCommand('doc.command.list-operation', { listType: 'ORDER_LIST' })