Text Runs and Paragraphs

Index-based model

Univer Docs does not store rich text as a nested tree. Plain text is stored in body.dataStream1, and styles and structure are attached to the text through index ranges.

The most common structures are:

  • ITextRun: inline text style range.
  • IParagraph: paragraph metadata and paragraph style.
  • ISectionBreak: section break metadata.

ITextRun

ITextRun describes the style of one continuous text range.

PropertyTypeDescription
stnumberStart index, inclusive.
ednumberEnd index, exclusive.
sId?stringReferenced style ID.
ts?ITextStyleInline text style object.
TypeScript
const body: IDocumentBody = {  dataStream: 'Hello Univer\r\n',  textRuns: [    {      st: 0,      ed: 5,      ts: { bl: 1 },    },    {      st: 6,      ed: 12,      ts: { cl: { rgb: '#2563eb' } },    },  ],}

In this example, Hello is bold and Univer is blue. The paragraph break \r\n is not included in the styled ranges.

IParagraph

IParagraph holds paragraph-level metadata. startIndex points at the paragraph break in dataStream.

PropertyTypeDescription
startIndexnumberIndex of the paragraph break.
paragraphIdstringStable paragraph ID.
styleId?stringNamed style the paragraph references.
paragraphStyle?IParagraphStyleParagraph style such as alignment, indentation, line spacing, and heading type.
bullet?Partial<IBullet> | nullNumbering override. Omit it to inherit numbering from the named style, or set null to turn numbering off.
TypeScript
const body: IDocumentBody = {  dataStream: 'Hello Univer\r\nSecond line\r\n',  paragraphs: [    {      startIndex: 12,      paragraphId: 'p-1',      paragraphStyle: {        namedStyleType: NamedStyleType.HEADING_1,      },    },    {      startIndex: 25,      paragraphId: 'p-2',    },  ],}

List numbering

When bullet is omitted, a paragraph keeps the numbering defined by its named style. Set bullet to null to remove numbering from that paragraph, or set individual fields such as nestingLevel to override part of the inherited bullet.

Read the effective list with resolveDocumentParagraphBullet(document, paragraph) from @univerjs/core. It returns undefined for an unnumbered paragraph and does not write inherited values back into the snapshot.

TypeScript
import { resolveDocumentParagraphBullet } from '@univerjs/core'const bullet = resolveDocumentParagraphBullet(document, paragraph)if (bullet) {  console.log(bullet.listType, bullet.nestingLevel)}

Word wrapping within a paragraph

paragraphStyle.wordWrap is controlled by the paragraph setting Allow Latin text to wrap in the middle of a word. BooleanNumber.TRUE allows a word to split across lines, and BooleanNumber.FALSE keeps each word whole. When the value is missing, it inherits from the referenced paragraph style first, then from documentStyle.defaultParagraphStyle; if neither sets it, words are not split. resolveDocumentParagraphWordWrap(document, paragraph) from @univerjs/core returns the effective value and does not write inherited defaults into the snapshot. Sheets cell wrapping is unaffected. DOCX w:wordWrap stores the inverse, so do not copy its XML value directly.

ISectionBreak

ISectionBreak also uses an index to attach section-level settings to the text stream. It can override page size, margins, headers, footers, columns, and text direction for a section.

TypeScript
const body: IDocumentBody = {  dataStream: 'First section\nSecond section\r\n',  sectionBreaks: [    {      startIndex: 13,      pageSize: { width: 595, height: 842 },      marginTop: 72,      marginBottom: 72,    },  ],}

Notes

  • Keep indexes aligned with dataStream. Wrong indexes can leave style ranges, paragraphs, selections, or rendering out of sync.
  • Prefer Facade APIs and commands when editing an active document.
  • When you build snapshots by hand, build dataStream first, then compute ranges and paragraph positions from that string.

Footnotes

  1. Text offsets use JavaScript UTF-16 code units, not visible character counts. For example, "😀".length is 2. A text run covers [st, ed): st is included and ed is excluded; do not reuse the inclusive end indices of sheet ranges. ↩

How is this guide?

© 2026 DreamNum Co., Ltd.