Word.ParagraphCollection class

包含 Word 集合。段落对象。

扩展

注解

API 集:WordApi 1.1

使用方

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/15-images/insert-and-get-pictures.yaml

// Inserts an image anchored to the last paragraph.
await Word.run(async (context) => {
  context.document.body.paragraphs
    .getLast()
    .insertParagraph("", "After")
    .insertInlinePictureFromBase64(base64Image, "End");

  await context.sync();
});

属性

context

与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。

items

获取此集合中已加载的子项。

方法

add(range)

返回表示 Paragraph 添加到文档中的新空白段落的对象。

closeUp()

清除指定段落前的段落间距。

decreaseSpacing()

以 6 磅增量缩减段前和段后的间距。

getFirst()

获取此集合中的第一个段落。 如果集合为空,则引发 ItemNotFound 错误。

getFirstOrNullObject()

获取此集合中的第一个段落。 如果集合为空,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

getLast()

获取此集合中的最后一个段落。 如果集合为空,则引发 ItemNotFound 错误。

getLastOrNullObject()

获取此集合中的最后一个段落。 如果集合为空,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

increaseSpacing()

以 6 磅增量增加段前和段后的间距。

indent()

将段落缩进一级。

indentCharacterWidth(count)

按指定的字符数缩进集合中的段落。

indentFirstLineCharacterWidth(count)

按指定的字符数缩进集合中段落的第一行。

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

openOrCloseUp()

切换段前间距。

openUp()

为指定段落设置 12 磅的段前间距。

outdent()

删除段落的一个级别缩进。

outlineDemote()

对指定段落应用下一个标题级别样式(从"标题 1"到"标题 8")。

outlineDemoteToBody()

通过应用“普通”样式将指定段落降级为正文文本。

outlinePromote()

将之前的标题级别样式 (标题 1 到标题 8) 应用于集合中的段落。

space1()

将指定的段落设置为单倍行距。

space1Pt5()

将指定的段落设置为 1.5 行距。

space2()

将指定段落设置为双倍行距。

tabHangingIndent(count)

将悬挂缩进设置为指定的制表位数。

tabIndent(count)

将指定段落的左缩进设置为指定的制表位数。

toJSON()

覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Word.ParagraphCollection 对象是 API 对象,但该 toJSON 方法返回一个纯 JavaScript 对象, (类型为 Word.Interfaces.ParagraphCollectionData) ,其中包含一个“items”数组,其中包含从集合的 items 中加载的任何属性的浅层副本。

track()

根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果在调用中 .sync 使用此对象,并且在“.run”批处理的顺序执行之外使用此对象,并且在设置属性或对对象调用方法时收到“InvalidObjectPath”错误,则需要在首次创建对象时将对象添加到跟踪对象集合。 如果此对象是集合的一部分,您还应跟踪父集合。

untrack()

释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 需要在内存释放生效之前调用 context.sync()

属性详细信息

context

与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。

context: RequestContext;

属性值

items

获取此集合中已加载的子项。

readonly items: Word.Paragraph[];

属性值

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/20-lists/insert-list.yaml

// This example starts a new list with the second paragraph.
await Word.run(async (context) => {
  const paragraphs: Word.ParagraphCollection = context.document.body.paragraphs;
  paragraphs.load("$none");

  await context.sync();

  // Start new list using the second paragraph.
  const list: Word.List = paragraphs.items[1].startNewList();
  list.load("$none");

  await context.sync();

  // To add new items to the list, use Start or End on the insertLocation parameter.
  list.insertParagraph("New list item at the start of the list", "Start");
  const paragraph: Word.Paragraph = list.insertParagraph("New list item at the end of the list (set to list level 5)", "End");

  // Set up list level for the list item.
  paragraph.listItem.level = 4;

  // To add paragraphs outside the list, use Before or After.
  list.insertParagraph("New paragraph goes after (not part of the list)", "After");

  await context.sync();
});

方法详细信息

add(range)

返回表示 Paragraph 添加到文档中的新空白段落的对象。

add(range: Word.Range): Word.Paragraph;

参数

range
Word.Range

要在其前添加新段落的区域。 新的段落不替换该区域。

返回

注解

API 集:WordApiDesktop 1.4

closeUp()

清除指定段落前的段落间距。

closeUp(): void;

返回

void

注解

API 集:WordApiDesktop 1.4

decreaseSpacing()

以 6 磅增量缩减段前和段后的间距。

decreaseSpacing(): void;

返回

void

注解

API 集:WordApiDesktop 1.4

getFirst()

获取此集合中的第一个段落。 如果集合为空,则引发 ItemNotFound 错误。

getFirst(): Word.Paragraph;

返回

注解

API 集:WordApi 1.3

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-annotations.yaml

// Gets annotations found in the selected paragraph.
await Word.run(async (context) => {
  const paragraph: Word.Paragraph = context.document.getSelection().paragraphs.getFirst();
  const annotations: Word.AnnotationCollection = paragraph.getAnnotations();
  annotations.load("id,state,critiqueAnnotation");

  await context.sync();

  console.log("Annotations found:");

  for (let i = 0; i < annotations.items.length; i++) {
    const annotation: Word.Annotation = annotations.items[i];

    console.log(`ID ${annotation.id} - state '${annotation.state}':`, annotation.critiqueAnnotation.critique);
  }
});

getFirstOrNullObject()

获取此集合中的第一个段落。 如果集合为空,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

getFirstOrNullObject(): Word.Paragraph;

返回

注解

API 集:WordApi 1.3

getLast()

获取此集合中的最后一个段落。 如果集合为空,则引发 ItemNotFound 错误。

getLast(): Word.Paragraph;

返回

注解

