TableCell Object (JavaScript API for OneNote)

Applies to: OneNote Online

Represents a cell in a OneNote table.

To provide feedback on this API, you can file an issue in GitHub.

Properties

Property Type Description
cellIndex int Gets the index of the cell in its row. Read-only.
id string Gets the ID of the cell. Read-only.
rowIndex int Gets the index of the cell's row in the table. Read-only.
shadingColor string Gets and sets the shading color of the cell.

See property access examples.

Relationships

Relationship Type Description
paragraphs ParagraphCollection Gets the collection of Paragraph objects in the table cell. Read-only.
parentRow TableRow Gets the parent row of the cell. Read-only.

Methods

Method Return Type Description
appendHtml(html: string) void Adds the specified HTML to the bottom of the table cell.
appendImage(base64EncodedImage: string, width: double, height: double) Image Adds the specified image to the table cell.
appendRichText(paragraphText: string) RichText Adds the specified text to the table cell.
appendTable(rowCount: number, columnCount: number, values: string) Table Adds a table with the specified number of rows and columns to the table cell.
clear() void Clears the contents of the cell.
load(param: object) void Fills the proxy object created in the JavaScript layer with property and object values specified in the parameter.

Method details

appendHtml(html: string)

Adds the specified HTML to the bottom of the table cell.

Syntax

tableCellObject.appendHtml(html);

Parameters

Parameter Type Description
html string The HTML string to append. See supported HTML for the OneNote add-ins JavaScript API.

Returns

Void

Examples

OneNote.run(function(ctx) {
    var app = ctx.application;
    var outline = app.getActiveOutline();
    
    // Queue a command to load outline.paragraphs and their types.
    ctx.load(outline, "paragraphs, paragraphs/type");
    
    // Run the queued commands, and return a promise to indicate task completion.
    return ctx.sync().then(function () {
        var paragraphs = outline.paragraphs;
        
        // for each table, get a table cell at row one and column two and add "Hello".
        for (var i = 0; i < paragraphs.items.length; i++) {
            var paragraph = paragraphs.items[i];
            if (paragraph.type == "Table") {
                var table = paragraph.table;
                var cell = table.getCell(1 /*Row Index*/, 2 /*Column Index*/);
                cell.appendHtml("<p>Hello</p>");
            }
        }
        return ctx.sync();
    })
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});


appendImage(base64EncodedImage: string, width: double, height: double)

Adds the specified image to the table cell.

Syntax

tableCellObject.appendImage(base64EncodedImage, width, height);

Parameters

Parameter Type Description
base64EncodedImage string HTML string to append.
width double Optional. Width in the unit of Points. The default value is null, and image width will be respected.
height double Optional. Height in the unit of Points. The default value is null, and image height will be respected.

Returns

Image


appendRichText(paragraphText: string)

Adds the specified text to the table cell.

Syntax

tableCellObject.appendRichText(paragraphText);

Parameters

Parameter Type Description
paragraphText string HTML string to append.

Returns

RichText

Examples

OneNote.run(function(ctx) {
    var app = ctx.application;
    var outline = app.getActiveOutline();
    var appendedRichText = null;
    
    // Queue a command to load outline.paragraphs and their types.
    ctx.load(outline, "paragraphs, paragraphs/type");
    
    // Run the queued commands, and return a promise to indicate task completion.
    return ctx.sync().then(function () {
        var paragraphs = outline.paragraphs;
        
        // for each table, get a table cell at row one and column two and add "Hello".
        for (var i = 0; i < paragraphs.items.length; i++) {
            var paragraph = paragraphs.items[i];
            if (paragraph.type == "Table") {
                var table = paragraph.table;
                var cell = table.getCell(1 /*Row Index*/, 2 /*Column Index*/);
                appendedRichText = cell.appendRichText("Hello");
            }
        }
        return ctx.sync();
    })
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});


appendTable(rowCount: number, columnCount: number, values: string)

Adds a table with the specified number of rows and columns to the table cell.

Syntax

tableCellObject.appendTable(rowCount, columnCount, values);

