Reputation: 15
Trying to write and if statement in Google sheets to hide a range of columns dependent on cell value in D4
If D4 == 1 hide columns H:AA
If D4 == 2 hide columns O:AA
Else show A:AA
Code below:
var cell = "D4";
var value1 = 1;
var value2 = 2;
var value3 = 3;
var activeSheet = ss.getActiveSheet();
function HideColumn(); {
If(cell == value1) {
activeSheet.hideColumn("H:AA");
Else If(cell == value2) {
activeSheet.hideColumn("O:AA");
Else If(cell == value3) {
activeSheet.unhideColumn("H:AA");
}
}
}
}
Upvotes: 0
Views: 367
Reputation: 26836
var ss=SpreadsheetApp.getActive();
var value1 = 1;
var value2 = 2;
var value3 = 3;
var activeSheet = ss.getActiveSheet();
var cell = activeSheet.getRange("D4").getValue();
function HideColumn() {
if(cell == value1) {
activeSheet.hideColumns(8, (27-8+1));
}
else if(cell == value2) {
activeSheet.hideColumns(15, (27-15+1));
}
else if(cell == value3) {
activeSheet.hideColumns(8, (27-8+1));
}
}
Explanations:
hideColumns(columnIndex, numColumns)
(mind the notation)if
and else if
statements should be separate rather than nested (note the brackets)if
and else
statements in Apps Script are written in low casevar cell = activeSheet.getRange("D4").getValue();
I hope my answer was helpful for you.
Upvotes: 1