Excel.TableColumnCollection class

Represents a collection of all the columns that are part of the table.

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 columns in the table.

items

Gets the loaded child items in this collection.

Methods

add(index, values, name)

Adds a new column to the table.

addAsJson(index, values, name)

Adds a new column to the table. Unlike add(), addAsJson() takes any type of cell value, such as image or entity data types.

getCount()

Gets the number of columns in the table.

getItem(key)

Gets a column object by name or ID.

getItemAt(index)

Gets a column based on its position in the collection.

getItemOrNullObject(key)

Gets a column object by name or ID. If the column 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.TableColumnCollection object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.TableColumnCollectionData) that contains an "items" array with shallow copies of any loaded properties from the collection's items.

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 columns in the table.

readonly count: number;

Property Value

number

Remarks

[ API set: ExcelApi 1.1 ]

items

Gets the loaded child items in this collection.

readonly items: Excel.TableColumn[];

Property Value

Method Details

add(index, values, name)

Adds a new column to the table.

add(index?: number, values?: Array<Array<boolean | string | number>> | boolean | string | number, name?: string): Excel.TableColumn;

Parameters

index

number

Optional. Specifies the relative position of the new column. If null or -1, the addition happens at the end. Columns with a higher index will be shifted to the side. Zero-indexed.

values

Array<Array<boolean | string | number>> | boolean | string | number

Optional. A 2D array of unformatted values of the table column.

name

string

Optional. Specifies the name of the new column. If null, the default name will be used.

Returns

Remarks

[ API set: ExcelApi 1.1 requires an index smaller than the total column count; 1.4 allows index to be optional (null or -1) and will append a column at the end; 1.4 allows name parameter at creation time. ]

Examples

await Excel.run(async (context) => { 
    const tables = context.workbook.tables;
    const values = [["Sample"], ["Values"], ["For"], ["New"], ["Column"]];
    const column = tables.getItem("Table1").columns.add(null, values);
    column.load('name');
    await context.sync();
    
    console.log(column.name);
});

addAsJson(index, values, name)

Adds a new column to the table. Unlike add(), addAsJson() takes any type of cell value, such as image or entity data types.

addAsJson(index?: number, values?: CellValue[][], name?: string): Excel.TableColumn;

Parameters

index

number

Optional. Specifies the relative position of the new column. If null or -1, the addition happens at the end. Columns with a higher index will be shifted to the side. Zero-indexed.

values

Excel.CellValue[][]

Optional. A 2D array of cell values of the table column.

name

string

Optional. Specifies the name of the new column. If null, the default name will be used.

Returns

Remarks

[ API set: ExcelApi 1.16 ]

getCount()

Gets the number of columns in the table.

getCount(): OfficeExtension.ClientResult<number>;

Returns

Remarks

[ API set: ExcelApi 1.4 ]

getItem(key)

Gets a column object by name or ID.

getItem(key: number | string): Excel.TableColumn;

Parameters

key

number | string

Column name or ID.

Returns

Remarks

[ API set: ExcelApi 1.1 ]

Examples

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

getItemAt(index)

Gets a column based on its position in the collection.

getItemAt(index: number): Excel.TableColumn;

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 tableColumn = context.workbook.tables.getItem['Table1'].columns.getItemAt(0);
    tableColumn.load('name');
    await context.sync();
    console.log(tableColumn.name);
});

getItemOrNullObject(key)

Gets a column object by name or ID. If the column 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: number | string): Excel.TableColumn;

Parameters

key

number | string

Column name or ID.

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

Parameters

options

Excel.Interfaces.TableColumnCollectionLoadOptions & 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.TableColumnCollection;

Parameters

propertyNames

string | string[]

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

Returns

Examples

await Excel.run(async (context) => { 
    const tableColumns = context.workbook.tables.getItem('Table1').columns;
    tableColumns.load('items');
    await context.sync();
    
    console.log("tableColumns Count: " + tableColumns.count);
    for (let i = 0; i < tableColumns.items.length; i++) {
        console.log(tableColumns.items[i].name);
    }
});

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

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

toJSON(): Excel.Interfaces.TableColumnCollectionData;

Returns