리치 텍스트 사용 가이드
Univer의 풍부한 텍스트는 세포 내용, 의견 및 하이퍼 링크에서 나타납니다. 이 페이지는 풍부한 텍스트 개체를 구성하는 방법을 설명하고 그들을 적용하는 방법을 설명합니다.
Facade API
Facade API는 풍부한 텍스트를 구성하는 개체에 대한 공장 방법을 제공합니다:
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()각 생성 방법은 두 가지 형태로 존재합니다. Builder 버전과 Value 버전. newRichText와 같은 Builder 버전은 스타일을 생성하고 수정합니다. newRichTextValue과 같은 Value 버전은 read-only 스타일 객체를 만듭니다.
기본 개념
부유한 원본은 2개의 핵심 부속으로 이루어져 있습니다:
- 본문내용는 원본의 외관을 통제합니다: 글꼴, 크기, 색깔, 등.
- 관련 상품는 단락 수준에서 형식을 제어합니다. 정렬, 들여쓰기 및 선 간격.
텍스트 스타일 설정
텍스트 스타일 만들기
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()텍스트 장식
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()단락 스타일 설정
단락 스타일 만들기
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()사용 사례
1. 셀 리치 텍스트
단일 셀의 경우 setRichTextValueForCell를 호출합니다. 단일 통화에서 세포의 블록을 설정하려면 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 World2. 댓글 리치 텍스트
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')}3. 하이퍼링크 리치 텍스트
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)이 가이드는 어떠셨나요?