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

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

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

toJSON()

Events

onChanged

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

[ API set: ExcelApi 1.7 ]

Property Details

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
Excel.Table[]

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

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

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

load(option?: string | string[]): Excel.TableCollection;
Parameters
option
string | string[]

A comma-delimited string or an array of strings that specify the properties to load.

Returns
Remarks

In addition to this signature, this method has the following signatures:

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 - Only available on collection types. It is similar to the preceding signature. Option.top specifies the maximum number of collection items that can be included in the result. Option.skip specifies the number of items that are to be skipped and not included in the result. If option.top is specified, the result set will start after skipping the specified number of items.

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

toJSON()

toJSON(): Excel.Interfaces.TableCollectionData;
Returns

Event Details

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

    OfficeHelpers.UI.notify("A handler has been registered for the table collection onChanged event",
        "Try changing cell values in the tables, and watch the console output.");
});