TableRowCollection Object (JavaScript API for Excel)

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

Properties

Property Type Description Req. Set
count int Returns the number of rows in the table. Read-only. 1.1
items TableRow[] A collection of tableRow objects. Read-only. 1.1

See property access examples.

Relationships

None

Methods

Method Return Type Description Req. Set
add(index: number, values: object) TableRow Adds one or more rows to the table. The return object will be the top of the newly added row(s). 1.1
getCount() int Gets the number of rows in the table. 1.4
getItemAt(index: number) TableRow Gets a row based on its position in the collection. 1.1

Method Details

add(index: number, values: object)

Adds one or more rows to the table. The return object will be the top of the newly added row(s).

Syntax

tableRowCollectionObject.add(index, values);

Parameters

Parameter Type Description
index number Optional. Specifies the relative position of the new row. If null or -1, the addition happens at the end. Any rows below the inserted row are shifted downwards. Zero-indexed.
values object Optional. A 2-dimensional array of unformatted values of the table row.

Returns

TableRow

Examples

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

Syntax

tableRowCollectionObject.getCount();

Parameters

None

Returns

int

getItemAt(index: number)

Gets a row based on its position in the collection.

Syntax

tableRowCollectionObject.getItemAt(index);

Parameters

Parameter Type Description
index number Index value of the object to be retrieved. Zero-indexed.

Returns

TableRow

Examples

Excel.run(function (ctx) { 
    var tablerow = ctx.workbook.tables.getItem('Table1').rows.getItemAt(0);
    tablerow.load('name');
    return ctx.sync().then(function() {
            console.log(tablerow.name);
    });
}).catch(function(error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
});

Property access examples

Excel.run(function (ctx) { 
    var tablerows = ctx.workbook.tables.getItem('Table1').rows;
    tablerows.load('items');
    return ctx.sync().then(function() {
        console.log("tablerows Count: " + tablerows.count);
        for (var i = 0; i < tablerows.items.length; i++)
        {
            console.log(tablerows.items[i].index);
        }
    });
}).catch(function(error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
});

results matching ""

    No results matching ""