富文本使用指南
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)用于创建只读的样式对象。
基础概念
富文本由两个核心部分组成:
- TextStyle(文本样式):控制文字的外观,包括字体、字号、颜色等。
- 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 Univer2. 评论富文本
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)你觉得这篇文档如何?