Search code examples
office-jsonenoteonenote-api

OneNote Add in: TableCell contents


The documentation for OneNote add-ins shows us how to get a table cell. In the code snippet below (minor modifications from the documentation's example), I load a table cell at position [0,0]. However, once I get the TableCell it is unclear on how I load its contents. How do I know what the interior of a TableCell contains? Is there someway to do

var cell = table.getCell(0,0);
cell.getContents();//Is this correct?

Or is this not the right way to think about the getting pattern because the internal contents are unknown?

Thanks!

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, append a column.
		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(0,0);
				//To do - how to get value inside?
			}
		}
		return ctx.sync();
	})
})
.catch(function(error) {
	console.log("Error: " + error);
	if (error instanceof OfficeExtension.Error) {
		console.log("Debug info: " + JSON.stringify(error.debugInfo));
	}
});


Solution

  • See official documentation for table cell: https://github.com/OfficeDev/office-js-docs/blob/master/reference/onenote/tablecell.md

    A table cell simply contains paragraphs. Those paragraphs can be richtext, image, outline, or even yet another table with tablerows and tablecells.

    To know what is inside a table cell, you'd have to load the children paragraph with their type, and then load the children paragraph properties based on their type (the same way you did it in your code).