Word.CustomXmlPartCollection class
包含 Word 集合。CustomXmlPart 对象。
方法
| add(xml) | 向文档添加新的自定义 XML 部件。 |
| get |
获取其命名空间匹配给定命名空间的自定义 XML 部件的新作用域内集合。 |
| get |
获取集合中项的数目。 |
| get |
获取基于其 ID 的自定义 XML 部件。 |
| get |
获取基于其 ID 的自定义 XML 部件。 如果 CustomXmlPart 不存在,则此方法将返回其 |
| load(options) | 将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
| toJSON() | 覆盖 JavaScript |
| track() | 根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果在调用中 |
| untrack() | 释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 需要在内存释放生效之前调用 |
属性详细信息
context
items
方法详细信息
add(xml)
向文档添加新的自定义 XML 部件。
add(xml: string): Word.CustomXmlPart;
参数
- xml
-
string
XML 内容。 必须是有效的 XML 片段。
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-custom-xml-part-ns.yaml
// Adds a custom XML part.
// If you want to populate the CustomXml.namespaceUri property, you must include the xmlns attribute.
await Word.run(async (context) => {
const originalXml =
"<Reviewers xmlns='http://schemas.contoso.com/review/1.0'><Reviewer>Juan</Reviewer><Reviewer>Hong</Reviewer><Reviewer>Sally</Reviewer></Reviewers>";
const customXmlPart = context.document.customXmlParts.add(originalXml);
customXmlPart.load(["id", "namespaceUri"]);
const xmlBlob = customXmlPart.getXml();
await context.sync();
const readableXml = addLineBreaksToXML(xmlBlob.value);
console.log(`Added custom XML part with namespace URI ${customXmlPart.namespaceUri}:`, readableXml);
// Store the XML part's ID in a setting so the ID is available to other functions.
const settings: Word.SettingCollection = context.document.settings;
settings.add("ContosoReviewXmlPartIdNS", customXmlPart.id);
await context.sync();
});
...
// Adds a custom XML part.
await Word.run(async (context) => {
const originalXml =
"<Reviewers><Reviewer>Juan</Reviewer><Reviewer>Hong</Reviewer><Reviewer>Sally</Reviewer></Reviewers>";
const customXmlPart: Word.CustomXmlPart = context.document.customXmlParts.add(originalXml);
customXmlPart.load("id");
const xmlBlob = customXmlPart.getXml();
await context.sync();
const readableXml = addLineBreaksToXML(xmlBlob.value);
console.log("Added custom XML part:", readableXml);
// Store the XML part's ID in a setting so the ID is available to other functions.
const settings: Word.SettingCollection = context.document.settings;
settings.add("ContosoReviewXmlPartId", customXmlPart.id);
await context.sync();
});
getByNamespace(namespaceUri)
获取其命名空间匹配给定命名空间的自定义 XML 部件的新作用域内集合。
getByNamespace(namespaceUri: string): Word.CustomXmlPartScopedCollection;
参数
- namespaceUri
-
string
命名空间 URI。
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-custom-xml-part-ns.yaml
// Original XML: <Reviewers xmlns='http://schemas.contoso.com/review/1.0'><Reviewer>Juan</Reviewer><Reviewer>Hong</Reviewer><Reviewer>Sally</Reviewer></Reviewers>
// Gets the custom XML parts with the specified namespace URI.
await Word.run(async (context) => {
const namespaceUri = "http://schemas.contoso.com/review/1.0";
console.log(`Specified namespace URI: ${namespaceUri}`);
const scopedCustomXmlParts: Word.CustomXmlPartScopedCollection =
context.document.customXmlParts.getByNamespace(namespaceUri);
scopedCustomXmlParts.load("items");
await context.sync();
console.log(`Number of custom XML parts found with this namespace: ${!scopedCustomXmlParts.items ? 0 : scopedCustomXmlParts.items.length}`);
});
getCount()
获取集合中项的数目。
getCount(): OfficeExtension.ClientResult<number>;
返回
OfficeExtension.ClientResult<number>
注解
getItem(id)
获取基于其 ID 的自定义 XML 部件。
getItem(id: string): Word.CustomXmlPart;
参数
- id
-
string
要检索的自定义 XML 部分的 ID 或索引。
返回
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/50-document/manage-custom-xml-part-ns.yaml
// Original XML: <Reviewers xmlns='http://schemas.contoso.com/review/1.0'><Reviewer>Juan</Reviewer><Reviewer>Hong</Reviewer><Reviewer>Sally</Reviewer></Reviewers>
// Queries a custom XML part for elements matching the search terms.
await Word.run(async (context) => {
const settings: Word.SettingCollection = context.document.settings;
const xmlPartIDSetting: Word.Setting = settings.getItemOrNullObject("ContosoReviewXmlPartIdNS").load("value");
await context.sync();
if (xmlPartIDSetting.value) {
const customXmlPart: Word.CustomXmlPart = context.document.customXmlParts.getItem(xmlPartIDSetting.value);
const xpathToQueryFor = "/contoso:Reviewers";
const clientResult = customXmlPart.query(xpathToQueryFor, {
contoso: "http://schemas.contoso.com/review/1.0"
});
await context.sync();
console.log(`Queried custom XML part for ${xpathToQueryFor} and found ${clientResult.value.length} matches:`);
for (let i = 0; i < clientResult.value.length; i++) {
console.log(clientResult.value[i]);
}
} else {
console.warn("Didn't find custom XML part to query.");
}
});
...
// Original XML: <Reviewers><Reviewer>Juan</Reviewer><Reviewer>Hong</Reviewer><Reviewer>Sally</Reviewer></Reviewers>
// Queries a custom XML part for elements matching the search terms.
await Word.run(async (context) => {
const settings: Word.SettingCollection = context.document.settings;
const xmlPartIDSetting: Word.Setting = settings.getItemOrNullObject("ContosoReviewXmlPartId").load("value");
await context.sync();
if (xmlPartIDSetting.value) {
const customXmlPart: Word.CustomXmlPart = context.document.customXmlParts.getItem(xmlPartIDSetting.value);
const xpathToQueryFor = "/Reviewers/Reviewer";
const clientResult = customXmlPart.query(xpathToQueryFor, {
contoso: "http://schemas.contoso.com/review/1.0"
});
await context.sync();
console.log(`Queried custom XML part for ${xpathToQueryFor} and found ${clientResult.value.length} matches:`);
for (let i = 0; i < clientResult.value.length; i++) {
console.log(clientResult.value[i]);
}
} else {
console.warn("Didn't find custom XML part to query.");
}
});
getItemOrNullObject(id)
获取基于其 ID 的自定义 XML 部件。 如果 CustomXmlPart 不存在,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性。
getItemOrNullObject(id: string): Word.CustomXmlPart;
参数
- id
-
string
要检索的对象的 ID。
返回
注解
load(options)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(options?: Word.Interfaces.CustomXmlPartCollectionLoadOptions & Word.Interfaces.CollectionLoadOptions): Word.CustomXmlPartCollection;
参数
为要加载的对象属性提供选项。
返回
load(propertyNames)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNames?: string | string[]): Word.CustomXmlPartCollection;
参数
- propertyNames
-
string | string[]
指定要加载的属性的逗号分隔的字符串或字符串数组。
返回
load(propertyNamesAndPaths)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。
load(propertyNamesAndPaths?: OfficeExtension.LoadOption): Word.CustomXmlPartCollection;
参数
- propertyNamesAndPaths
- OfficeExtension.LoadOption
propertyNamesAndPaths.select 是指定要加载的属性的逗号分隔字符串,以及 propertyNamesAndPaths.expand 指定要加载的导航属性的逗号分隔字符串。
返回
toJSON()
覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Word.CustomXmlPartCollection 对象是 API 对象,但该 toJSON 方法返回一个纯 JavaScript 对象, (类型为 Word.Interfaces.CustomXmlPartCollectionData) ,其中包含一个“items”数组,其中包含从集合的 items 中加载的任何属性的浅层副本。
toJSON(): Word.Interfaces.CustomXmlPartCollectionData;
返回
track()
根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果在调用中 .sync 使用此对象,并且在“.run”批处理的顺序执行之外使用此对象,并且在设置属性或对对象调用方法时收到“InvalidObjectPath”错误,则需要在首次创建对象时将对象添加到跟踪对象集合。 如果此对象是集合的一部分,您还应跟踪父集合。
track(): Word.CustomXmlPartCollection;
返回
untrack()
释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 需要在内存释放生效之前调用 context.sync() 。
untrack(): Word.CustomXmlPartCollection;