リッチテキスト利用ガイド
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()ビルダーバージョンとバリューバージョンの2つのフォームにそれぞれ作成方法があります。newRichTextなどのビルダーバージョンは、スタイルを作成および変更します。newRichTextValueのような値バージョンは、読み取り専用のスタイルオブジェクトを作成します。
基本概念
リッチテキストは2つのコアパーツで構成されています。
- テキストスタイルはテキストの出現を制御します: 壷、サイズ、色、等。
- パラグラフスタイルは、段落レベルでのフォーマットを制御する:アライメント、インデント、およびライン間隔。
テキストスタイル設定
テキストスタイルの作成
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' }) // 一行目インデント 2cm .setIndentStart({ value: 1, unit: 'cm' }) // 左インデント 1cm .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 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// A1:B2 のセル値をリッチテキストに設定fRange.setRichTextValues([ [richText, richText], [null, null],])console.log(fRange.getValue(true).toPlainText()) // Hello World2. コメントのリッチテキスト
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('Visit Univer', '/')// セルへ設定range.setRichTextValueForCell(richText)このガイドはいかがですか?