API 集:WordApi 1.3

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/15-images/insert-and-get-pictures.yaml

// Inserts an image anchored to the last paragraph.
await Word.run(async (context) => {
  context.document.body.paragraphs
    .getLast()
    .insertParagraph("", "After")
    .insertInlinePictureFromBase64(base64Image, "End");

  await context.sync();
});

getLastOrNullObject()

获取此集合中的最后一个段落。 如果集合为空,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

getLastOrNullObject(): Word.Paragraph;

返回

注解

API 集:WordApi 1.3

increaseSpacing()

以 6 磅增量增加段前和段后的间距。

increaseSpacing(): void;

返回

void

注解

API 集:WordApiDesktop 1.4

indent()

将段落缩进一级。

indent(): void;

返回

void

注解

API 集:WordApiDesktop 1.4

indentCharacterWidth(count)

按指定的字符数缩进集合中的段落。

indentCharacterWidth(count: number): void;

参数

count

number

指定段落要缩进的字符数。

返回

void

注解

API 集:WordApiDesktop 1.4

indentFirstLineCharacterWidth(count)

按指定的字符数缩进集合中段落的第一行。

indentFirstLineCharacterWidth(count: number): void;

参数

count

number

每个指定段落的首行要缩进的字符数。

返回

void

注解

API 集:WordApiDesktop 1.4

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(options?: Word.Interfaces.ParagraphCollectionLoadOptions & Word.Interfaces.CollectionLoadOptions): Word.ParagraphCollection;

参数

options

Word.Interfaces.ParagraphCollectionLoadOptions & Word.Interfaces.CollectionLoadOptions

为要加载的对象属性提供选项。

返回

示例

// This example shows how to get the paragraphs in the Word document
// along with their text and font size properties.
// 
// Run a batch operation against the Word object model.
await Word.run(async (context) => {

    // Create a proxy object for the paragraphs collection.
    const paragraphs = context.document.body.paragraphs;

    // Queue a command to load the text and font properties.
    // It is best practice to always specify the property set. Otherwise, all properties are
    // returned in on the object.
    paragraphs.load('text, font/size');

    // Synchronize the document state by executing the queued commands,
    // and return a promise to indicate task completion.
    await context.sync();

    // Insert code that works with the paragraphs loaded by paragraphs.load().
});

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNames?: string | string[]): Word.ParagraphCollection;

参数

propertyNames

string | string[]

指定要加载的属性的逗号分隔的字符串或字符串数组。

返回

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNamesAndPaths?: OfficeExtension.LoadOption): Word.ParagraphCollection;

参数

propertyNamesAndPaths
OfficeExtension.LoadOption

propertyNamesAndPaths.select 是指定要加载的属性的逗号分隔字符串,以及 propertyNamesAndPaths.expand 指定要加载的导航属性的逗号分隔字符串。

返回

openOrCloseUp()

切换段前间距。

openOrCloseUp(): void;

返回

void

注解

API 集:WordApiDesktop 1.4

openUp()

为指定段落设置 12 磅的段前间距。

openUp(): void;

返回

void

注解

API 集:WordApiDesktop 1.4

outdent()

删除段落的一个级别缩进。

outdent(): void;

返回

void

注解

API 集:WordApiDesktop 1.4

outlineDemote()

对指定段落应用下一个标题级别样式(从"标题 1"到"标题 8")。

outlineDemote(): void;

返回

void

注解

API 集:WordApiDesktop 1.4

outlineDemoteToBody()

通过应用“普通”样式将指定段落降级为正文文本。

outlineDemoteToBody(): void;

返回

void

注解

API 集:WordApiDesktop 1.4

outlinePromote()

将之前的标题级别样式 (标题 1 到标题 8) 应用于集合中的段落。

outlinePromote(): void;

返回

void

注解

API 集:WordApiDesktop 1.4

space1()

将指定的段落设置为单倍行距。

space1(): void;

返回

void

注解

API 集:WordApiDesktop 1.4

space1Pt5()

将指定的段落设置为 1.5 行距。

space1Pt5(): void;

返回

void

注解

API 集:WordApiDesktop 1.4

space2()

将指定段落设置为双倍行距。

space2(): void;

返回

void

注解

API 集:WordApiDesktop 1.4

tabHangingIndent(count)

将悬挂缩进设置为指定的制表位数。

tabHangingIndent(count: number): void;

参数

count

number

如果为正,则表示要缩进的制表位数;如果为负,则表示要删除的缩进制表位数。

返回

void

注解

API 集:WordApiDesktop 1.4

tabIndent(count)

将指定段落的左缩进设置为指定的制表位数。

tabIndent(count: number): void;

参数

count

number

如果为正,则表示要缩进的制表位数;如果为负,则表示要删除的缩进制表位数。

返回

void

注解

API 集:WordApiDesktop 1.4

toJSON()

覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Word.ParagraphCollection 对象是 API 对象,但该 toJSON 方法返回一个纯 JavaScript 对象, (类型为 Word.Interfaces.ParagraphCollectionData) ,其中包含一个“items”数组,其中包含从集合的 items 中加载的任何属性的浅层副本。

toJSON(): Word.Interfaces.ParagraphCollectionData;

返回

track()

根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果在调用中 .sync 使用此对象,并且在“.run”批处理的顺序执行之外使用此对象,并且在设置属性或对对象调用方法时收到“InvalidObjectPath”错误,则需要在首次创建对象时将对象添加到跟踪对象集合。 如果此对象是集合的一部分,您还应跟踪父集合。

track(): Word.ParagraphCollection;

返回

untrack()

释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 需要在内存释放生效之前调用 context.sync()

untrack(): Word.ParagraphCollection;

返回