Excel.CustomXmlPartCollection class

自定义 XML 部件的集合。A collection of custom XML parts.

[API 集: ExcelApi 1.5][ API set: ExcelApi 1.5 ]

Extends

属性

context

与该对象关联的请求上下文。The request context associated with the object. 这会将加载项的进程连接到 Office 主机应用程序的进程。This connects the add-in's process to the Office host application's process.

items

获取此集合中已加载的子项。Gets the loaded child items in this collection.

方法

add(xml)

向工作簿添加新的自定义 XML 部件。Adds a new custom XML part to the workbook.

[API 集: ExcelApi 1.5][ API set: ExcelApi 1.5 ]

getByNamespace(namespaceUri)

获取其命名空间匹配给定命名空间的自定义 XML 部件的新作用域内集合。Gets a new scoped collection of custom XML parts whose namespaces match the given namespace.

[API 集: ExcelApi 1.5][ API set: ExcelApi 1.5 ]

getCount()

获取此集合中 CustomXml 部件的数量。Gets the number of CustomXml parts in the collection.

[API 集: ExcelApi 1.5][ API set: ExcelApi 1.5 ]

getItem(id)

获取基于其 ID 的自定义 XML 部件。Gets a custom XML part based on its ID.

[API 集: ExcelApi 1.5][ API set: ExcelApi 1.5 ]

getItemOrNullObject(id)

获取基于其 ID 的自定义 XML 部件。Gets a custom XML part based on its ID. 如果 CustomXmlPart 不存在, 则返回对象的 isNull 属性将为 true。If the CustomXmlPart does not exist, the return object's isNull property will be true.

[API 集: ExcelApi 1.5][ API set: ExcelApi 1.5 ]

load(option)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性之前,你必须调用“context.sync()”。You must call "context.sync()" before reading the properties.

load(propertyNames)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

load(option)
toJSON()

重写 JavaScript toJSON()方法, 以便在将 API 对象传递到时提供更有用的输出JSON.stringify()Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify反过来, 也会调用传递toJSON给它的对象的方法。尽管原始Excel.CustomXmlPartCollection对象是 API 对象, 但toJSON方法返回一个无格式的 JavaScript 对象 (类型为Excel.Interfaces.CustomXmlPartCollectionData, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.CustomXmlPartCollection object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.CustomXmlPartCollectionData), 其中包含集合的项中所有加载属性的浅表副本的 "items" 数组。) that contains an "items" array with shallow copies of any loaded properties from the collection's items.

属性详细信息

context

与该对象关联的请求上下文。The request context associated with the object. 这会将加载项的进程连接到 Office 主机应用程序的进程。This connects the add-in's process to the Office host application's process.

context: RequestContext;

属性值

RequestContext

items

获取此集合中已加载的子项。Gets the loaded child items in this collection.

readonly items: Excel.CustomXmlPart[];

属性值

Excel.CustomXmlPart[]

方法详细信息

add(xml)

向工作簿添加新的自定义 XML 部件。Adds a new custom XML part to the workbook.

[API 集: ExcelApi 1.5][ API set: ExcelApi 1.5 ]

add(xml: string): Excel.CustomXmlPart;

参数

xml
string

XML 内容。XML content. 必须是有效的 XML 片段。Must be a valid XML fragment.

返回

示例Examples

await Excel.run(async (context) => {
    // You must have the xmlns attribute to populate the 
    // CustomXml.namespaceUri property.
    const originalXml = "<Reviewers xmlns='http://schemas.contoso.com/review/1.0'><Reviewer>Juan</Reviewer><Reviewer>Hong</Reviewer><Reviewer>Sally</Reviewer></Reviewers>";
    const customXmlPart = context.workbook.customXmlParts.add(originalXml);
    customXmlPart.load("id");
    const xmlBlob = customXmlPart.getXml();

    await context.sync();

    const readableXml = addLineBreaksToXML(xmlBlob.value);
    $("#display-xml").text(readableXml);

    // Store the XML part's ID in a setting.
    const settings = context.workbook.settings;
    settings.add("ContosoReviewXmlPartId", customXmlPart.id);

    await context.sync();
});

getByNamespace(namespaceUri)

获取其命名空间匹配给定命名空间的自定义 XML 部件的新作用域内集合。Gets a new scoped collection of custom XML parts whose namespaces match the given namespace.

[API 集: ExcelApi 1.5][ API set: ExcelApi 1.5 ]

getByNamespace(namespaceUri: string): Excel.CustomXmlPartScopedCollection;

参数

namespaceUri
string

这必须是完全限定的架构 URI;例如, "http://schemas.contoso.com/review/1.0"。This must be a fully qualified schema URI; for example, "http://schemas.contoso.com/review/1.0".

返回

示例Examples

await Excel.run(async (context) => {
    $("#display-xml").text("");
    const contosoNamespace = "http://schemas.contoso.com/review/1.0";
    const customXmlParts = context.workbook.customXmlParts;
    const filteredXmlParts = customXmlParts.getByNamespace(contosoNamespace);
    const numberOfPartsInNamespace = filteredXmlParts.getCount();

    await context.sync();

    if (numberOfPartsInNamespace.value == 1) {
        const onlyXmlPartInNamespace = filteredXmlParts.getOnlyItem();
        const xmlBlob = onlyXmlPartInNamespace.getXml();

        await context.sync();

        // Make it a bit more readable.
        const readableXml = xmlBlob.value.replace(/></g, ">\n<");

        $("#display-xml").text(`The only XML part in the namespace ${contosoNamespace} is:
            ${readableXml}`);

    } else {
        console.log(`There are ${numberOfPartsInNamespace.value} XML parts with namespace ${contosoNamespace}. There should be exactly 1.`);
    }        

    await context.sync();
});

