lonewolf
lonewolf

Reputation: 23

How to go directly to a certain sheet when you open a google spreadsheet?

I want my spreadsheet to open at a certain sheet. I've tried the following but it still just opens the first sheet.

function setActiveSheetToday(){
    var date = new Date();
    var sheet = getSheetByDate(date); //custom function that returns sheet object
    var SS = sheet.getParent();
    SS.setActiveSheet(sheet);
}

Then I make an onOpen trigger.

ScriptApp.newTrigger('setActiveSheetToday').forSpreadsheet(SS_ID).onOpen().create();

When I open the spreadsheet, it still opens at the first sheet. Any suggestions?

EDIT

I think I found the reason why it doesn't work here: https://developers.google.com/apps-script/guides/bound#special_methods

So it apparently only works for bounded scripts..

Upvotes: 1

Views: 432

Answers (2)

dmusgrave
dmusgrave

Reputation: 121

You could make the sheet you want to open on the first sheet. Just add SS.moveActiveSheet(1); to your setActiveSheetToday() function.

Alternatively, activating a sheet from a bound script changes the focus as you want. Doing it from a separate script file doesn't appear to.

Upvotes: 1

Max Makhrov
Max Makhrov

Reputation: 18707

Have you checked the object, your sheet?

You may try this function for simple testing:

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet1');
  ss.setActiveSheet(sheet);
}

make sheet called "Sheet1", go to any other sheet and reload the page.

for your code I suggest the test:

Logger.log(sheet.getName());

Upvotes: 0

Related Questions