Insert a Column using Office Script

Anonymous
2022-08-11T11:43:23.927+00:00

Hi there, I am trying to insert a column using Office Scripts but it fails with errors.

230427-image.png

230428-image.png

What could be the issue?

Microsoft 365 and Office | Development | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Brian Gonzalez 76 Reputation points
    2022-08-11T23:14:17.9+00:00

    You can try using addColumn() to add a new column to the table instead of getRange(). Add column expects a number and is zero indexed. So index 0 would map to column A, index 1 would map to column B, etc. Since you want to insert where column B would be that would be index 1. After the addColumn() method, you can set the header name for the new column using the setName() method. You can see an example of how to do that using the code below:

    function main(workbook: ExcelScript.Workbook) {  
      let sh = workbook.getActiveWorksheet()  
      let tbl = sh.getTable("Table1")  
      tbl.addColumn(1).setName("Status")  
    }  
    

1 additional answer

Sort by: Most helpful
  1. Remus Laslau 0 Reputation points
    2023-02-28T10:06:29.6866667+00:00

    I was struggling with a similar script until I realised there is a 'Record Actions' option.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.