I am trying to trigger a function in Google Apps Scripts, and am using openById("Sheet_Id); But on using it, I am getting this error:

Exception: You do not have permission to call SpreadsheetApp.openById. Required permissions: https://www.googleapis.com/auth/spreadsheets at saveActiveSheet(Code:2:27) at onSelectionChange(Code:27:9)

1 Answer 1


onSelectionChange is a reserved name for a simple trigger. This kind of trigger can't execute methods that require permission to run, like SpreadsheetApp.openById. Ref. https://developers.google.com/apps-script/guides/triggers.

You have to design think for another way to run your function, like using a custom menu, a custom dialog, a sidebar, an installable trigger, the Apps Script REST API or running the function from the Google Apps Script editor.

  • I tried running the function from the Google Apps Script Editor, but it assumes the active sheet to be Sheet0 by default, even though the sheet that is open in my Google Sheet is Sheet1, and therefore prints the wrong value. Oct 21 at 13:17
  • function saveActiveSheet() { var ss = SpreadsheetApp.openByUrl("URL_OF_THE_SHEET").getActiveSheet(); console.log(ss.getSheetId()); var userProperties = PropertiesService.getUserProperties(); userProperties.setProperty('ACTIVE_SHEET', ss.getSheetName()); return ss.getSheetId(); } Oct 21 at 13:18
  • Follow up questions should be posted as a new question.
    – Rubén
    Oct 21 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.