0
votes

Currently building a function where when a certain selection is selected from a drop-down data validation menu, the user is redirected to a different sheet within the workbook. What I currently have does not return an error but also does not do what is intended. Any help would be greatly appreciated.

function gotoselectsheetnosheet() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var range = sheet.getRange(4,3);
  var data = range.getValue();  
  if (data == "Answers")
  {  
     var dinner = ss.getSheetByName('Answers');
     dinner.activate();
   }
}

please see this image for further clarification

2

2 Answers

1
votes

Try this solution.

function onEdit(e) {
  if(e.source.getActiveSheet().getName() === "Menu" && e.range.getRow() === 4 && e.range.getColumn() === 3) {
    e.source.getSheetByName(e.value).activate();
  }
}
1
votes

This works if you setup an onEdit() trigger.

function gotosheet(e)
{
  e.source.getSheetByName(e.value).activate();

}

e.source is the event event objects resource for the spreadsheet object. Look here to learn more about it.

So then the next is getSheetByName(e.value) and e.value is the new value in the cell being edited.

Then the next is activate. And that makes it move to the named sheet. So presumably you have selected the appropriate sheet names in your validation setup.

Validation setup:

enter image description here

enter image description here When you select one of the two possible selections in the validation drop down that triggers the onEdit trigger and the function takes you to the tab with that name.

Named Range Used in Validation Setup:

enter image description here

OnEdit Trigger Setup:

enter image description here