Excel.BindingCollection class

表示属于工作簿的所有绑定对象的集合。Represents the collection of all the binding objects that are part of 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.

count

返回集合中绑定的数量。Returns the number of bindings in the collection. 只读。Read-only.

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

items

获取此集合中已加载的子项。Gets the loaded child items in this collection.

方法

add(range, bindingType, id)

将新的 binding 对象添加到特定区域。Add a new binding to a particular Range.

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

add(range, bindingTypeString, id)

将新的 binding 对象添加到特定区域。Add a new binding to a particular Range.

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

addFromNamedItem(name, bindingType, id)

根据工作簿中的命名项添加新的 binding 对象。Add a new binding based on a named item in the workbook. 如果已命名项目引用多个区域, 则将返回 "InvalidReference" 错误。If the named item references to multiple areas, the "InvalidReference" error will be returned.

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

addFromNamedItem(name, bindingTypeString, id)

根据工作簿中的命名项添加新的 binding 对象。Add a new binding based on a named item in the workbook. 如果已命名项目引用多个区域, 则将返回 "InvalidReference" 错误。If the named item references to multiple areas, the "InvalidReference" error will be returned.

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

addFromSelection(bindingType, id)

根据当前选择的内容添加新的 binding 对象。Add a new binding based on the current selection. 如果所选内容有多个区域, 则将返回 "InvalidReference" 错误。If the selection has multiple areas, the "InvalidReference" error will be returned.

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

addFromSelection(bindingTypeString, id)

根据当前选择的内容添加新的 binding 对象。Add a new binding based on the current selection. 如果所选内容有多个区域, 则将返回 "InvalidReference" 错误。If the selection has multiple areas, the "InvalidReference" error will be returned.

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

getCount()

获取集合中的绑定数量。Gets the number of bindings in the collection.

[API 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

getItem(id)

按 ID 获取绑定对象。Gets a binding object by ID.

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

getItemAt(index)

根据其在项目数组中的位置获取绑定对象。Gets a binding object based on its position in the items array.

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

getItemOrNullObject(id)

按 ID 获取 Binding 对象。Gets a binding object by ID. 如果没有 Binding 对象,将返回 NULL 对象。If the binding object does not exist, will return a null object.

[API 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

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(option)
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.BindingCollection对象是 API 对象, 但toJSON方法返回一个无格式的 JavaScript 对象 (类型为Excel.Interfaces.BindingCollectionData, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.BindingCollection object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.BindingCollectionData), 其中包含集合的项中所有加载属性的浅表副本的 "items" 数组。) that contains an "items" array with shallow copies of any loaded properties from the collection's items.

属性详细信息

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

count

返回集合中绑定的数量。Returns the number of bindings in the collection. 只读。Read-only.

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

readonly count: number;

属性值

number

items

获取此集合中已加载的子项。Gets the loaded child items in this collection.

readonly items: Excel.Binding[];

属性值

Excel.Binding[]

方法详细信息

add(range, bindingType, id)

将新的 binding 对象添加到特定区域。Add a new binding to a particular Range.

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

add(range: Range | string, bindingType: Excel.BindingType, id: string): Excel.Binding;

参数

range
Range | string

要将绑定绑定到的范围。可以是 Excel 范围对象,也可以是字符串。如果是字符串,必须包含完整地址,包括工作表名称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. 请参阅 BindingType。See Excel.BindingType.

id
string

绑定的名称。Name of binding.

返回

add(range, bindingTypeString, id)

将新的 binding 对象添加到特定区域。Add a new binding to a particular Range.

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

add(range: Range | string, bindingTypeString: "Range" | "Table" | "Text", id: string): Excel.Binding;

参数

range
Range | string

要将绑定绑定到的范围。可以是 Excel 范围对象,也可以是字符串。如果是字符串,必须包含完整地址,包括工作表名称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

bindingTypeString
"Range" | "Table" | "Text"

绑定的类型。Type of binding. 请参阅 BindingType。See Excel.BindingType.

id
string

绑定的名称。Name of binding.

返回

addFromNamedItem(name, bindingType, id)

根据工作簿中的命名项添加新的 binding 对象。Add a new binding based on a named item in the workbook. 如果已命名项目引用多个区域, 则将返回 "InvalidReference" 错误。If the named item references to multiple areas, the "InvalidReference" error will be returned.

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

addFromNamedItem(name: string, bindingType: Excel.BindingType, id: string): Excel.Binding;

参数

name
string

从中创建绑定的名称。Name from which to create binding.

bindingType
Excel.BindingType

绑定的类型。Type of binding. 请参阅 BindingType。See Excel.BindingType.

id
string

绑定的名称。Name of binding.

返回

addFromNamedItem(name, bindingTypeString, id)

根据工作簿中的命名项添加新的 binding 对象。Add a new binding based on a named item in the workbook. 如果已命名项目引用多个区域, 则将返回 "InvalidReference" 错误。If the named item references to multiple areas, the "InvalidReference" error will be returned.

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

addFromNamedItem(name: string, bindingTypeString: "Range" | "Table" | "Text", id: string): Excel.Binding;

参数

name
string

从中创建绑定的名称。Name from which to create binding.

bindingTypeString
"Range" | "Table" | "Text"

绑定的类型。Type of binding. 请参阅 BindingType。See Excel.BindingType.

id
string

绑定的名称。Name of binding.

返回

addFromSelection(bindingType, id)

根据当前选择的内容添加新的 binding 对象。Add a new binding based on the current selection. 如果所选内容有多个区域, 则将返回 "InvalidReference" 错误。If the selection has multiple areas, the "InvalidReference" error will be returned.

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

addFromSelection(bindingType: Excel.BindingType, id: string): Excel.Binding;

参数

bindingType
Excel.BindingType

绑定的类型。Type of binding. 请参阅 BindingType。See Excel.BindingType.

id
string

绑定的名称。Name of binding.

返回

addFromSelection(bindingTypeString, id)

根据当前选择的内容添加新的 binding 对象。Add a new binding based on the current selection. 如果所选内容有多个区域, 则将返回 "InvalidReference" 错误。If the selection has multiple areas, the "InvalidReference" error will be returned.

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

addFromSelection(bindingTypeString: "Range" | "Table" | "Text", id: string): Excel.Binding;

参数

bindingTypeString
"Range" | "Table" | "Text"

绑定的类型。Type of binding. 请参阅 BindingType。See Excel.BindingType.

id
string

绑定的名称。Name of binding.

返回

getCount()

获取集合中的绑定数量。Gets the number of bindings in the collection.

[API 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

getCount(): OfficeExtension.ClientResult<number>;

返回

OfficeExtension.ClientResult<number>

getItem(id)

按 ID 获取绑定对象。Gets a binding object by ID.

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

getItem(id: string): Excel.Binding;

参数

id
string

要检索的绑定对象的 ID。Id of the binding object to be retrieved.

返回

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

getItemAt(index)

根据其在项目数组中的位置获取绑定对象。Gets a binding object based on its position in the items array.

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

getItemAt(index: number): Excel.Binding;

参数

index
number

要检索的对象的索引值。Index value of the object to be retrieved. 从零开始编制索引。Zero-indexed.

返回

示例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)

按 ID 获取 Binding 对象。Gets a binding object by ID. 如果没有 Binding 对象,将返回 NULL 对象。If the binding object does not exist, will return a null object.

[API 集: ExcelApi 1.4][ API set: ExcelApi 1.4 ]

getItemOrNullObject(id: string): Excel.Binding;

参数

id
string

要检索的绑定对象的 ID。Id of the binding object to be retrieved.

返回

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.BindingCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions): Excel.BindingCollection;

参数

option
Excel.Interfaces.BindingCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions

返回

注解

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

load(option?: string | string[]): Excel.BindingCollection-Where 选项是一个逗号分隔的字符串或一个字符串数组, 用于指定要加载的属性。load(option?: string | string[]): Excel.BindingCollection - 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.BindingCollection-Where 选项。选择是一个用逗号分隔的字符串, 用于指定要加载的属性和选项。 expand 是一个用逗号分隔的字符串, 用于指定要加载的导航属性。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-仅适用于集合类型。load(option?: { select?: string; expand?: string; top?: number; skip?: number }): Excel.BindingCollection - 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.BindingCollection;

参数

propertyNames
string | string[]

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

返回

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

load(option)

load(option?: OfficeExtension.LoadOption): Excel.BindingCollection;

参数

返回

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.BindingCollection对象是 API 对象, 但toJSON方法返回一个无格式的 JavaScript 对象 (类型为Excel.Interfaces.BindingCollectionData, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.BindingCollection object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.BindingCollectionData), 其中包含集合的项中所有加载属性的浅表副本的 "items" 数组。) that contains an "items" array with shallow copies of any loaded properties from the collection's items.

toJSON(): Excel.Interfaces.BindingCollectionData;

返回