Hide columns based on cell value
The following code does the trick:
function onOpen() {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// create menu
var menu = [{name: "Hide columns", functionName: "hideColumn"},
{name: "Show all columns", functionName: "showColumn"}];
// add to menu
ss.addMenu("Check", menu);
}
function hideColumn() {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get first sheet
var sheet = ss.getSheets()[0];
// get data
var data = sheet.getDataRange();
// get number of columns
var lastCol = data.getLastColumn()+1;
Logger.log(lastCol);
// itterate through columns
for(var i=1; i<lastCol; i++) {
if(data.getCell(3, i).getValue() == '') {
sheet.hideColumns(i);
}
}
}
function showColumn() {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get first sheet
var sheet = ss.getSheets()[0];
// get data
var data = sheet.getDataRange();
// get number of columns
var lastCol = data.getLastColumn();
// show all columns
sheet.showColumns(1, lastCol);
}
- Creates menu upon opening file
- First menu option, hides all columns with row 3 being empty
- Second option, shows all hidden columns
See example file I prepared to watch it in action: Hide columns based on cell value