0
votes

I am just learning to play with scripts and cannot get a script to hide and show a specific row to work on the sheet I want to apply the script to. I have a google sheet I created as a sandbox where it does work but once I copy the script and assign the script to a button the "show" function does not work while the hide function continues to.

All I'm trying to do is hide row 3 and then be able to unhide the same row on this sheet.

/** @OnlyCurrentDoc */

function hide() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('3:3').activate();
  spreadsheet.getActiveSheet().hideRows(spreadsheet.getActiveRange().getRow(), spreadsheet.getActiveRange().getNumRows());
  spreadsheet.getRange('4:4').activate();
};

function show() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('4:4').activate();
  spreadsheet.getActiveSheet().showRows(3, 1);
};

There are no errors, the script runs to completion without any visible changes to the sheet(the row is still hidden)

2
If you didn't this yet, please read developers.google.com/apps-script/guides/sheets - Rubén
The title says "Script to hide a google sheets row doesn't work consistently", but on a comment to my answer you says that the hide part works fine. please edit your question to clarify what works and what don't . - Rubén

2 Answers

1
votes

Try this

/**
 * This hides the row 3 of the active sheet.
 *
 */ 
function hide(){
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.hideRow(3);
}

The above code is a simplified version. Since you want to hide only one row, use it use hideRow(rowNum).

Reference

0
votes

You could also consider highlighting the row and clicking "group row". This will create a collapsing + button that will show/hide that row on the left hand side of the page