Google-sheets – How to toggle every other column between visible/hidden in the Google Sheets

google sheetsgoogle-apps-script

I have a Google spreadsheet with my monthly balance sheet for my company. I keep track of estimated and actual like this:

Expense | Jan11 Actual | Jan11 Estimate | Dec10 Actual | Dec10 Estimate | etc…

I would love it if I could easily hide all the Estimate columns with a touch of a button, and turn them on with another touch.

How can I do this?

Best Answer

Google Apps Script can do it.

This should get you started:

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [ {name: "Hide", functionName: "myFunction"} ];
  ss.addMenu("Scripts", menuEntries);
}

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var colCount = sheet.getMaxColumns();
  var x;
  for ( x = 1; x < colCount; x = x + 2) {
    var r = sheet.getRange(1,x);
    sheet.hideColumn(r);
  }
}