Excel.CustomXmlPart class

Represents a custom XML part object in a workbook.

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.

id

The custom XML part's ID.

namespaceUri

The custom XML part's namespace URI.

Methods

delete()

Deletes the custom XML part.

getXml()

Gets the custom XML part's full XML content.

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.

setXml(xml)

Sets the custom XML part's full XML content.

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.CustomXmlPart object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.CustomXmlPartData) that contains shallow copies of any loaded child properties from the original object.

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

id

The custom XML part's ID.

readonly id: string;

Property Value

string

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) => {
    // 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();
});

namespaceUri

The custom XML part's namespace URI.

readonly namespaceUri: string;

Property Value

string

Remarks

[ API set: ExcelApi 1.5 ]

Method Details

delete()

Deletes the custom XML part.

delete(): void;

Returns

void

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();
    }
});

getXml()

Gets the custom XML part's full XML content.

getXml(): OfficeExtension.ClientResult<string>;

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) => {
    // 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();
});

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.CustomXmlPartLoadOptions): Excel.CustomXmlPart;

Parameters

options
Excel.Interfaces.CustomXmlPartLoadOptions

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.CustomXmlPart;

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?: {
            select?: string;
            expand?: string;
        }): Excel.CustomXmlPart;

Parameters

propertyNamesAndPaths

{ select?: string; expand?: string; }

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

setXml(xml)

Sets the custom XML part's full XML content.

setXml(xml: string): void;

Parameters

xml

string

XML content for the part.

Returns

void

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) {   
        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();
    }
});

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.CustomXmlPart object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.CustomXmlPartData) that contains shallow copies of any loaded child properties from the original object.

toJSON(): Excel.Interfaces.CustomXmlPartData;

Returns