xyz
xyz

Reputation: 2300

Error with loop in Google Sheets Script

I am trying to write a script to delete columns by passing an array of column numbers getting error

Cannot find method deleteColumns((class))

I'm pretty sure this is the problem

for (var i = 0; i <= arr.length; i++){
      sheet.deleteColumns(arr[i]);
      };

Thanks for any help on this

function DMC_n(){
   DMC('Elements', [1,2]);  
}

function DMC(shtName, arr){
  var sheet =  SpreadsheetApp.getActiveSpreadsheet().getSheetByName(shtName);
  var range = sheet.getDataRange();
  var values = range.getValues();

  for (var i = 0; i <= arr.length -1; i++){
      sheet.deleteColumns(arr[i]);
      };
}

Upvotes: 0

Views: 78

Answers (1)

nbkhope
nbkhope

Reputation: 7474

Your function DMC() defines three parameters, where the last one, "arr", is not given from your function call:

DMC('Elements', [1,2]);

So the third argument is undefined.

Upvotes: 1

Related Questions