I have no background in programming but I am trying to come up with a Script on Google Sheets to remove the first 3 columns in my Workbook except the "Main Menu". I have 61 tabs so I didn't want to individually remove them. So, the following is what I managed to come up with from doing the googling. I kept seeing the error "Cannot find method deleteColumns()." Any help would be much appreciated.
function QuickDelete() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sss = ss.getSheets();
for (i = 0; i < sheets.length; i++) {
switch(sss[i].getSheetName()) {
case "Main Menu":
break;
default:
ss.deleteColumns(sss[i]);
}
}
}
In your for
loop, your are using sheets
, which you haven't defined before. You have to change sss
to sheets
or viceversa everywhere in your code.
You have to provide two parameters to deleteColumns, the position of the first column
to delete and the number of columns
to delete. In your case, 1
and 3
respectively.
So your code could be like this:
function QuickDelete() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
for (i = 0; i < sheets.length; i++) {
if (sheets[i].getSheetName() !== "Main Menu") {
sheets[i].deleteColumns(1, 3)
}
}
}
Also, the switch
statement makes sense if there are many values
you have to check your expression
with. In your case, I'd better use if
to check if the name of the sheet is Main Menu
.
https://developers.google.com/apps-script/reference/spreadsheet/sheet#deleteColumns(Integer,Integer) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else