Excel.CustomXmlPartScopedCollection class

A scoped collection of custom XML parts. A scoped collection is the result of some operation, e.g. filtering by namespace. A scoped collection cannot be scoped any further.

[ API set: ExcelApi 1.5 ]

Extends
OfficeExtension.ClientObject

Properties

items

Gets the loaded child items in this collection.

Methods

getCount()

Gets the number of CustomXML parts in this collection.

[ API set: ExcelApi 1.5 ]

getItem(id)

Gets a custom XML part based on its ID.

[ API set: ExcelApi 1.5 ]

getItemOrNullObject(id)

Gets a custom XML part based on its ID. If the CustomXmlPart does not exist, the return object's isNull property will be true.

[ API set: ExcelApi 1.5 ]

getOnlyItem()

If the collection contains exactly one item, this method returns it. Otherwise, this method produces an error.

[ API set: ExcelApi 1.5 ]

getOnlyItemOrNullObject()

If the collection contains exactly one item, this method returns it. Otherwise, this method returns Null.

[ API set: ExcelApi 1.5 ]

load(option)

Queues up a command to load the specified properties of the object. You must call "context.sync()" before reading the properties.

toJSON()

Property Details

items

Gets the loaded child items in this collection.

readonly items: Excel.CustomXmlPart[];
Property Value
Excel.CustomXmlPart[]

Method Details

getCount()

Gets the number of CustomXML parts in this collection.

[ API set: ExcelApi 1.5 ]

getCount(): OfficeExtension.ClientResult<number>;
Returns
OfficeExtension.ClientResult<number>

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 {
        OfficeHelpers.UI.notify(`There are ${numberOfPartsInNamespace.value} XML parts with namespace ${contosoNamespace}.
            There should be exactly 1.`);
    }        

    await context.sync();
});

getItem(id)

Gets a custom XML part based on its ID.

[ API set: ExcelApi 1.5 ]

getItem(id: string): Excel.CustomXmlPart;
Parameters
id
string

ID of the object to be retrieved.

Returns

getItemOrNullObject(id)

Gets a custom XML part based on its ID. If the CustomXmlPart does not exist, the return object's isNull property will be true.

[ API set: ExcelApi 1.5 ]

getItemOrNullObject(id: string): Excel.CustomXmlPart;
Parameters
id
string

ID of the object to be retrieved.

Returns

Examples

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

    await context.sync();

    let customXmlPart = context.workbook.customXmlParts.getItem(xmlPartIDSetting.value);
    const xmlBlob = customXmlPart.getXml();
    customXmlPart.delete();
    customXmlPart = context.workbook.customXmlParts.getItemOrNullObject(xmlPartIDSetting.value);

    await context.sync();

    if (customXmlPart.isNullObject) {
        $("#display-xml").text(`The XML part with the id ${xmlPartIDSetting.value} has been deleted.`);

        // Delete the unneeded setting too.
        xmlPartIDSetting.delete();            
    } else {
        const readableXml = addLineBreaksToXML(xmlBlob.value);
        const strangeMessage = `This is strange. The XML part with the id ${xmlPartIDSetting.value} has not been deleted:\n${readableXml}`
        $("#display-xml").text(strangeMessage);
    }

    await context.sync();
});

getOnlyItem()

If the collection contains exactly one item, this method returns it. Otherwise, this method produces an error.

[ API set: ExcelApi 1.5 ]

getOnlyItem(): Excel.CustomXmlPart;
Returns

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 {
        OfficeHelpers.UI.notify(`There are ${numberOfPartsInNamespace.value} XML parts with namespace ${contosoNamespace}.
            There should be exactly 1.`);
    }        

    await context.sync();
});

getOnlyItemOrNullObject()

If the collection contains exactly one item, this method returns it. Otherwise, this method returns Null.

[ API set: ExcelApi 1.5 ]

getOnlyItemOrNullObject(): Excel.CustomXmlPart;
Returns

load(option)

Queues up a command to load the specified properties of the object. You must call "context.sync()" before reading the properties.

load(option?: string | string[]): Excel.CustomXmlPartScopedCollection;
Parameters
option
string | string[]

A comma-delimited string or an array of strings that specify the properties to load.

Returns
Remarks

In addition to this signature, this method has the following signatures:

load(option?: { select?: string; expand?: string; }): Excel.CustomXmlPartScopedCollection - 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.CustomXmlPartScopedCollection - Only available on collection types. It is similar to the preceding signature. Option.top specifies the maximum number of collection items that can be included in the result. Option.skip specifies the number of items that are to be skipped and not included in the result. If option.top is specified, the result set will start after skipping the specified number of items.

toJSON()

toJSON(): Excel.Interfaces.CustomXmlPartScopedCollectionData;
Returns