Univer Docs API
Univer Docs provides professional document typesetting capabilities, with concepts closely aligned with Microsoft Word.
Importing
import '@univerjs/docs/facade'import '@univerjs/docs-ui/facade'Operate Documents
For document units, 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
Use univerAPI.createDocument(data) to create a new document and get its FDocument facade wrapper.
The parameter is an optional document data object that contains the initial data for the document. If {} is passed, an empty document is created.
When working with the underlying univer instance directly instead of the Facade API, use univer.createUnit(UniverInstanceType.UNIVER_DOC, data).
const doc = univerAPI.createDocument(data)Get Document unitId
const doc = univerAPI.getActiveDocument()const unitId = doc?.getId()Get Document Data
const doc = univerAPI.getActiveDocument()const saveData = doc.save()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?.insertText(0, 'Univer')Delete Text
Delete text by document offsets.
const doc = univerAPI.getActiveDocument()doc?.deleteRange({ startOffset: 0, endOffset: 1 })Modify Style
Set text style on a paragraph.
const doc = univerAPI.getActiveDocument()const paragraph = doc?.getParagraphs()[0]paragraph?.setStyle({ textStyle: { cl: { rgb: '#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?.insertText(0, '\f')Document statistics
Use the underlying DocumentDataModel to calculate statistics for the whole document or selected ranges.
const doc = univerAPI.getActiveDocument()if (!doc) throw new Error('No active document')const statistics = await doc.getDocumentDataModel().getStatistics({ locale: univerAPI.Enum.LocaleType.EN_US,})console.log(statistics.words, statistics.charactersWithSpaces, statistics.paragraphs)getStatistics() returns word, character, and paragraph counts. Pass ranges to count selected text or an AbortSignal in signal to cancel obsolete calculations.
How is this guide?