Parameters

Parameter Type Description
rowCount number Required. The number of rows in the table.
columnCount number Required. The number of columns in the table.
values string Optional 2D array. Cells are filled if the corresponding strings are specified in the array.

Returns

Table


clear()

Clears the contents of the cell.

Syntax

tableCellObject.clear();

Parameters

None

Returns

Void


load(param: object)

Fills the proxy object created in the JavaScript layer with property and object values specified in the parameter.

Syntax

object.load(param);

Parameters

Parameter Type Description
param object Optional. Accepts parameter and relationship names as a delimited string or an array. Or, provide loadOption object.

Returns

Void


Property access examples

id, cellIndex, rowIndex

OneNote.run(function(ctx) {
    var app = ctx.application;
    var outline = app.getActiveOutline();
    
    // Queue a command to load outline.paragraphs and their types.
    ctx.load(outline, "paragraphs, paragraphs/type");
    
    // Run the queued commands, and return a promise to indicate task completion.
    return ctx.sync().then(function () {
        var paragraphs = outline.paragraphs;
        
        // for each table, get a table cell at row one and column two.
        for (var i = 0; i < paragraphs.items.length; i++) {
            var paragraph = paragraphs.items[i];
            if (paragraph.type == "Table") {
                var table = paragraph.table;
                var cell = table.getCell(1 /*Row Index*/, 2 /*Column Index*/);
                
                // Queue a command to load the table cell.
                ctx.load(cell);
                ctx.sync().then(function() {
                    console.log("Cell Id: " + cell.id);
                    console.log("Cell Index: " + cell.cellIndex);
                    console.log("Cell's Row Index: " + cell.rowIndex);
                });
            }
        }
        return ctx.sync();
    })
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});


parentTable, cells

ParentTable, ParentRow, Paragraphs
OneNote.run(function(ctx) {
    var app = ctx.application;
    var outline = app.getActiveOutline();
    
    // Queue a command to load outline.paragraphs and their types.
    ctx.load(outline, "paragraphs, paragraphs/type");
    
    // Run the queued commands, and return a promise to indicate task completion.
    return ctx.sync().then(function () {
        var paragraphs = outline.paragraphs;
        
        // for each table, get a table cell at row one and column two.
        for (var i = 0; i < paragraphs.items.length; i++) {
            var paragraph = paragraphs.items[i];
            if (paragraph.type == "Table") {
                var table = paragraph.table;
                var cell = table.getCell(1 /*Row Index*/, 2 /*Column Index*/);
                
                // Queue a command to load parentTable, parentRow and paragraphs of the table cell.
                ctx.load(cell, "parentTable, parentRow, paragraphs");
                
                ctx.sync().then(function() {
                    console.log("Parent Table Id: " + cell.parentTable.id);
                    console.log("Parent Row Id: " + cell.parentRow.id);
                    var paragraphs = cell.paragraphs;
                    
                    for (var i = 0; i < paragraphs.items.length; i++) {
                        console.log("Paragraph Id: " + paragraphs.items[i].id);
                    }
                });
            }
        }
        return ctx.sync();
    })
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});


div> get a table cell at row one and column two. for (var i = 0; i < paragraphs.items.length; i++) { var paragraph = paragraphs.items[i]; if (paragraph.type == "Table") { var table = paragraph.table; var cell = table.getCell(1 /*Row Index*/, 2 /*Column Index*/); // Queue a command to load parentTable, parentRow and paragraphs of the table cell. ctx.load(cell, "parentTable, parentRow, paragraphs"); ctx.sync().then(function() { console.log("Parent Table Id: " + cell.parentTable.id); console.log("Parent Row Id: " + cell.parentRow.id); var paragraphs = cell.paragraphs; for (var i = 0; i < paragraphs.items.length; i++) { console.log("Paragraph Id: " + paragraphs.items[i].id); } }); } } return ctx.sync(); }) }) .catch(function(error) { console.log("Error: " + error); if (error instanceof OfficeExtension.Error) { console.log("Debug info: " + JSON.stringify(error.debugInfo)); } });