Joshua
Joshua

Reputation: 15

How to fix 'TypeError: Cannot find function insertColumns' in Google Scripts

I'm fairly new to JS and GS and wanted to create a simple insert at a specific location, but I keep getting this error:

Cannot find method insertColumnBefore(number,number). (line 34, file "InsertColBalSheet")

I've tried: - conversion to a number - setting the getSheetByName to array [0] (which causes the subsequent line to fail)

function InsertColBalSheet() {
    var ss = SpreadsheetApp.getActive();
    var sheet = ss.getSheetByName("BalanceNew");

    ss.setActiveSheet(sheet)

    var cell = ss.getRange("HY3")
    ss.setCurrentCell(cell);
    ss.getActiveSheet().insertColumnBefore(ss.getActiveRange().getColumn(), 4);
};

I'm expecting 4 columns to be inserted at HY3, but instead, I get the error stated above.

Upvotes: 1

Views: 115

Answers (1)

pnuts
pnuts

Reputation: 59475

Since you know where to insert, a slightly simpler version:

function InsertColBalSheet() {
    var ss = SpreadsheetApp.getActive();
    var sheet = ss.getSheetByName("BalanceNew");
      ss.setActiveSheet(sheet)
       var cell = ss.getRange("HY3")
      ss.setCurrentCell(cell);
ss.getActiveSheet().insertColumnsBefore(233,4);
     };

but the key difference is insertColumnsBefore.

Upvotes: 1

Related Questions