OneNote.Table class

Represents a table in a OneNote page.

Extends

Remarks

[ API set: OneNoteApi 1.1 ]

Properties

borderVisible

Gets or sets whether the borders are visible or not. True if they are visible, false if they are hidden.

columnCount

Gets the number of columns in the table.

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

id

Gets the ID of the table.

paragraph

Gets the Paragraph object that contains the Table object.

rowCount

Gets the number of rows in the table.

rows

Gets all of the table rows.

Methods

appendColumn(values)

Adds a column to the end of the table. Values, if specified, are set in the new column. Otherwise the column is empty.

appendRow(values)

Adds a row to the end of the table. Values, if specified, are set in the new row. Otherwise the row is empty.

clear()

Clears the contents of the table.

getCell(rowIndex, cellIndex)

Gets the table cell at a specified row and column.

insertColumn(index, values)

Inserts a column at the given index in the table. Values, if specified, are set in the new column. Otherwise the column is empty.

insertRow(index, values)

Inserts a row at the given index in the table. Values, if specified, are set in the new row. Otherwise the row is empty.

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.

set(properties, options)

Sets multiple properties of an object at the same time. You can pass either a plain object with the appropriate properties, or another API object of the same type.

set(properties)

Sets multiple properties on the object at the same time, based on an existing loaded object.

setShadingColor(colorCode)

Sets the shading color of all cells in the table. The color code to set the cells to.

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's passed to it.) Whereas the original OneNote.Table object is an API object, the toJSON method returns a plain JavaScript object (typed as OneNote.Interfaces.TableData) that contains shallow copies of any loaded child properties from the original object.

track()

Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you're using this object across .sync calls and outside the sequential execution of a ".run" batch, and get an "InvalidObjectPath" error when setting a property or invoking a method on the object, you need to add the object to the tracked object collection when the object was first created.

untrack()

Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You'll need to call context.sync() before the memory release takes effect.

Property Details

borderVisible

Gets or sets whether the borders are visible or not. True if they are visible, false if they are hidden.

borderVisible: boolean;

Property Value

boolean

Remarks

[ API set: OneNoteApi 1.1 ]

columnCount

Gets the number of columns in the table.

readonly columnCount: number;

Property Value

number

Remarks

[ API set: OneNoteApi 1.1 ]

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

id

Gets the ID of the table.

readonly id: string;

Property Value

string

Remarks

[ API set: OneNoteApi 1.1 ]

paragraph

Gets the Paragraph object that contains the Table object.

readonly paragraph: OneNote.Paragraph;

Property Value

Remarks

[ API set: OneNoteApi 1.1 ]

rowCount

Gets the number of rows in the table.

readonly rowCount: number;

Property Value

number

Remarks

[ API set: OneNoteApi 1.1 ]

rows

Gets all of the table rows.

readonly rows: OneNote.TableRowCollection;

Property Value

Remarks

[ API set: OneNoteApi 1.1 ]

Method Details

appendColumn(values)

Adds a column to the end of the table. Values, if specified, are set in the new column. Otherwise the column is empty.

appendColumn(values?: string[]): void;

Parameters

values

string[]

Optional. Strings to insert in the new column, specified as an array. Must not have more values than rows in the table.

Returns

void

Remarks

[ API set: OneNoteApi 1.1 ]

Examples

await OneNote.run(async (context) => {
    const app = context.application;
    const outline = app.getActiveOutline();
    
    // Queue a command to load outline.paragraphs and their types.
    context.load(outline, "paragraphs, paragraphs/type");
    
    // Run the queued commands, and return a promise to indicate task completion.
    await context.sync();
    const paragraphs = outline.paragraphs;
    
    // For each table, append a column.
    for (let i = 0; i < paragraphs.items.length; i++) {
        const paragraph = paragraphs.items[i];
        if (paragraph.type == "Table") {
            const table = paragraph.table;
            table.appendColumn(["cell0", "cell1"]);
        }
    }
    await context.sync();
});

appendRow(values)

Adds a row to the end of the table. Values, if specified, are set in the new row. Otherwise the row is empty.

appendRow(values?: string[]): OneNote.TableRow;

Parameters

values

string[]

Optional. Strings to insert in the new row, specified as an array. Must not have more values than columns in the table.

Returns

Remarks

[ API set: OneNoteApi 1.1 ]

Examples

await OneNote.run(async (context) => {
    const app = context.application;
    const outline = app.getActiveOutline();
    
    // Queue a command to load outline.paragraphs and their types.
    context.load(outline, "paragraphs, paragraphs/type");
    
    // Run the queued commands, and return a promise to indicate task completion.
    await context.sync();

    const paragraphs = outline.paragraphs;
    
    // For each table, append a column.
    for (let i = 0; i < paragraphs.items.length; i++) {
        const paragraph = paragraphs.items[i];
        if (paragraph.type == "Table") {
            const table = paragraph.table;
            const row = table.appendRow(["cell0", "cell1"]);
        }
    }
    await context.sync();
});

clear()

Clears the contents of the table.

clear(): void;

Returns

void

Remarks

[ API set: OneNoteApi 1.1 ]

getCell(rowIndex, cellIndex)

Gets the table cell at a specified row and column.

getCell(rowIndex: number, cellIndex: number): OneNote.TableCell;

Parameters

rowIndex

number

The index of the row.

cellIndex

number

The index of the cell in the row.

Returns

Remarks

[ API set: OneNoteApi 1.1 ]

Examples

