Rich Text Usage Guide

Rich text in Univer appears in cell content, comments, and hyperlinks. This page describes how to construct rich text objects and how to apply them.

Facade API

The Facade API provides factory methods for the objects that compose rich text:

TypeScript
// Create rich textconst richText = univerAPI.newRichText()const richTextValue = univerAPI.newRichTextValue({ body: { dataStream: 'Hello Univer\r\n' } })// Create paragraph styleconst paragraphStyle = univerAPI.newParagraphStyle()const paragraphStyleValue = univerAPI.newParagraphStyleValue()// Create text styleconst textStyle = univerAPI.newTextStyle()const textStyleValue = univerAPI.newTextStyleValue()// Create text decorationconst decoration = univerAPI.newTextDecoration()

Each creation method exists in two forms: a Builder version and a Value version. A Builder version, such as newRichText, creates and modifies a style. A Value version, such as newRichTextValue, creates a read-only style object.

Basic concepts

Rich text consists of two core parts:

  1. TextStyle controls the appearance of text: font, size, color, and so on.
  2. ParagraphStyle controls formatting at the paragraph level: alignment, indentation, and line spacing.

Text style settings

Creating text styles

TypeScript
// Create a basic text styleconst textStyle = univerAPI.newTextStyle()  .setFontFamily('Arial')  .setFontSize(12)  .setItalic(true)  .setBold(true)  .build()// Create a text style with colorsconst coloredStyle = univerAPI.newTextStyle()  .setColor({ rgb: '#FF0000' }) // Set red text  .setBackground({ rgb: '#FFFF00' }) // Set yellow background  .build()

Text decorations

TypeScript
// Add underlineconst underlineStyle = univerAPI.newTextStyle()  .setUnderline(    univerAPI.newTextDecoration()      .setShow(true)      .setColor({ rgb: '#0000FF' })      .setLineType(univerAPI.Enum.TextDecoration.SINGLE),  )  .build()// Add strikethroughconst strikethroughStyle = univerAPI.newTextStyle()  .setStrikethrough(    univerAPI.newTextDecoration()      .setShow(true),  )  .build()

Paragraph style settings

Creating paragraph styles

TypeScript
// Create a basic paragraph styleconst paragraphStyle = univerAPI.newParagraphStyle()  .setHorizontalAlign(univerAPI.Enum.HorizontalAlign.CENTER) // Center alignment  .setLineSpacing(1.5) // 1.5 line spacing  .build()// Set paragraph indentationconst indentedStyle = univerAPI.newParagraphStyle()  .setIndentFirstLine({ value: 2, unit: 'cm' }) // First line indent 2cm  .setIndentStart({ value: 1, unit: 'cm' }) // Left indent 1cm  .build()

Use cases

1. Cell rich text

For a single cell, call setRichTextValueForCell. To set a block of cells in a single call, call setRichTextValues.

TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const fRange = fWorksheet.getRange('A1:B2')console.log(fRange.getValue(true))// Set A1 cell value to rich textconst richText = univerAPI.newRichText()  .insertText('Hello World')  .setStyle(0, 1, { bl: 1, cl: { rgb: '#c81e1e' } })  .setStyle(6, 7, { bl: 1, cl: { rgb: '#c81e1e' } })fRange.setRichTextValueForCell(richText)console.log(fRange.getValue(true).toPlainText()) // Hello World// Set A1:B2 cell value to rich textfRange.setRichTextValues([  [richText, richText],  [null, null],])console.log(fRange.getValue(true).toPlainText()) // Hello World

2. Comment rich text

TypeScript
// Add formatted commentsconst range = univerAPI.getActiveWorkbook()  .getActiveSheet()  .getActiveRange()// Create comment contentconst comment = univerAPI.newTheadComment()  .setContent(    univerAPI.newRichText()      .insertText('Please check the data in this cell'),  )// Add comment asynchronouslyconst success = await range.addCommentAsync(comment)if (success) {  console.log('Comment added successfully')}
TypeScript
// Create rich text with hyperlinkconst range = univerAPI.getActiveWorkbook()  .getActiveSheet()  .getActiveRange()// Create hyperlink using newRichText().insertLinkconst richText = univerAPI.newRichText()  .insertLink('Visit Univer', '/')// Set to cellrange.setRichTextValueForCell(richText)

How is this guide?

© 2026 DreamNum Co., Ltd.