Excel.TableColumnCollection class

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

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

[ API set: ExcelApi 1.1 ]

items

Gets the loaded child items in this collection.

Methods

add(index, values, name)

Adds a new column to the table.

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

getCount()

Gets the number of columns in the table.

[ API set: ExcelApi 1.4 ]

getItem(key)

Gets a column object by Name or ID.

[ API set: ExcelApi 1.1 ]

getItemAt(index)

Gets a column based on its position in the collection.

[ API set: ExcelApi 1.1 ]

getItemOrNullObject(key)

Gets a column object by Name or ID. If the column 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.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. 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.TableColumn[];

Property Value

Method Details

add(index, values, name)

Adds a new column to the table.

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

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 2-dimensional 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

Examples

Excel.run(function (ctx) { 
    var tables = ctx.workbook.tables;
    var values = [["Sample"], ["Values"], ["For"], ["New"], ["Column"]];
    var column = tables.getItem("Table1").columns.add(null, values);
    column.load('name');
    return ctx.sync().then(function() {
        console.log(column.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 columns in the table.

[ API set: ExcelApi 1.4 ]

getCount(): OfficeExtension.ClientResult<number>;

Returns

OfficeExtension.ClientResult<number>

getItem(key)

Gets a column object by Name or ID.

[ API set: ExcelApi 1.1 ]

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

Parameters

key
number | string

Column Name or ID.

Returns

Examples

Excel.run(function (ctx) { 
    var tablecolumn = ctx.workbook.tables.getItem('Table1').columns.getItem(0);
    tablecolumn.load('name');
    return ctx.sync().then(function() {
            console.log(tablecolumn.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 column based on its position in the collection.

[ API set: ExcelApi 1.1 ]

getItemAt(index: number): Excel.TableColumn;

Parameters

index
number

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

Returns

Examples

Excel.run(function (ctx) { 
    var tablecolumn = ctx.workbook.tables.getItem['Table1'].columns.getItemAt(0);
    tablecolumn.load('name');
    return ctx.sync().then(function() {
            console.log(tablecolumn.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 column object by Name or ID. If the column does not exist, will return a null object.

[ API set: ExcelApi 1.4 ]

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

Parameters

key
number | string

Column Name or ID.

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

Excel.run(function (ctx) { 
    var tablecolumns = ctx.workbook.tables.getItem('Table1').columns;
    tablecolumns.load('items');
    return ctx.sync().then(function() {
        console.log("tablecolumns Count: " + tablecolumns.count);
        for (var i = 0; i < tablecolumns.items.length; i++) {
            console.log(tablecolumns.items[i].name);
        }
    });
}).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.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