Excel.Binding class

Represents an Office.js binding that is defined in the workbook.

[ API set: ExcelApi 1.1 ]

Extends

Properties

context

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

id

Represents binding identifier. Read-only.

[ API set: ExcelApi 1.1 ]

type

Returns the type of the binding. See Excel.BindingType for details. Read-only.

[ API set: ExcelApi 1.1 ]

Methods

delete()

Deletes the binding.

[ API set: ExcelApi 1.3 ]

getRange()

Returns the range represented by the binding. Will throw an error if binding is not of the correct type.

[ API set: ExcelApi 1.1 ]

getTable()

Returns the table represented by the binding. Will throw an error if binding is not of the correct type.

[ API set: ExcelApi 1.1 ]

getText()

Returns the text represented by the binding. Will throw an error if binding is not of the correct type.

[ API set: ExcelApi 1.1 ]

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

Events

onDataChanged

Occurs when data or formatting within the binding is changed.

[ API set: ExcelApi 1.2 ]

onSelectionChanged

Occurs when the selected content in the binding is changed.

[ API set: ExcelApi 1.2 ]

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

id

Represents binding identifier. Read-only.

[ API set: ExcelApi 1.1 ]

readonly id: string;

Property Value

string

type

Returns the type of the binding. See Excel.BindingType for details. Read-only.

[ API set: ExcelApi 1.1 ]

readonly type: Excel.BindingType | "Range" | "Table" | "Text";

Property Value

Excel.BindingType | "Range" | "Table" | "Text"

Method Details

delete()

Deletes the binding.

[ API set: ExcelApi 1.3 ]

delete(): void;

Returns

void

getRange()

Returns the range represented by the binding. Will throw an error if binding is not of the correct type.

[ API set: ExcelApi 1.1 ]

getRange(): Excel.Range;

Returns

Examples

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

getTable()

Returns the table represented by the binding. Will throw an error if binding is not of the correct type.

[ API set: ExcelApi 1.1 ]

getTable(): Excel.Table;

Returns

Examples

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

getText()

Returns the text represented by the binding. Will throw an error if binding is not of the correct type.

[ API set: ExcelApi 1.1 ]

getText(): OfficeExtension.ClientResult<string>;

Returns

OfficeExtension.ClientResult<string>

Examples

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

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.BindingLoadOptions): Excel.Binding;

Parameters

Returns

Remarks

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

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

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 binding = ctx.workbook.bindings.getItemAt(0);
    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));
    }
});

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

Parameters

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

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

toJSON(): Excel.Interfaces.BindingData;

Returns

Event Details

onDataChanged

Occurs when data or formatting within the binding is changed.

[ API set: ExcelApi 1.2 ]

readonly onDataChanged: OfficeExtension.EventHandlers<Excel.BindingDataChangedEventArgs>;

Returns

OfficeExtension.EventHandlers<Excel.BindingDataChangedEventArgs>

Examples

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");    
    const salesTable = sheet.tables.getItem("SalesTable");
    const salesByQuarterBinding = context.workbook.bindings.add(salesTable.getRange(), "Table", "SalesByQuarter");
    salesByQuarterBinding.onDataChanged.add(onSalesDataChanged);

    console.log("The data changed handler is registered.");

    await context.sync();
});

onSelectionChanged

Occurs when the selected content in the binding is changed.

[ API set: ExcelApi 1.2 ]

readonly onSelectionChanged: OfficeExtension.EventHandlers<Excel.BindingSelectionChangedEventArgs>;

Returns

OfficeExtension.EventHandlers<Excel.BindingSelectionChangedEventArgs>