0
votes

I want to create a way to hide a certain number of rows if there is a certain value in a cell of a Google spreadsheet.

E.g.

         A                   B
1   Include All options`?       Yes/No
2   Option A:                   Yes/No
3   Option B:                   Yes/no
etc.

If cell B1 is "yes" there is no need to look at all other options. However, if they select "No", there needs to be an option for selecting the Option "Yes/No". For this reason, I would like to hide the rows when option = yes but unhide them if it is No

1
please read how to post here. You cant post a spec and expect someone to code it. - Zig Mandel
Also its a dup of others like stackoverflow.com/questions/20383874/… - Zig Mandel

1 Answers

0
votes

This code detects whether a certain cell (B3) was edited, then hides a couple of rows under that row (row 3) if it was edited:

function onEdit(e) {
  Logger.log('e.value: ' + e.value);

  var cellEdited = e.range.getA1Notation();
  Logger.log('cellEdited: ' + cellEdited);

  if (cellEdited === "B3") {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var theSheet = ss.getActiveSheet();

    theSheet.hideRows(4, 2);
  };
}

This code uses a function name of onEdit(), which means that it monitors the spreadsheet for edits to a cell. If there is an edit, the function runs. This is a "simple" trigger. There are also installable triggers for monitoring the spreadsheet.

Note the Logger.log() statements. These statement print information to the LOGS which you can VIEW from the VIEW, LOGS menu.

For your purposes:

function onEdit(e) {
  Logger.log('e.value: ' + e.value);

  var cellEdited = e.range.getA1Notation();
  Logger.log('cellEdited: ' + cellEdited);

  if (cellEdited === "B1" && e.value === "yes") {

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var theSheet = ss.getActiveSheet();

    theSheet.hideRows(2, 2);
  };
}