await OneNote.run(async (context) => {
    const app = context.application;
    const outline = app.getActiveOutline();
    
    // Queue a command to load outline.paragraphs and their types.
    context.load(outline, "paragraphs, paragraphs/type");
    
    // Run the queued commands, and return a promise to indicate task completion.
    await context.sync();

    const paragraphs = outline.paragraphs;
    
    // For each table, get a cell in the second row and third column.
    for (let i = 0; i < paragraphs.items.length; i++) {
        const paragraph = paragraphs.items[i];
        if (paragraph.type == "Table") {
            const table = paragraph.table;
            const cell = table.getCell(2 /*Row Index*/, 3 /*Column Index*/);
        }
    }
    await context.sync();
});

insertColumn(index, values)

Inserts a column at the given index in the table. Values, if specified, are set in the new column. Otherwise the column is empty.

insertColumn(index: number, values?: string[]): void;

Parameters

index

number

Index where the column will be inserted in the table.

values

string[]

Optional. Strings to insert in the new column, specified as an array. Must not have more values than rows in the table.

Returns

void

Remarks

[ API set: OneNoteApi 1.1 ]

Examples

await OneNote.run(async (context) => {
    const app = context.application;
    const outline = app.getActiveOutline();
    
    // Queue a command to load outline.paragraphs and their types.
    context.load(outline, "paragraphs, paragraphs/type");
    
    // Run the queued commands, and return a promise to indicate task completion.
    await context.sync();

    const paragraphs = outline.paragraphs;
    
    // For each table, insert a column at index two.
    for (let i = 0; i < paragraphs.items.length; i++) {
        const paragraph = paragraphs.items[i];
        if (paragraph.type == "Table") {
            const table = paragraph.table;
            table.insertColumn(2, ["cell0", "cell1"]);
        }
    }
    await context.sync();
});

insertRow(index, values)

Inserts a row at the given index in the table. Values, if specified, are set in the new row. Otherwise the row is empty.

insertRow(index: number, values?: string[]): OneNote.TableRow;

Parameters

index

number

Index where the row will be inserted in the table.

values

string[]

Optional. Strings to insert in the new row, specified as an array. Must not have more values than columns in the table.

Returns

Remarks

[ API set: OneNoteApi 1.1 ]

Examples

await OneNote.run(async (context) => {
    const app = context.application;
    const outline = app.getActiveOutline();
    
    // Queue a command to load outline.paragraphs and their types.
    context.load(outline, "paragraphs, paragraphs/type");
    
    // Run the queued commands, and return a promise to indicate task completion.
    await context.sync()

    const paragraphs = outline.paragraphs;
    
    // For each table, insert a row at index two.
    for (let i = 0; i < paragraphs.items.length; i++) {
        const paragraph = paragraphs.items[i];
        if (paragraph.type == "Table") {
            const table = paragraph.table;
            const row = table.insertRow(2, ["cell0", "cell1"]);
        }
    }
    await context.sync();
});

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?: OneNote.Interfaces.TableLoadOptions): OneNote.Table;

Parameters

options
OneNote.Interfaces.TableLoadOptions

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[]): OneNote.Table;

Parameters

propertyNames

string | string[]

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

Returns

Examples

await OneNote.run(async (context) => {
    const app = context.application;
    const outline = app.getActiveOutline();
    
    // Queue a command to load outline.paragraphs and their types.
    context.load(outline, "paragraphs/type");
    
    // Run the queued commands, and return a promise to indicate task completion.
    await context.sync();

    const paragraphs = outline.paragraphs;
    
    // For each table, log properties.
    for (let i = 0; i < paragraphs.items.length; i++) {
        const paragraph = paragraphs.items[i];
        if (paragraph.type == "Table") {
            const table = paragraph.table;
            context.load(table);
            await context.sync();

            console.log("Table Id: " + table.id);
            console.log("Row Count: " + table.rowCount);
            console.log("Column Count: " + table.columnCount);
            await context.sync();
        }
    }
});

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?: {
            select?: string;
            expand?: string;
        }): OneNote.Table;

Parameters

propertyNamesAndPaths

{ select?: string; expand?: string; }

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

set(properties, options)

Sets multiple properties of an object at the same time. You can pass either a plain object with the appropriate properties, or another API object of the same type.

set(properties: Interfaces.TableUpdateData, options?: OfficeExtension.UpdateOptions): void;

Parameters

properties
OneNote.Interfaces.TableUpdateData

A JavaScript object with properties that are structured isomorphically to the properties of the object on which the method is called.

options
OfficeExtension.UpdateOptions

Provides an option to suppress errors if the properties object tries to set any read-only properties.

Returns

void

set(properties)

Sets multiple properties on the object at the same time, based on an existing loaded object.

set(properties: OneNote.Table): void;

Parameters

properties
OneNote.Table

Returns

void

setShadingColor(colorCode)

Sets the shading color of all cells in the table. The color code to set the cells to.

setShadingColor(colorCode: string): void;

Parameters

colorCode

string

Returns

void

Remarks

[ API set: OneNoteApi 1.1 ]

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's passed to it.) Whereas the original OneNote.Table object is an API object, the toJSON method returns a plain JavaScript object (typed as OneNote.Interfaces.TableData) that contains shallow copies of any loaded child properties from the original object.

toJSON(): OneNote.Interfaces.TableData;

Returns

track()

Track the object for automatic adjustment based on surrounding changes in the document. This call is a shorthand for context.trackedObjects.add(thisObject). If you're using this object across .sync calls and outside the sequential execution of a ".run" batch, and get an "InvalidObjectPath" error when setting a property or invoking a method on the object, you need to add the object to the tracked object collection when the object was first created.

track(): OneNote.Table;

Returns

untrack()

Release the memory associated with this object, if it has previously been tracked. This call is shorthand for context.trackedObjects.remove(thisObject). Having many tracked objects slows down the host application, so please remember to free any objects you add, once you're done using them. You'll need to call context.sync() before the memory release takes effect.

untrack(): OneNote.Table;

Returns