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.

Extends

Remarks

[ API set: ExcelApi 1.5 ]

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.

getItem(id)

Gets a custom XML part based on its ID.

getItemOrNullObject(id)

Gets a custom XML part based on its ID. If the CustomXmlPart does not exist, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

getOnlyItem()

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

getOnlyItemOrNullObject()

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

load(options)

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(propertyNamesAndPaths)

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

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

items

Gets the loaded child items in this collection.

readonly items: Excel.CustomXmlPart[];

Property Value

Method Details

getCount()

Gets the number of CustomXML parts in this collection.

getCount(): OfficeExtension.ClientResult<number>;

Returns

Remarks

[ API set: ExcelApi 1.5 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/18-custom-xml-parts/test-xml-for-unique-namespace.yaml

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.

getItem(id: string): Excel.CustomXmlPart;

Parameters

id

string

ID of the object to be retrieved.

Returns

Remarks

[ API set: ExcelApi 1.5 ]

getItemOrNullObject(id)

Gets a custom XML part based on its ID. If the CustomXmlPart does not exist, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

getItemOrNullObject(id: string): Excel.CustomXmlPart;

Parameters

id

string

ID of the object to be retrieved.

Returns

Remarks

[ API set: ExcelApi 1.5 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/18-custom-xml-parts/create-set-get-and-delete-custom-xml-parts.yaml

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.

getOnlyItem(): Excel.CustomXmlPart;

Returns

Remarks

[ API set: ExcelApi 1.5 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/18-custom-xml-parts/test-xml-for-unique-namespace.yaml

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.

getOnlyItemOrNullObject(): Excel.CustomXmlPart;

Returns

Remarks

[ API set: ExcelApi 1.5 ]

load(options)

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

load(options?: Excel.Interfaces.CustomXmlPartScopedCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions): Excel.CustomXmlPartScopedCollection;

Parameters

options

Excel.Interfaces.CustomXmlPartScopedCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions

Provides options for which properties of the object to load.

Returns

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(propertyNamesAndPaths)

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

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

Parameters

propertyNamesAndPaths
OfficeExtension.LoadOption

propertyNamesAndPaths.select is a comma-delimited string that specifies the properties to load, and propertyNamesAndPaths.expand is a comma-delimited string that specifies the navigation properties to load.

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