GuidesUniver DocsFeaturesCoreDoc API

Univer Docs API

Concepts

Univer documents will provide users with the ability to layout documents at a professional level, with concepts that are as consistent as possible with Word.

Document

The command system unitId corresponds to the unique identifier of the document; 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 in a separate data structure and is associated with the text content by subscript indexes.

For elements such as line breaks, page breaks, sections, paragraphs, tables, etc., the text content is marked with different special characters that are converted to the corresponding element at rendering time.

ℹī¸

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

Create Document

The univer.createUnit(UniverInstanceType.UNIVER_DOC, {}) method creates and returns a DocumentDataModel object.

In the following example, a blank document is created by passing in an empty object.

Get Document Id

const univerAPI = FUniver.newAPI(univer);
const doc = univerAPI.getActiveDocument()
const unitId = doc?.getId()

Get Document Data

const univerAPI = FUniver.newAPI(univer);
const doc = univerAPI.getActiveDocument()
const saveData = doc.getSnapshot();

Or

univer.createUnit(UniverInstanceType.UNIVER_DOC, {})
const saveData = document.save();

Destroying Document

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

univer.dispose();

Text

Operate on text elements in rich text areas

Append Text

Adds the specified text to the end of this text region.

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

Delete Text

Delete the character before the cursor

const univerAPI = FUniver.newAPI(univer)
univerAPI.executeCommand("doc.command.delete-left")

Delete the character after the cursor

const univerAPI = FUniver.newAPI(univer)
univerAPI.executeCommand("doc.command.delete-right")

Styles

Set the font color for the selected text

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

page break

\f is a page break, used to insert page breaks in a document.

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

List

Adds an unordered list to the paragraph where the cursor is located; repeated calls cancel the unordered list.

const univerAPI = FUniver.newAPI(univer)
univerAPI.executeCommand("doc.command.list-operation", {listType: 'BULLET_LIST'})

Adds an ordered list to the paragraph where the cursor is located; repeated calls cancel the ordered list.

const univerAPI = FUniver.newAPI(univer)
univerAPI.executeCommand("doc.command.list-operation", {listType: 'ORDER_LIST'})

Reference

For more documentation and APIs, please refer to:


Was this page helpful?