Word.DocumentCreated class

对象 DocumentCreated 是由 创建的顶级对象 Application.createDocumentDocumentCreated对象是特殊Document对象。

扩展

注解

API 集:WordApi 1.3

使用方

示例

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

// Updates the text of the current document with the text from another document passed in as a Base64-encoded string.
await Word.run(async (context) => {
  // Use the Base64-encoded string representation of the selected .docx file.
  const externalDoc: Word.DocumentCreated = context.application.createDocument(externalDocument);
  await context.sync();

  if (!Office.context.requirements.isSetSupported("WordApiHiddenDocument", "1.3")) {
    console.warn("The WordApiHiddenDocument 1.3 requirement set isn't supported on this client so can't proceed. Try this action on a platform that supports this requirement set.");
    return;
  }

  const externalDocBody: Word.Body = externalDoc.body;
  externalDocBody.load("text");
  await context.sync();

  // Insert the external document's text at the beginning of the current document's body.
  const externalDocBodyText = externalDocBody.text;
  const currentDocBody: Word.Body = context.document.body;
  currentDocBody.insertText(externalDocBodyText, Word.InsertLocation.start);
  await context.sync();
});

属性

body

获取 Body 文档的对象。 正文是不包括页眉、页脚、脚注、文本框等的文本。

contentControls

获取文档中的对象集合 ContentControl 。 这包括文档正文、页眉、页脚、文本框等内容控件。

context

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

customXmlParts

获取文档中的自定义 XML 部分。

properties

获取文档的属性。

saved

指示是否已保存在文档中所做的更改。 值 true 指示文档自保存后未更改。

sections

获取文档中的对象集合 Section

settings

获取文档中加载项的设置。

方法

addStyle(name, type)

按名称和类型将样式添加到文档中。

addStyle(name, type)

按名称和类型将样式添加到文档中。

deleteBookmark(name)

从文档中删除书签(如果存在)。

getBookmarkRange(name)

获取书签的范围。 如果书签不存在,则引发 ItemNotFound 错误。

getBookmarkRangeOrNullObject(name)

获取书签的范围。 如果书签不存在,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

getContentControls(options)

获取文档中当前支持的内容控件。

getStyles()

获取表示 StyleCollection 文档的整个样式集的对象。

insertFileFromBase64(base64File, insertLocation, insertFileOptions)

在目标文档的特定位置插入具有其他属性的文档。 默认情况下,将复制页眉、页脚、水印和其他部分属性。

load(options)

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

load(propertyNames)

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

load(propertyNamesAndPaths)

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

open()

打开文档。

save(saveBehavior, fileName)

保存文档。

save(saveBehavior, fileName)

保存文档。

set(properties, options)

同时设置对象的多个属性。 可以传递具有相应属性的纯文本对象,也可以传递另一个相同类型的 API 对象。

set(properties)

基于现有加载的对象,同时在对象上设置多个属性。

toJSON()

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

track()

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

untrack()

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

属性详细信息

body

获取 Body 文档的对象。 正文是不包括页眉、页脚、脚注、文本框等的文本。

readonly body: Word.Body;

属性值

注解

API 集:WordApiHiddenDocument 1.3

contentControls

获取文档中的对象集合 ContentControl 。 这包括文档正文、页眉、页脚、文本框等内容控件。

readonly contentControls: Word.ContentControlCollection;

属性值

注解

API 集:WordApiHiddenDocument 1.3

context

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

context: RequestContext;

属性值

customXmlParts

获取文档中的自定义 XML 部分。

readonly customXmlParts: Word.CustomXmlPartCollection;

属性值

注解

API 集:WordApiHiddenDocument 1.4

properties

获取文档的属性。

readonly properties: Word.DocumentProperties;

属性值

注解

API 集:WordApiHiddenDocument 1.3

saved

指示是否已保存在文档中所做的更改。 值 true 指示文档自保存后未更改。

readonly saved: boolean;

属性值

boolean

注解

API 集:WordApiHiddenDocument 1.3

sections

获取文档中的对象集合 Section

readonly sections: Word.SectionCollection;

属性值

注解

API 集:WordApiHiddenDocument 1.3

settings

获取文档中加载项的设置。

readonly settings: Word.SettingCollection;

属性值

注解

API 集:WordApiHiddenDocument 1.4

方法详细信息

addStyle(name, type)

按名称和类型将样式添加到文档中。

addStyle(name: string, type: Word.StyleType): Word.Style;

参数

name

string

表示样式名称的字符串。

type
Word.StyleType

样式类型,包括字符、列表、段落或表格。

返回

注解

API 集:WordApiHiddenDocument 1.5

addStyle(name, type)

按名称和类型将样式添加到文档中。

addStyle(name: string, type: "Character" | "List" | "Paragraph" | "Table"): Word.Style;

参数

name

string

表示样式名称的字符串。

type

"Character" | "List" | "Paragraph" | "Table"

样式类型,包括字符、列表、段落或表格。

返回

注解

API 集:WordApiHiddenDocument 1.5

deleteBookmark(name)

从文档中删除书签(如果存在)。

deleteBookmark(name: string): void;

参数

name

string

不区分大小写的书签名称。

返回

void

注解

API 集:WordApiHiddenDocument 1.4

