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

Properties

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

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.

load(propertyNames)

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

load(option)
toJSON()

Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.CustomXmlPartScopedCollection object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.CustomXmlPartScopedCollectionData) that contains an "items" array with shallow copies of any loaded properties from the collection's items.

Property Details

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

context: RequestContext;

Property Value

RequestContext

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 {
        console.log(`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.getItemOrNullObject("ContosoReviewXmlPartId").load("value");
    await context.sync();

    if (xmlPartIDSetting.value) {   
        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 {
        console.log(`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?: Excel.Interfaces.CustomXmlPartScopedCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions): Excel.CustomXmlPartScopedCollection;

Parameters

option
Excel.Interfaces.CustomXmlPartScopedCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions

Returns

Remarks

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

load(option?: string | string[]): Excel.CustomXmlPartScopedCollection - 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.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.

load(propertyNames)

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

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

Parameters

propertyNames
string | string[]

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

Returns

load(option)

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

Parameters

Returns

toJSON()

Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.CustomXmlPartScopedCollection object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.CustomXmlPartScopedCollectionData) that contains an "items" array with shallow copies of any loaded properties from the collection's items.

toJSON(): Excel.Interfaces.CustomXmlPartScopedCollectionData;

Returns