Paragraph
Paragraph
段落对象
属性
index
JSSDK: 1.9.1、zOffice V8.0 FP1 支持
获取段落在文档中的序号
语法
paragraph.index
paragraph: 段落对象
返回值
Promise<number>
示例
async function example1() {
const paragraph = await Application.ActiveDocument.Paragraphs.item(5);
const index = await paragraph.index;
}
// 获取第一个大纲条目对应的文档段落的序号
async function example2() {
const outlineEntry = await Application.ActiveDocument.OutlineEnties.item(1);
const index = await outlineEntry.paragraph.index;
}
range
JSSDK: 1.2.2、zOffice2022.3 FP2 支持
获取段落的Range
语法
paragraph.range
paragraph: 段落对象
返回值
Promise<Range>
示例
async function example() {
const paragraph = await Application.ActiveDocument.Paragraphs.item(1);
const range = await paragraph.range;
}
outlineLevel
JSSDK: 1.4.0、zOffice V6.1 支持
返回指定段落的大纲级别。
语法
Paragraph.outlineLevel
Paragraph: 段落对象
返回值
Promise<Word.WdOutlineLevel>
示例
async function example() {
const paragraph = await Application.ActiveDocument.Paragraphs.item(1);
const outlineLevel = await paragraph.outlineLevel;
}
style
JSSDK: 1.4.0、zOffice V6.1 支持
返回指定对象的样式。
语法
Paragraph.style
Paragraph: 段落对象
返回值
Word.Style
示例
async function example() {
const paragraph = await Application.ActiveDocument.Paragraphs.item(1);
const style = paragraph.style;
}
方法
appendText
加文本追加到当前段落
语法
paragraph.appendText(content)
paragraph: 段落对象
参数
| 属性 | 数据类型 | 必填 | 说明 |
|---|---|---|---|
| content | string | 是 | 追加的纯文字文本内容 |
返回值
Promise<boolean>
示例
async function example(content) {
const paragraph = await Application.ActiveDocument.Paragraphs.item(1);
const res = await paragraph.appendText(content);
}
clear
清除段落内容
语法
paragraph.clear()
paragraph: 段落对象
返回值
Promise<boolean>
示例
async function example() {
const paragraph = await Application.ActiveDocument.Paragraphs.item(1);
const res = await paragraph.clear();
}
getRange
获取段落指定偏移量和长度的Range
语法
paragraph.getRange(startOffset, length)
paragraph: 段落对象
参数
| 属性 | 数据类型 | 必填 | 说明 |
|---|---|---|---|
| startOffset | number | 否 | 偏移量(默认为0,两个参数都不传时返回整个段落的range) |
| length | number | 否 | 要获取Range的长度(不传或者length超过startOffset后段落长度时,返回startOffset到段落末尾的range) |
返回值
Promise<Range>
示例
async function example() {
const paragraph = await Application.ActiveDocument.Paragraphs.item(1);
const res = await paragraph.getRange(1, 10);
}
getTextContent
获取段落指定偏移量和长度的内容,不传参数时默认获取段落整段内容
语法
paragraph.getTextContent(startOffset, length, options)
paragraph: 段落对象
参数
| 属性 | 数据类型 | 必填 | 说明 |
|---|---|---|---|
| startOffset | number | 否 | 偏移量(从0开始,默认为0) |
| length | number | 否 | 要获取内容的长度(不传或传0时,获取 startOffset 之后的所有内容) |
| options | object | 否 | 额外参数 |
options结构
| 属性 | 数据类型 | 必填 | 说明 |
|---|---|---|---|
| listSymbol | boolean | 否 | 是否返回编号,默认false |
返回值
Promise<string>
示例
// 获取到的段落的完整内容,不包含编号
async function example1() {
const paragraph = await Application.ActiveDocument.Paragraphs.item(1);
const text = await paragraph.getTextContent();
}
// 获取到的段落的完整内容,包含编号
async function example2() {
const paragraph = await Application.ActiveDocument.Paragraphs.item(1);
const text = await paragraph.getTextContent(undefined, undefined, { listSymbol: true });
}
setText
将段落内容设置为指定文字
语法
paragraph.setText(content)
paragraph: 段落对象
返回值
Promise<boolean>
参数
| 属性 | 数据类型 | 必填 | 说明 |
|---|---|---|---|
| content | string | 是 | 添加的纯文本字符串 |
示例
async function example(content) {
const paragraph = await Application.ActiveDocument.Paragraphs.item(1);
const success = await paragraph.setText(content);
}