1

I am trying to add cells to the existing table using insert API provided by office js. Microsoft official doc gave sample code to insert in sheet which works fine.

Excel.run(function (context) {
    var sheet = context.workbook.worksheets.getItem("Sample");
    var range = sheet.getRange("B4:E4");

    range.insert(Excel.InsertShiftDirection.down);

    return context.sync();
}).catch(errorHandlerFunction);

But i was trying do same with table on the sheet. But it is not working.

table = ctx.workbook.tables.getItem(tableName);
let range = table.getRange();
range.insert(Excel.InsertShiftDirection.down);

Is there a way to achieve this?

Amir
  • 1,855
  • 3
  • 24
  • 40

1 Answers1

1

You could use Excel.TableRow, the sample code:

Excel.run(function (ctx) { 
    var tables = ctx.workbook.tables;
    var values = [["Sample", "Values", "For", "New", "Row"]];
    var row = tables.getItem("Table1").rows.add(null, values);
    row.load('index');
    return ctx.sync().then(function() {
        console.log(row.index);
    });
}).catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

The document can be found at https://learn.microsoft.com/en-us/javascript/api/excel/excel.tablerowcollection?view=excel-js-preview#add-index--values-

Raymond Lu
  • 2,178
  • 1
  • 6
  • 19
  • Thanks Raymond. But I was looking if that could be achieve using Insert method. I was doing just analysis on this part. Looks like it is not possible. – Amir Apr 01 '20 at 21:57