JavaScript - Snippets

link - learn.microsoft.com/en-us/office/dev/add-ins/excel/excel-add-ins-tables


const body = context.document.body;

const wrdTbl = body.insertTable(7, 4, Word.RangeLocation.start)

const wrdRows = wrdTbl.rows


const row_1 = wrdRows.getFirst()

row_1.shadingColor = '#003366'


row_1.preferredHeight = 40

row_1.font.name = "Roboto Medium"

row_1.font.size = 12

row_1.horizontalAlignment = "Centered"

row_1.verticalAlignment = "Center"


row_1.values = [['Name','Age','Sales','Cost']]

wrdRows.load('items')

await context.sync();


for (var i = 1; i < wrdRows.items.length; i++){


wrdRows.items[i].values = [['Alex','70','40000','60000']]

wrdRows.items[i].font.color = '#666666'

wrdRows.items[i].verticalAlignment = Word.VerticalAlignment.center

wrdRows.items[i].preferredHeight = 35

wrdRows.items[i].cells.load('items');

await context.sync();


wrdRows.items[i].cells.items[1].horizontalAlignment = 'Right'

wrdRows.items[i].cells.items[2].horizontalAlignment = 'Right'

wrdRows.items[i].cells.items[3].horizontalAlignment = Word.Alignment.justified

wrdRows.items[i].cells.items[3].setCellPadding(Word.CellPaddingLocation.top, 10)

}


const tblBorders = wrdTbl.getBorder(Word.BorderLocation.all)

tblBorders.color = "#000044"

tblBorders.width = 1

tblBorders.type = Word.BorderType.wave

const tblBordersIV = wrdTbl.getBorder(Word.BorderLocation.insideVertical)

tblBordersIV.type = "None"


const tblBordersOT = wrdTbl.getBorder(Word.BorderLocation.outside)

tblBordersOT.type = "None"


wrdTbl.load('headerRowCount');

wrdTbl.styleBuiltIn = Word.Style.gridTable6Colorful_Accent1

wrdTbl.styleLastColumn = true;


await context.sync();



Create and access a table

$("#run").click(() => tryCatch(getTableCell));

$("#setup").click(() => tryCatch(insertTable));


async function getTableCell() {

await Word.run(async (context) => {

let firstCell = context.document.body.tables.getFirst().getCell(0, 0).body;

firstCell.load("text");


await context.sync();

console.log("First cell text is " + firstCell.text);

});

}


async function insertTable() {

await Word.run(async (context) => {

let data = [["Tokyo", "Beijing", "Seattle"], ["Apple", "Orange", "Pineapple"]];

let table = context.document.body.insertTable(2, 3, "Start", data);

table.styleBuiltIn = Word.Style.gridTable5Dark_Accent2;

table.styleFirstColumn = false;


await context.sync();

});

}


async function tryCatch(callback) {

try {

await callback();

} catch (error) {

console.error(error);

}

}


??

function insertTable() {

Word.run(function (context) {

var range = context.document.getSelection();

var cc = range.insertContentControl();

cc.title = "My Table";

var values = [["Apple", "red", "round"], ["Banana", "yellow", "long"], ["Pear", "green", "oblong"]];

context.load(cc);

return context.sync().then(function () {

var table = cc.insertTable(3, 3, 'Start', values);

})

.then(context.sync);

})

.catch(function (error) {

console.log('Error: ' + JSON.stringify(error));

if (error instanceof OfficeExtension.Error) {

console.log('Debug info: ' + JSON.stringify(error.debugInfo));

}

});

}


let myTable = new Office.TableData();

myTable.headers = ["First Name", "Last Name", "Grade"];

myTable.rows = [["Brittney", "Booker", "A"], ["Sanjit", "Pandit", "C"], ["Naomi", "Peacock", "B"]];

Office.context.document.setSelectedDataAsync(myTable, { coercionType: Office.CoercionType.Table });


Word.run(async function (ctx) {


let myTable = new Office.TableData();

myTable.headers = ["First Name", "Last Name", "Grade"];

myTable.rows = [["Brittney", "Booker", "A"], ["Sanjit", "Pandit", "C"], ["Naomi", "Peacock", "B"]];

Office.context.document.setSelectedDataAsync(myTable, { coercionType: Office.CoercionType.Table });


await ctx.sync();


event.completed();


}).catch((error) => {

BETDebug.error_Handler(c_MODULE, "action_Tables_InsertDataTable", error);

})


link - contrivedexample.com/2016/01/01/3-ways-to-create-a-ms-word-table-in-office-js-add-in/


© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrev