Excel.Binding class

Represents an Office.js binding that is defined in 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.

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

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

Parameters

options
Excel.Interfaces.BindingLoadOptions

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

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>