Google-sheets – Google Sheets Customize View Using Create Custom Menu and Hiding/Showing Specifc Columns

google sheetsgoogle-apps-script

Trying to determine a way that I can use a custom menu (.createMenu) with views that will allow me to show / hide specific columns. I need the capability to pick specific multiple columns and not ranges. In the example below how can you pick a custom view from the menu (say "Customer Info") that will only show columns A, B, C, and H? Maybe another view could show "Financial".

enter image description here

Best Answer

Below is a rough example of how to show / hide individual columns from a Google Sheet.

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Custom Menu')
      .addItem('Show View A', 'showViewA')
      .addItem('Show View B', 'showViewB')
      .addItem('Show All', 'showAll')
      .addToUi();
}

function showViewA() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  showAll();
  sheet.hideColumn(sheet.getRange("A1"));
  sheet.hideColumn(sheet.getRange("F1"));
}

function showViewB() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  showAll();
  sheet.hideColumn(sheet.getRange("C1"));
  sheet.hideColumn(sheet.getRange("D1"));
}

function showAll() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  sheet.unhideColumn(sheet.getRange(1, 1, 1, sheet.getLastColumn()));
}

The View A menu item hides columns A and F, while View B menu item hides columns C and D. The Show All menu item does what it says on the tin. Hopefullly the example gives a starting point.