Excel.BindingCollection class

Represents the collection of all the binding objects that are part of the workbook.

[ API set: ExcelApi 1.1 ]

Extends
OfficeExtension.ClientObject

Properties

context

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

count

Returns the number of bindings in the collection. Read-only.

[ API set: ExcelApi 1.1 ]

items

Gets the loaded child items in this collection.

Methods

add(range, bindingType, id)

Add a new binding to a particular Range.

[ API set: ExcelApi 1.3 ]

add(range, bindingTypeString, id)

Add a new binding to a particular Range.

[ API set: ExcelApi 1.3 ]

addFromNamedItem(name, bindingType, id)

Add a new binding based on a named item in the workbook. If the named item references to multiple areas, the "InvalidReference" error will be returned.

[ API set: ExcelApi 1.3 ]

addFromNamedItem(name, bindingTypeString, id)

Add a new binding based on a named item in the workbook. If the named item references to multiple areas, the "InvalidReference" error will be returned.

[ API set: ExcelApi 1.3 ]

addFromSelection(bindingType, id)

Add a new binding based on the current selection. If the selection has multiple areas, the "InvalidReference" error will be returned.

[ API set: ExcelApi 1.3 ]

addFromSelection(bindingTypeString, id)

Add a new binding based on the current selection. If the selection has multiple areas, the "InvalidReference" error will be returned.

[ API set: ExcelApi 1.3 ]

getCount()

Gets the number of bindings in the collection.

[ API set: ExcelApi 1.4 ]

getItem(id)

Gets a binding object by ID.

[ API set: ExcelApi 1.1 ]

getItemAt(index)

Gets a binding object based on its position in the items array.

[ API set: ExcelApi 1.1 ]

getItemOrNullObject(id)

Gets a binding object by ID. If the binding object does not exist, will return a null object.

[ API set: ExcelApi 1.4 ]

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.BindingCollection object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.BindingCollectionData) 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

count

Returns the number of bindings in the collection. Read-only.

[ API set: ExcelApi 1.1 ]

readonly count: number;

Property Value

number

items

Gets the loaded child items in this collection.

readonly items: Excel.Binding[];

Property Value

Method Details

add(range, bindingType, id)

Add a new binding to a particular Range.

[ API set: ExcelApi 1.3 ]

add(range: Range | string, bindingType: Excel.BindingType, id: string): Excel.Binding;

Parameters

range
Range | string

Range to bind the binding to. May be an Excel Range object, or a string. If string, must contain the full address, including the sheet name

bindingType
Excel.BindingType

Type of binding. See Excel.BindingType.

id
string

Name of binding.

Returns

add(range, bindingTypeString, id)

Add a new binding to a particular Range.

[ API set: ExcelApi 1.3 ]

add(range: Range | string, bindingTypeString: "Range" | "Table" | "Text", id: string): Excel.Binding;

Parameters

range
Range | string

Range to bind the binding to. May be an Excel Range object, or a string. If string, must contain the full address, including the sheet name

bindingTypeString
"Range" | "Table" | "Text"

Type of binding. See Excel.BindingType.

id
string

Name of binding.

Returns

addFromNamedItem(name, bindingType, id)

Add a new binding based on a named item in the workbook. If the named item references to multiple areas, the "InvalidReference" error will be returned.

[ API set: ExcelApi 1.3 ]

addFromNamedItem(name: string, bindingType: Excel.BindingType, id: string): Excel.Binding;

Parameters

name
string

Name from which to create binding.

bindingType
Excel.BindingType

Type of binding. See Excel.BindingType.

id
string

Name of binding.

Returns

addFromNamedItem(name, bindingTypeString, id)

Add a new binding based on a named item in the workbook. If the named item references to multiple areas, the "InvalidReference" error will be returned.

[ API set: ExcelApi 1.3 ]

addFromNamedItem(name: string, bindingTypeString: "Range" | "Table" | "Text", id: string): Excel.Binding;

Parameters

name
string

Name from which to create binding.

bindingTypeString
"Range" | "Table" | "Text"

Type of binding. See Excel.BindingType.

id
string

Name of binding.

Returns

addFromSelection(bindingType, id)

Add a new binding based on the current selection. If the selection has multiple areas, the "InvalidReference" error will be returned.

[ API set: ExcelApi 1.3 ]

