Excel.TableCollection class

Represents a collection of all the tables that are part of the workbook or worksheet, depending on how it was reached.

Extends

Remarks

[ API set: ExcelApi 1.1 ]

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.

items

Gets the loaded child items in this collection.

Methods

add(address, hasHeaders)

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

getCount()

Gets the number of tables in the collection.

getItem(key)

Gets a table by name or ID.

getItemAt(index)

Gets a table based on its position in the collection.

getItemOrNullObject(key)

Gets a table by name or ID. If the table doesn't exist, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

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

onChanged

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

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.

readonly count: number;

Property Value

number

Remarks

[ API set: ExcelApi 1.1 ]

items

Gets the loaded child items in this collection.

readonly items: Excel.Table[];

Property Value

Method Details

add(address, hasHeaders)

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

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

Parameters

address

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

A 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 a header and shift the data down by one row.

Returns

Remarks

[ API set: ExcelApi 1.1 ]

Examples

await Excel.run(async (context) => { 
    const table = context.workbook.tables.add('Sheet1!A1:E7', true);
    table.load('name');
    await context.sync();
    
    console.log(table.name);
});

getCount()

Gets the number of tables in the collection.

getCount(): OfficeExtension.ClientResult<number>;

Returns

Remarks

[ API set: ExcelApi 1.4 ]

getItem(key)

Gets a table by name or ID.

getItem(key: string): Excel.Table;

Parameters

key

string

Name or ID of the table to be retrieved.

Returns

Remarks

[ API set: ExcelApi 1.1 ]

Examples

await Excel.run(async (context) => { 
    const tableName = 'Table1';
    const table = context.workbook.tables.getItem(tableName);
    table.load('name');
    await context.sync();
    
    console.log(table.name);
});

getItemAt(index)

Gets a table based on its position in the collection.

getItemAt(index: number): Excel.Table;

Parameters

index

number

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

Returns

Remarks

[ API set: ExcelApi 1.1 ]

Examples

await Excel.run(async (context) => { 
    const table = context.workbook.tables.getItemAt(0);
    table.load('name');
    await context.sync();
    
    console.log(table.name);
});

getItemOrNullObject(key)

Gets a table by name or ID. If the table doesn't exist, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

getItemOrNullObject(key: string): Excel.Table;

Parameters

key

string

Name or ID of the table to be retrieved.

Returns

Remarks

[ 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(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

// Get the number of tables.
await Excel.run(async (context) => { 
    const tables = context.workbook.tables;
    tables.load('count');
    await context.sync();
    
    console.log(tables.count);
});

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

onChanged

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

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

Event Type

Remarks

[ API set: ExcelApi 1.7 ]

Examples

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/30-events/events-tablecollection-changed.yaml

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