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

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 ]

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 ]

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 ]

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

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
Excel.Binding[]

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

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

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

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

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

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

toJSON()

toJSON(): Excel.Interfaces.BindingCollectionData;
Returns