富文字使用指南

Univer 的富文字可用於單元格內容、評論和超連結。本文介紹如何建立富文字物件,以及如何應用這些物件。

Facade API

Facade API 提供一組工廠方法,用於建立構成富文字的各類物件:

TypeScript
// 建立富文字const richText = univerAPI.newRichText()const richTextValue = univerAPI.newRichTextValue({ body: { dataStream: 'Hello Univer\r\n' } })// 建立段落樣式const paragraphStyle = univerAPI.newParagraphStyle()const paragraphStyleValue = univerAPI.newParagraphStyleValue()// 建立文字樣式const textStyle = univerAPI.newTextStyle()const textStyleValue = univerAPI.newTextStyleValue()// 建立文字裝飾const decoration = univerAPI.newTextDecoration()

每個建立方法都分為 Builder 版本和 Value 版本。Builder 版本(如 newRichText)用於建立和修改樣式,Value 版本(如 newRichTextValue)用於建立只讀的樣式物件。

基礎概念

富文字由兩個核心部分組成:

  1. TextStyle(文字樣式):控制文字的外觀,包括字型、字號、顏色等。
  2. ParagraphStyle(段落樣式):控制段落級格式,包括對齊方式、縮排和行距。

文字樣式設定

建立文字樣式

TypeScript
// 建立一個基礎的文字樣式const textStyle = univerAPI.newTextStyle()  .setFontFamily('Arial')  .setFontSize(12)  .setItalic(true)  .setBold(true)  .build()// 建立帶顏色的文字樣式const coloredStyle = univerAPI.newTextStyle()  .setColor({ rgb: '#FF0000' }) // 設定紅色文字  .setBackground({ rgb: '#FFFF00' }) // 設定黃色背景  .build()

文字裝飾

TypeScript
// 新增下劃線const underlineStyle = univerAPI.newTextStyle()  .setUnderline(    univerAPI.newTextDecoration()      .setShow(true)      .setColor({ rgb: '#0000FF' })      .setLineType(univerAPI.Enum.TextDecoration.SINGLE),  )  .build()// 新增刪除線const strikethroughStyle = univerAPI.newTextStyle()  .setStrikethrough(    univerAPI.newTextDecoration()      .setShow(true),  )  .build()

段落樣式設定

建立段落樣式

TypeScript
// 建立基礎段落樣式const paragraphStyle = univerAPI.newParagraphStyle()  .setHorizontalAlign(univerAPI.Enum.HorizontalAlign.CENTER) // 居中對齊  .setLineSpacing(1.5) // 1.5倍行距  .build()// 設定段落縮排const indentedStyle = univerAPI.newParagraphStyle()  .setIndentFirstLine({ value: 2, unit: 'cm' }) // 首行縮排2釐米  .setIndentStart({ value: 1, unit: 'cm' }) // 左側縮排1釐米  .build()

應用場景

1. 單元格富文字

設定單個單元格時使用 setRichTextValueForCell,一次設定一片單元格區域時使用 setRichTextValues。

TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const fRange = fWorksheet.getRange('A1:B2')console.log(fRange.getValue(true))// A1 單元格設定為富文字const richText = univerAPI.newRichText()  .insertText('Hello Univer')  .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 Univer// A1:B2 單元格設定為富文字fRange.setRichTextValues([  [richText, richText],  [null, null],])console.log(fRange.getValue(true).toPlainText()) // Hello Univer

2. 評論富文字

TypeScript
// 新增帶格式的評論const range = univerAPI.getActiveWorkbook()  .getActiveSheet()  .getActiveRange()// 建立評論內容const comment = univerAPI.newTheadComment()  .setContent(    univerAPI.newRichText()      .insertText('你好,請檢查這個單元格的資料'),  )// 非同步新增評論const success = await range.addCommentAsync(comment)if (success) {  console.log('評論新增成功')}

3. 超連結富文字

TypeScript
// 建立帶超連結的富文字const range = univerAPI.getActiveWorkbook()  .getActiveSheet()  .getActiveRange()// 使用 newRichText().insertLink 建立超連結const richText = univerAPI.newRichText()  .insertLink('訪問 Univer', '/')// 設定到單元格range.setRichTextValueForCell(richText)

你覺得這篇文檔如何?

© 2026 DreamNum Co., Ltd.