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

count

Returns the number of tables in the workbook. Read-only.

[ API set: ExcelApi 1.1 ]

items

Gets the loaded child items in this collection.

Methods

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 set: ExcelApi 1.1 ]

getCount()

Gets the number of tables in the collection.

[ API set: ExcelApi 1.4 ]

getItem(key)

Gets a table by Name or ID.

[ API set: ExcelApi 1.1 ]

getItemAt(index)

Gets a table based on its position in the collection.

[ API set: ExcelApi 1.1 ]

getItemOrNullObject(key)

Gets a table by Name or ID. If the table does not exist, will return a null object.

[ API set: ExcelApi 1.4 ]

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.TableCollection object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.TableCollectionData) that contains an "items" array with shallow copies of any loaded properties from the collection's items.

Events

onAdded

Occurs when new table is added in a workbook.

[ API set: ExcelApi 1.9 ]

onChanged

Occurs when data changes on any table in a workbook, or a worksheet.

[ API set: ExcelApi 1.7 ]

onDeleted

Occurs when the specified table is deleted in a workbook.

[ API set: ExcelApi 1.9 ]

onFiltered

Occurs when filter is applied on any table in a workbook, or a worksheet.

[ API set: ExcelApi BETA (PREVIEW ONLY) ]

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

count

Returns the number of tables in the workbook. Read-only.

[ API set: ExcelApi 1.1 ]

readonly count: number;

Property Value

number

items

Gets the loaded child items in this collection.

readonly items: Excel.Table[];

Property Value

Method Details

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 set: ExcelApi 1.1 ]

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

Parameters

address
Range | string

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 set: ExcelApi 1.1 / 1.3. Prior to ExcelApi 1.3, this parameter must be a string. Starting with Excel Api 1.3, this parameter may be a Range object or a string.]

hasHeaders
boolean

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.

Returns

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 set: ExcelApi 1.4 ]

getCount(): OfficeExtension.ClientResult<number>;

Returns

OfficeExtension.ClientResult<number>

getItem(key)

Gets a table by Name or ID.

[ API set: ExcelApi 1.1 ]

getItem(key: string): Excel.Table;

Parameters

key
string

Name or ID of the table to be retrieved.

Returns

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 set: ExcelApi 1.1 ]

getItemAt(index: number): Excel.Table;

Parameters

index
number

Index value of the object to be retrieved. Zero-indexed.

Returns

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)

Gets a table by Name or ID. If the table does not exist, will return a null object.

[ API set: ExcelApi 1.4 ]

getItemOrNullObject(key: string): Excel.Table;

Parameters

key
string

Name or ID of the table to be retrieved.

Returns

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

Parameters

options
Excel.Interfaces.TableCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions

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

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 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(propertyNamesAndPaths)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNamesAndPaths?: OfficeExtension.LoadOption): Excel.TableCollection;

Parameters

propertyNamesAndPaths
OfficeExtension.LoadOption

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.TableCollection object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.TableCollectionData) that contains an "items" array with shallow copies of any loaded properties from the collection's items.

toJSON(): Excel.Interfaces.TableCollectionData;

Returns

Event Details

onAdded

Occurs when new table is added in a workbook.

[ API set: ExcelApi 1.9 ]

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

Returns

OfficeExtension.EventHandlers<Excel.TableAddedEventArgs>

onChanged

Occurs when data changes on any table in a workbook, or a worksheet.

[ API set: ExcelApi 1.7 ]

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

Returns

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 set: ExcelApi 1.9 ]

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

Returns

OfficeExtension.EventHandlers<Excel.TableDeletedEventArgs>

onFiltered

Note

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

Occurs when filter is applied on any table in a workbook, or a worksheet.

[ API set: ExcelApi BETA (PREVIEW ONLY) ]

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

Returns

OfficeExtension.EventHandlers<Excel.TableFilteredEventArgs>