Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hide Google Sheets columns with zeros
/*
* https://stackoverflow.com/a/13591754/31493
*/
function onOpen() {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// create menu
var menu = [{name: "Hide columns", functionName: "hideColumns"},
{name: "Show all columns", functionName: "showColumns"}];
// add to menu
ss.addMenu("Show/Hide Columns", menu);
}
function hideColumns() {
// 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);
// iterate through columns
for (var i = 1; i < lastCol; i++) {
if (data.getCell(3, i).getValue() == 0) {
sheet.hideColumns(i);
}
}
}
function showColumns() {
// 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.