getCount()

获取此集合中 CustomXml 部件的数量。Gets the number of CustomXml parts in the collection.

[API 集: ExcelApi 1.5][ API set: ExcelApi 1.5 ]

getCount(): OfficeExtension.ClientResult<number>;

返回

OfficeExtension.ClientResult<number>

getItem(id)

获取基于其 ID 的自定义 XML 部件。Gets a custom XML part based on its ID.

[API 集: ExcelApi 1.5][ API set: ExcelApi 1.5 ]

getItem(id: string): Excel.CustomXmlPart;

参数

id
string

要检索的对象的 ID。ID of the object to be retrieved.

返回

示例Examples

await Excel.run(async (context) => {
    const settings = context.workbook.settings;
    const xmlPartIDSetting = settings.getItemOrNullObject("ContosoReviewXmlPartId").load("value");
    await context.sync();

    if (xmlPartIDSetting.value) {   
        const customXmlPart = context.workbook.customXmlParts.getItem(xmlPartIDSetting.value);

        // The setXml method does a whole-for-whole replacement 
        // of the entire XML.
        customXmlPart.setXml("<Reviewers xmlns='http://schemas.contoso.com/review/1.0'><Reviewer>John</Reviewer><Reviewer>Hitomi</Reviewer></Reviewers>");
        const xmlBlob = customXmlPart.getXml();
        await context.sync();

        const readableXml = addLineBreaksToXML(xmlBlob.value);
        $("#display-xml").text(readableXml);
        await context.sync();
    }
});

getItemOrNullObject(id)

获取基于其 ID 的自定义 XML 部件。Gets a custom XML part based on its ID. 如果 CustomXmlPart 不存在, 则返回对象的 isNull 属性将为 true。If the CustomXmlPart does not exist, the return object's isNull property will be true.

[API 集: ExcelApi 1.5][ API set: ExcelApi 1.5 ]

getItemOrNullObject(id: string): Excel.CustomXmlPart;

参数

id
string

要检索的对象的 ID。ID of the object to be retrieved.

返回

load(option)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性之前,你必须调用“context.sync()”。You must call "context.sync()" before reading the properties.

load(option?: Excel.Interfaces.CustomXmlPartCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions): Excel.CustomXmlPartCollection;

参数

option
Excel.Interfaces.CustomXmlPartCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions

返回

注解

除了此签名之外, 此方法还具有以下签名:In addition to this signature, this method has the following signatures:

load(option?: string | string[]): Excel.CustomXmlPartCollection-Where 选项是一个逗号分隔的字符串或一个字符串数组, 用于指定要加载的属性。load(option?: string | string[]): Excel.CustomXmlPartCollection - Where option is a comma-delimited string or an array of strings that specify the properties to load.

load(option?: { select?: string; expand?: string; }): Excel.CustomXmlPartCollection-Where 选项。选择是一个用逗号分隔的字符串, 用于指定要加载的属性和选项。 expand 是一个用逗号分隔的字符串, 用于指定要加载的导航属性。load(option?: { select?: string; expand?: string; }): Excel.CustomXmlPartCollection - Where option.select is a comma-delimited string that specifies the properties to load, and options.expand is a comma-delimited string that specifies the navigation properties to load.

load(option?: { select?: string; expand?: string; top?: number; skip?: number }): Excel.CustomXmlPartCollection-仅适用于集合类型。load(option?: { select?: string; expand?: string; top?: number; skip?: number }): Excel.CustomXmlPartCollection - Only available on collection types. 它类似于前面的签名。It is similar to the preceding signature. top 指定结果中可包含的集合项的最大数量。Option.top specifies the maximum number of collection items that can be included in the result. 选项。 skip 指定要跳过且不包含在结果中的项目数。Option.skip specifies the number of items that are to be skipped and not included in the result. 如果指定了 top 选项, 则在跳过指定数目的项目后将启动结果集。If option.top is specified, the result set will start after skipping the specified number of items.

load(propertyNames)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

load(propertyNames?: string | string[]): Excel.CustomXmlPartCollection;

参数

propertyNames
string | string[]

一个以逗号分隔的字符串或字符串数组, 用于指定要加载的属性。A comma-delimited string or an array of strings that specify the properties to load.

返回

load(option)

load(option?: OfficeExtension.LoadOption): Excel.CustomXmlPartCollection;

参数

返回

toJSON()

重写 JavaScript toJSON()方法, 以便在将 API 对象传递到时提供更有用的输出JSON.stringify()Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify反过来, 也会调用传递toJSON给它的对象的方法。尽管原始Excel.CustomXmlPartCollection对象是 API 对象, 但toJSON方法返回一个无格式的 JavaScript 对象 (类型为Excel.Interfaces.CustomXmlPartCollectionData, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.CustomXmlPartCollection object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.CustomXmlPartCollectionData), 其中包含集合的项中所有加载属性的浅表副本的 "items" 数组。) that contains an "items" array with shallow copies of any loaded properties from the collection's items.

toJSON(): Excel.Interfaces.CustomXmlPartCollectionData;

返回