addFromSelection(bindingType: Excel.BindingType, id: string): Excel.Binding;

Parameters

bindingType
Excel.BindingType

Type of binding. See Excel.BindingType.

id
string

Name of binding.

Returns

addFromSelection(bindingTypeString, id)

Add a new binding based on the current selection. If the selection has multiple areas, the "InvalidReference" error will be returned.

[ API set: ExcelApi 1.3 ]

addFromSelection(bindingTypeString: "Range" | "Table" | "Text", id: string): Excel.Binding;

Parameters

bindingTypeString
"Range" | "Table" | "Text"

Type of binding. See Excel.BindingType.

id
string

Name of binding.

Returns

getCount()

Gets the number of bindings in the collection.

[ API set: ExcelApi 1.4 ]

getCount(): OfficeExtension.ClientResult<number>;

Returns

OfficeExtension.ClientResult<number>

getItem(id)

Gets a binding object by ID.

[ API set: ExcelApi 1.1 ]

getItem(id: string): Excel.Binding;

Parameters

id
string

Id of the binding object to be retrieved.

Returns

Examples

// Create a table binding to monitor data changes in the table. 
// When data is changed, the background color of the table will be changed to orange.
function addEventHandler() {
    // Create Table1
    Excel.run(function (ctx) { 
        ctx.workbook.tables.add("Sheet1!A1:C4", true);
        return ctx.sync().then(function() {
                console.log("My Diet Data Inserted!");
        })
        .catch(function (error) {
                console.log(JSON.stringify(error));
        });
    });
    //Create a new table binding for Table1
    Office.context.document.bindings.addFromNamedItemAsync(
        "Table1", Office.CoercionType.Table, { id: "myBinding" }, function (asyncResult) {
        if (asyncResult.status == "failed") {
            console.log("Action failed with error: " + asyncResult.error.message);
        }
        else {
            // If succeeded, then add event handler to the table binding.
            Office.select("bindings#myBinding").addHandlerAsync(
                Office.EventType.BindingDataChanged, onBindingDataChanged);
        }
    });
}
    
// when data in the table is changed, this event will be triggered.
function onBindingDataChanged(eventArgs) {
    Excel.run(function (ctx) { 
        // highlight the table in orange to indicate data has been changed.
        ctx.workbook.bindings.getItem(eventArgs.binding.id).getTable().getDataBodyRange().format.fill.color = "Orange";
        return ctx.sync().then(function() {
                console.log("The value in this table got changed!");
        })
        .catch(function (error) {
                console.log(JSON.stringify(error));
        });
    });
}

getItemAt(index)

Gets a binding object based on its position in the items array.

[ API set: ExcelApi 1.1 ]

getItemAt(index: number): Excel.Binding;

Parameters

index
number

Index value of the object to be retrieved. Zero-indexed.

Returns

Examples

Excel.run(function (ctx) { 
    var lastPosition = ctx.workbook.bindings.count - 1;
    var binding = ctx.workbook.bindings.getItemAt(lastPosition);
    binding.load('type')
    return ctx.sync().then(function() {
            console.log(binding.type); 
    });
}).catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

getItemOrNullObject(id)

Gets a binding object by ID. If the binding object does not exist, will return a null object.

[ API set: ExcelApi 1.4 ]

getItemOrNullObject(id: string): Excel.Binding;

Parameters

id
string

Id of the binding object to be retrieved.

Returns

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.BindingCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions): Excel.BindingCollection;

Parameters

options
Excel.Interfaces.BindingCollectionLoadOptions & 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.BindingCollection;

Parameters

propertyNames
string | string[]

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

Returns

Examples

Excel.run(function (ctx) { 
    var bindings = ctx.workbook.bindings;
    bindings.load('items');
    return ctx.sync().then(function() {
        for (var i = 0; i < bindings.items.length; i++)
        {
            console.log(bindings.items[i].id);
        }
    });
}).catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
// Get the number of bindings
Excel.run(function (ctx) { 
    var bindings = ctx.workbook.bindings;
    bindings.load('count');
    return ctx.sync().then(function() {
        console.log("Bindings: Count= " + bindings.count);
    });
}).catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

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

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.BindingCollection object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.BindingCollectionData) that contains an "items" array with shallow copies of any loaded properties from the collection's items.

toJSON(): Excel.Interfaces.BindingCollectionData;

Returns