Word.ParagraphCollection class

包含Word的集合。Paragraph 对象。

扩展

注解

[ 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”数组,其中包含集合项中任何已加载属性的浅表副本。

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”数组,其中包含集合项中任何已加载属性的浅表副本。

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;

返回