I'm trying to use Office.js API to build an Angular Excel add-in, but I'm having a problem converting an Excel.Range to an Excel.Table.
I have a code like the example below binded to a button click, but although I manage to get the documentLines in the Excel Sheet, no table is created.
getExampleTable() {
const documentLinesHeader = [["Period 1", "Period 2", "Period 3"]];
const documentLines = [
["Line 1.1", "Line 1.2", "Line 1.3"],
["Line 2.1", "Line 2.2", "Line 2.3"],
["Line 3.1", "Line 3.2", "Line 3.3"],
];
const documentLinesCount = documentLines.length;
const columnCount = documentLines[0].length;
Excel.run(async (ctx) => {
let sheet = ctx.workbook.worksheets.getActiveWorksheet();
let cell = sheet.getCell(0,0);
let tableRange = cell.getResizedRange(documentLinesCount - 1, columnCount - 1);
tableRange.load("address");
await ctx.sync();
console.log("Table Range is:", tableRange.address);
// Outputs Table Range is: Sheet1!A1:C3
tableRange.values = documentLines;
let exampleTable = sheet.tables.add(tableRange, true);
exampleTable.getHeaderRowRange().values = documentLinesHeader;
exampleTable.name = "ExampleTable";
return await ctx.sync();
})
.catch(this.errorHandler);
}
If I look at the console, I have the error:
Debug info: {"code":"InvalidArgument","message":"The argument is invalid or
missing or has an incorrect format.","errorLocation":"TableCollection.add"}
My Office Version is: 16.0.4639.1000