Excel.TableCollection class

表示属于工作簿或工作表(具有取决于限定到的范围)的所有表的集合。Represents a collection of all the tables that are part of the workbook or worksheet, depending on how it was reached.

[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 tables in the workbook. 只读。Read-only.

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

items

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

方法

add(address, hasHeaders)

新建表。范围对象或源地址决定了在哪个工作表下添加表。如果无法添加表(例如,由于地址无效,或者表与另一个表重叠),则会引发错误。Create a new table. The range object or source address determines the worksheet under which the table will be added. If the table cannot be added (e.g., because the address is invalid, or the table would overlap with another table), an error will be thrown.

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

getCount()

获取集合中的表数量。Gets the number of tables in the collection.

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

getItem(key)

按名称或 ID 获取表。Gets a table by Name or ID.

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

getItemAt(index)

根据其在集合中的位置获取表。Gets a table based on its position in the collection.

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

getItemOrNullObject(key)

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

事件

onAdded

在工作簿中添加新表格时发生。Occurs when new table is added in a workbook.

[API 集: ExcelApi 1.9][ API set: ExcelApi 1.9 ]

onChanged

当工作簿中的任何表或工作表上的数据发生更改时发生。Occurs when data changes on any table in a workbook, or a worksheet.

[API 集: ExcelApi 1.7][ API set: ExcelApi 1.7 ]

onDeleted

在工作簿中删除指定的表格时发生。Occurs when the specified table is deleted in a workbook.

[API 集: ExcelApi 1.9][ API set: ExcelApi 1.9 ]

onFiltered

在工作簿或工作表中的任何表格上应用筛选器时发生。Occurs when filter is applied on any table in a workbook, or a worksheet.

[API 集: EXCELAPI BETA (仅限预览)][ API set: ExcelApi BETA (PREVIEW ONLY) ]

属性详细信息

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 tables in the workbook. 只读。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.Table[];

属性值

Excel.Table[]

方法详细信息

add(address, hasHeaders)

新建表。范围对象或源地址决定了在哪个工作表下添加表。如果无法添加表(例如,由于地址无效,或者表与另一个表重叠),则会引发错误。Create a new table. The range object or source address determines the worksheet under which the table will be added. If the table cannot be added (e.g., because the address is invalid, or the table would overlap with another table), an error will be thrown.

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

add(address: Range | string, hasHeaders: boolean): Excel.Table;

参数

address
Range | string

Range 对象或表示数据源的区域的字符串地址或名称。A Range object, or a string address or name of the range representing the data source. 如果地址不包含工作表名称,将使用当前活动的工作表。If the address does not contain a sheet name, the currently-active sheet is used. [Api 集: ExcelApi 1.1/1.3。[Api set: ExcelApi 1.1 / 1.3. 在 ExcelApi 1.3 之前, 此参数必须是字符串。Prior to ExcelApi 1.3, this parameter must be a string. 从 Excel Api 1.3 开始, 此参数可以是一个 Range 对象或一个字符串。]Starting with Excel Api 1.3, this parameter may be a Range object or a string.]

hasHeaders
boolean

指示导入的数据是否具有列标签的布尔值。如果源不包含标头(即,当此属性设置为 false 时),Excel 将自动生成标头,数据将向下移动一行。Boolean value that indicates whether the data being imported has column labels. If the source does not contain headers (i.e,. when this property set to false), Excel will automatically generate header shifting the data down by one row.

返回

示例Examples

Excel.run(function (ctx) { 
    var table = ctx.workbook.tables.add('Sheet1!A1:E7', true);
    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));
    }
});

getCount()

获取集合中的表数量。Gets the number of tables in the collection.

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

getCount(): OfficeExtension.ClientResult<number>;

返回

OfficeExtension.ClientResult<number>

getItem(key)

按名称或 ID 获取表。Gets a table by Name or ID.

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

getItem(key: string): Excel.Table;

参数

key
string

要检索的表的名称或 ID。Name or ID of the table to be retrieved.

返回

示例Examples

Excel.run(function (ctx) { 
    var tableName = 'Table1';
    var table = ctx.workbook.tables.getItem(tableName);
    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));
    }
});

getItemAt(index)

根据其在集合中的位置获取表。Gets a table based on its position in the collection.

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

getItemAt(index: number): Excel.Table;

参数

index
number

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

返回

示例Examples

Excel.run(function (ctx) { 
    var table = ctx.workbook.tables.getItemAt(0);
    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));
    }
});

getItemOrNullObject(key)

按名称或 ID 获取表。Gets a table by Name or ID. 如果没有表,将返回 NULL 对象。If the table does not exist, will return a null object.

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

getItemOrNullObject(key: string): Excel.Table;

参数

key
string

要检索的表的名称或 ID。Name or ID of the table 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.TableCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions): Excel.TableCollection;

参数

option
Excel.Interfaces.TableCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions

返回

注解

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

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

参数

propertyNames
string | string[]

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

返回

示例Examples

Excel.run(function (ctx) { 
    var tables = ctx.workbook.tables;
    tables.load();
    return ctx.sync().then(function() {
        console.log("tables Count: " + tables.count);
        for (var i = 0; i < tables.items.length; i++)
        {
            console.log(tables.items[i].name);
        }
    });
}).catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
// Get the number of tables
Excel.run(function (ctx) { 
    var tables = ctx.workbook.tables;
    tables.load('count');
    return ctx.sync().then(function() {
        console.log(tables.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.TableCollection;

参数

返回

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

toJSON(): Excel.Interfaces.TableCollectionData;

返回

事件详细信息

onAdded

在工作簿中添加新表格时发生。Occurs when new table is added in a workbook.

[API 集: ExcelApi 1.9][ API set: ExcelApi 1.9 ]

readonly onAdded: OfficeExtension.EventHandlers<Excel.TableAddedEventArgs>;

返回

OfficeExtension.EventHandlers<Excel.TableAddedEventArgs>

onChanged

当工作簿中的任何表或工作表上的数据发生更改时发生。Occurs when data changes on any table in a workbook, or a worksheet.

[API 集: ExcelApi 1.7][ API set: ExcelApi 1.7 ]

readonly onChanged: OfficeExtension.EventHandlers<Excel.TableChangedEventArgs>;

返回

OfficeExtension.EventHandlers<Excel.TableChangedEventArgs>

示例Examples

await Excel.run(async (context) => {
    let tables = context.workbook.tables;
    tables.onChanged.add(onChange);

    await context.sync();
    console.log("A handler has been registered for the table collection onChanged event");
});

onDeleted

在工作簿中删除指定的表格时发生。Occurs when the specified table is deleted in a workbook.

[API 集: ExcelApi 1.9][ API set: ExcelApi 1.9 ]

readonly onDeleted: OfficeExtension.EventHandlers<Excel.TableDeletedEventArgs>;

返回

OfficeExtension.EventHandlers<Excel.TableDeletedEventArgs>

onFiltered

备注

此 API 以预览状态提供给开发者,可能根据我们收到的反馈更改。 请勿在生产环境中使用此 API。

在工作簿或工作表中的任何表格上应用筛选器时发生。Occurs when filter is applied on any table in a workbook, or a worksheet.

[API 集: EXCELAPI BETA (仅限预览)][ API set: ExcelApi BETA (PREVIEW ONLY) ]

readonly onFiltered: OfficeExtension.EventHandlers<Excel.TableFilteredEventArgs>;

返回

OfficeExtension.EventHandlers<Excel.TableFilteredEventArgs>