Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Spreadsheet script "toggle" hide/view column/row

I know that using these function I can show and hide columns:

function showColumns() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  sheet.showColumns(2,3);   // B-D, three columns starting from 2nd 
  sheet.showColumn(7);      //  G, column number 7
}

function hideColumns() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  sheet.hideColumns(2,3);  // B-D, three columns starting from 2nd
  sheet.hideColumn(7);     // G, column 7
}

But for that, I need two buttons or menu script, one for hiding and one for showing the columns.

I would like to know if there is a "toggle" function that exist so I can activate the hide/show function without needing 2 buttons or menu items.

like image 334
user2970517 Avatar asked Sep 04 '26 12:09

user2970517


1 Answers

I don't see a way to detect the hidden/unhidden state of a column with a script, but the toggle behavior can be implemented by storing the state in Script Properties. Here is the function that manages the properties and calls either of two other functions, as appropriate.

function toggleColumns() {
  var scriptProperties = PropertiesService.getScriptProperties();
  if (scriptProperties.getProperty('hidden')) {
    showColumns();
    scriptProperties.setProperty('hidden', false); 
  }
  else {
    hideColumns();
    scriptProperties.setProperty('hidden', true);
  }
}

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!