Excel.Binding class

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

Extends

Remarks

[ API set: ExcelApi 1.1 ]

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 the binding identifier.

type

Returns the type of the binding. See Excel.BindingType for details.

Methods

delete()

Deletes the binding.

getRange()

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

getTable()

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

getText()

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

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.

onSelectionChanged

Occurs when the selected content in the binding is changed.

Note*: If multiple, discontiguous cells are selected, Binding.onSelectionChanged only reports row and column information for one selection. Use Worksheet.onSelectionChanged for multiple selected ranges.

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 the binding identifier.

readonly id: string;

Property Value

string

Remarks

[ API set: ExcelApi 1.1 ]

type

Returns the type of the binding. See Excel.BindingType for details.

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

Property Value

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

Remarks

[ API set: ExcelApi 1.1 ]

Method Details

delete()

Deletes the binding.

delete(): void;

Returns

void

Remarks

[ API set: ExcelApi 1.3 ]

getRange()

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

getRange(): Excel.Range;

Returns

Remarks

[ API set: ExcelApi 1.1 ]

Examples

await Excel.run(async (context) => { 
    const binding = context.workbook.bindings.getItemAt(0);
    const range = binding.getRange();
    range.load('cellCount');
    await context.sync();

    console.log(range.cellCount);
});

getTable()

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

getTable(): Excel.Table;

Returns

Remarks

[ API set: ExcelApi 1.1 ]

Examples

await Excel.run(async (context) => { 
    const binding = context.workbook.bindings.getItemAt(0);
    const table = binding.getTable();
    table.load('name');
    await context.sync();

    console.log(table.name);
});

getText()

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

getText(): OfficeExtension.ClientResult<string>;

Returns

Remarks

[ API set: ExcelApi 1.1 ]

Examples

await Excel.run(async (context) => { 
    const binding = context.workbook.bindings.getItemAt(0);
    const text = binding.getText();
    binding.load('text');
    await context.sync();

    console.log(text);
});

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

await Excel.run(async (context) => { 
    const binding = context.workbook.bindings.getItemAt(0);
    binding.load('type');
    await context.sync();

    console.log(binding.type);
});

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.

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

Event Type

Remarks

[ API set: ExcelApi 1.2 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/30-events/data-changed.yaml

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.

Note*: If multiple, discontiguous cells are selected, Binding.onSelectionChanged only reports row and column information for one selection. Use Worksheet.onSelectionChanged for multiple selected ranges.

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

Event Type

Remarks

[ API set: ExcelApi 1.2 ]