0

Can someone please help me to update only specific columns in Excel Table using Office JS API. I want to just update only column PSID and Name as shown in below diagram. I am using Node JS with project scaffolded from yo Office template with React and TypeScript.

enter image description here

Code that I was trying:

handleSubmit = async () => {
try {
  await Excel.run(async context => {
    var sheet = context.workbook.worksheets.getItem("Tech-Panel");
    var expensesTable = sheet.tables.add("A1:B1", true);//.getItem("TechPannelTable");

    const { PSID, Name, itcEmailIds, clientEmailIds, contactNo, skills, panelStatus,
      client, itcManager, availability, preferredTime, remarks } = this.state.techPanelEntry;

      expensesTable.getHeaderRowRange().values = [["PSID", "Name"]]
      /*expensesTable.rows.add(null, [
        [PSID, Name, itcEmailIds, clientEmailIds, contactNo, skills,
          panelStatus, (dateOfInactive != null ? dateOfInactive.toLocaleDateString("en-US") : null), client, itcManager, availability, preferredTime, remarks]
      ]);*/
      expensesTable.rows.add(null, [
        [PSID, Name]
      ]);

    
    if (Office.context.requirements.isSetSupported("ExcelApi", "1.2")) {
      sheet.getUsedRange().format.autofitColumns();
      sheet.getUsedRange().format.autofitRows();
    }
    await context.sync();
    this.setState({
      ...this.state,
      techPanelEntry: defaultTechPanel()
    })
  });
} catch (error) {
  this.setState({ error: JSON.stringify(error) })
}};
Tanuj
  • 53
  • 1
  • 8

1 Answers1

0

You can refer to this to update any column you need: table.columns.items[1].getDataBodyRange().values = [["aa"], ["bb"], ["cc"], ["dd"], ["dee"], ["gg"], ["uu"]];

Rita
  • 72
  • 3
  • Hi, thanks for the input. So let suppose I have to update 6 column values out of 29, then I have to write the above statement 6 times. Also is there any other method to achieve this if my columns are not adjacent. – Tanuj Apr 12 '21 at 05:55
  • if you update 6 column, just duplicate line and change the index number to the index you want to change. – Rita Apr 12 '21 at 08:34