3

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.

1 Answer 1

5

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); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't work because all properties are stored as string, i.e. false is stored as "false", which is true! So we'd better use string comparison.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.