getBookmarkRange(name)

获取书签的范围。 如果书签不存在,则引发 ItemNotFound 错误。

getBookmarkRange(name: string): Word.Range;

参数

name

string

不区分大小写的书签名称。

返回

注解

API 集:WordApiHiddenDocument 1.4

getBookmarkRangeOrNullObject(name)

获取书签的范围。 如果书签不存在,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

getBookmarkRangeOrNullObject(name: string): Word.Range;

参数

name

string

不区分大小写的书签名称。 仅支持字母数字和下划线字符。 它必须以字母开头,但如果要将书签标记为隐藏,请以下划线字符开头名称。 名称不能超过 40 个字符。

返回

注解

API 集:WordApiHiddenDocument 1.4

getContentControls(options)

获取文档中当前支持的内容控件。

getContentControls(options?: Word.ContentControlOptions): Word.ContentControlCollection;

参数

options
Word.ContentControlOptions

可选。 用于定义返回哪些内容控件的选项。

返回

注解

API 集:WordApiHiddenDocument 1.5

重要提示: 如果在 options 参数中提供了特定类型,则仅返回受支持类型的内容控件。 请注意,使用通用 Word 的方法将引发异常与特定类型无关的 ContentControl。 随着时间的推移,可能会支持其他类型的内容控件。 因此,加载项应请求和处理特定类型的内容控件。

getStyles()

获取表示 StyleCollection 文档的整个样式集的对象。

getStyles(): Word.StyleCollection;

返回

注解

API 集:WordApiHiddenDocument 1.5

insertFileFromBase64(base64File, insertLocation, insertFileOptions)

在目标文档的特定位置插入具有其他属性的文档。 默认情况下,将复制页眉、页脚、水印和其他部分属性。

insertFileFromBase64(base64File: string, insertLocation: Word.InsertLocation.replace | Word.InsertLocation.start | Word.InsertLocation.end | "Replace" | "Start" | "End", insertFileOptions?: Word.InsertFileOptions): Word.SectionCollection;

参数

base64File

string

.docx 文件的 Base64 编码内容。

insertLocation

replace | start | end | "Replace" | "Start" | "End"

值必须为 replacestart、或 end

insertFileOptions
Word.InsertFileOptions

可选。 应导入到目标文档的其他属性。

返回

注解

API 集:WordApiHiddenDocument 1.5

如果要插入的文档包含 ActiveX 控件,则不支持插入, (可能在表单字段) 中。 请考虑将此类窗体字段替换为适合你的场景的内容控件或其他选项。

load(options)

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

load(options?: Word.Interfaces.DocumentCreatedLoadOptions): Word.DocumentCreated;

参数

options
Word.Interfaces.DocumentCreatedLoadOptions

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

返回

load(propertyNames)

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

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

参数

propertyNames

string | string[]

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

返回

load(propertyNamesAndPaths)

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

load(propertyNamesAndPaths?: {
            select?: string;
            expand?: string;
        }): Word.DocumentCreated;

参数

propertyNamesAndPaths

{ select?: string; expand?: string; }

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

返回

open()

打开文档。

open(): void;

返回

void

注解

API 集:WordApi 1.3

示例

// Create and open a new document in a new tab or window.
await Word.run(async (context) => {
    const externalDoc = context.application.createDocument();
    await context.sync();

    externalDoc.open();
    await context.sync();
});

save(saveBehavior, fileName)

保存文档。

save(saveBehavior?: Word.SaveBehavior, fileName?: string): void;

参数

saveBehavior
Word.SaveBehavior

可选。 DocumentCreated 仅支持 save.

fileName

string

可选。 文件名 (不包括文件扩展名) 。 仅对新文档生效。

返回

void

注解

API 集:WordApiHiddenDocument 1.3

注意: 和fileName参数是在 saveBehavior WordApiHiddenDocument 1.5 中引入的。

save(saveBehavior, fileName)

保存文档。

save(saveBehavior?: "Save" | "Prompt", fileName?: string): void;

参数

saveBehavior

"Save" | "Prompt"

可选。 DocumentCreated 仅支持 save.

fileName

string

可选。 文件名 (不包括文件扩展名) 。 仅对新文档生效。

返回

void

注解

API 集:WordApiHiddenDocument 1.3

注意: 和fileName参数是在 saveBehavior WordApiHiddenDocument 1.5 中引入的。

set(properties, options)

同时设置对象的多个属性。 可以传递具有相应属性的纯文本对象,也可以传递另一个相同类型的 API 对象。

set(properties: Interfaces.DocumentCreatedUpdateData, options?: OfficeExtension.UpdateOptions): void;

参数

properties
Word.Interfaces.DocumentCreatedUpdateData

一个 JavaScript 对象,其属性与调用该方法的对象的属性同构结构。

options
OfficeExtension.UpdateOptions

提供一个选项,用于在 properties 对象尝试设置任何只读属性时禁止错误。

返回

void

set(properties)

基于现有加载的对象,同时在对象上设置多个属性。

set(properties: Word.DocumentCreated): void;

参数

返回

void

toJSON()

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

toJSON(): Word.Interfaces.DocumentCreatedData;

返回

track()

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

track(): Word.DocumentCreated;

返回

untrack()

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

untrack(): Word.DocumentCreated;

返回