Excel.Binding class

表示工作簿中定义的 Office.js 绑定。Represents an Office.js binding that is defined in the workbook.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

Extends

属性

context

与该对象关联的请求上下文。The request context associated with the object. 这会将加载项的进程连接到 Office 主机应用程序的进程。This connects the add-in's process to the Office host application's process.

id

表示绑定标识符。Represents binding identifier. 只读。Read-only.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

type

返回绑定的类型。Returns the type of the binding. 有关详细信息, 请参阅 BindingType。See Excel.BindingType for details. 只读。Read-only.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

方法

delete()

删除 binding 对象。Deletes the binding.

[API 集: ExcelApi 1.3][ 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 集: ExcelApi 1.1][ 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 集: ExcelApi 1.1][ 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 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

load(option)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性之前,你必须调用“context.sync()”。You must call "context.sync()" before reading the properties.

load(propertyNames)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

toJSON()

重写 JavaScript toJSON()方法, 以便在将 API 对象传递到时提供更有用的输出JSON.stringify()Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify反过来, 也会调用传递toJSON给它的对象的方法。尽管原始的 Excel. Binding 对象是 API 对象, 但toJSON方法返回一个简单的 JavaScript 对象 (类型为Excel.Interfaces.BindingData, 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.

事件

onDataChanged

当绑定内的数据或格式变化时发生。Occurs when data or formatting within the binding is changed.

[API 集: ExcelApi 1.2][ API set: ExcelApi 1.2 ]

onSelectionChanged

当绑定中的所选内容更改时发生。Occurs when the selected content in the binding is changed.

[API 集: ExcelApi 1.2][ API set: ExcelApi 1.2 ]

属性详细信息

context

与该对象关联的请求上下文。The request context associated with the object. 这会将加载项的进程连接到 Office 主机应用程序的进程。This connects the add-in's process to the Office host application's process.

context: RequestContext;

属性值

RequestContext

id

表示绑定标识符。Represents binding identifier. 只读。Read-only.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

readonly id: string;

属性值

string

type

返回绑定的类型。Returns the type of the binding. 有关详细信息, 请参阅 BindingType。See Excel.BindingType for details. 只读。Read-only.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

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

属性值

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

方法详细信息

delete()

删除 binding 对象。Deletes the binding.

[API 集: ExcelApi 1.3][ API set: ExcelApi 1.3 ]

delete(): void;

返回

void

getRange()

返回绑定表示的区域。Returns the range represented by the binding. 如果绑定类型不正确,将引发错误。Will throw an error if binding is not of the correct type.

[API 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

getRange(): Excel.Range;

返回

示例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 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

getTable(): Excel.Table;

返回

示例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 集: ExcelApi 1.1][ API set: ExcelApi 1.1 ]

getText(): OfficeExtension.ClientResult<string>;

返回

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. 在读取属性之前,你必须调用“context.sync()”。You must call "context.sync()" before reading the properties.

load(option?: Excel.Interfaces.BindingLoadOptions): Excel.Binding;

参数

返回

注解

除了此签名之外, 此方法还具有以下签名:In addition to this signature, this method has the following signatures:

load(option?: string | string[]): Excel.Binding-Where 选项是一个逗号分隔的字符串或一个字符串数组, 用于指定要加载的属性。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 选项。选择是一个用逗号分隔的字符串, 用于指定要加载的属性和选项。 expand 是一个用逗号分隔的字符串, 用于指定要加载的导航属性。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-仅适用于集合类型。load(option?: { select?: string; expand?: string; top?: number; skip?: number }): Excel.Binding - Only available on collection types. 它类似于前面的签名。It is similar to the preceding signature. top 指定结果中可包含的集合项的最大数量。Option.top specifies the maximum number of collection items that can be included in the result. 选项。 skip 指定要跳过且不包含在结果中的项目数。Option.skip specifies the number of items that are to be skipped and not included in the result. 如果指定了 top 选项, 则在跳过指定数目的项目后将启动结果集。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. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

load(propertyNames?: string | string[]): Excel.Binding;

参数

propertyNames
string | string[]

一个以逗号分隔的字符串或字符串数组, 用于指定要加载的属性。A comma-delimited string or an array of strings that specify the properties to load.

返回

示例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. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

load(propertyNamesAndPaths?: { select?: string; expand?: string; }): Excel.Binding;

参数

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

其中, propertyNamesAndPaths 是用逗号分隔的字符串, 用于指定要加载的属性和 propertyNamesAndPaths。扩展是一个用逗号分隔的字符串, 用于指定要加载的导航属性。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.

返回

toJSON()

重写 JavaScript toJSON()方法, 以便在将 API 对象传递到时提供更有用的输出JSON.stringify()Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify反过来, 也会调用传递toJSON给它的对象的方法。尽管原始的 Excel. Binding 对象是 API 对象, 但toJSON方法返回一个简单的 JavaScript 对象 (类型为Excel.Interfaces.BindingData, 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;

返回

事件详细信息

onDataChanged

当绑定内的数据或格式变化时发生。Occurs when data or formatting within the binding is changed.

[API 集: ExcelApi 1.2][ API set: ExcelApi 1.2 ]

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

返回

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 集: ExcelApi 1.2][ API set: ExcelApi 1.2 ]

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

返回

OfficeExtension.EventHandlers<Excel.BindingSelectionChangedEventArgs>