1

I am trying to run a simple script that opens a Google Spreadsheet and a Google Form in the same standalone Apps Script file. It seems like the script can only give authorization to the FormApp, but not the SpreadsheetApp. The authorization scope does not grant permission to manipulate the spreadsheet.

The error is as follows:

You do not have permission to call openById

If I run both lines separately (by removing one line or another), then the script works normally.

Follow the example code.gs:

function Code(){
    var spreadsheet = SpreadsheetApp.openById("<spreadsheet_id>")
    var form = FormApp.openById("<form_id>")
}

Follow the script scopes listed:

https://www.googleapis.com/auth/forms

Note that the App Script is not asking for the Spreadsheet scope.

The weird part is If I create a bounded script into a Spreadsheet or a Form, then the script works normally. The problem only happens on a standalone script.

I am not using the @OnlyCurrentDoc annotation

3
  • 1
    In the code editor, click the "File" menu, then click "Project properties" and click the "Scopes" tab. Please edit your question and post the scopes listed. Commented Sep 16, 2018 at 12:45
  • Not sure what you mean by "if I run both lines separately" Commented Sep 16, 2018 at 13:06
  • Sandy Good, The only scope the script ask is the FormApp scope (googleapis.com/auth/forms). tehhowch, by "running separately" I meant running only the SpreadsheetApp command alone or FromApp command alone. Those both commands do not work together in the same script. Commented Sep 16, 2018 at 14:42

1 Answer 1

3

If you define any scopes manually, you disable automatic scope detection. Either define them all, or remove the oauthScopes section from your manifest file.

You can review the available OAuth2 scopes and details of what they provide on the Google Identity Platform website.

Namely, you need to add a scope allowing access to Google Sheets, such as https://www.googleapis.com/auth/spreadsheets, in addition to other scopes your script requires (such as Forms access, etc.).

Sign up to request clarification or add additional context in comments.

4 Comments

I didn't define any scope manually (As far as I know, you cannot define scopes manually on App Script). Just create a standalone script and insert the function below. You will see what I meant. function Code(){ var spreadsheet = SpreadsheetApp.openById("<spreadsheet_id>") var form = FormApp.openById("<form_id>") }
@rafael see the edited link. Explicit scope control is common (and almost required) for well-designed add-ons.
@RafaelDeAlemarVidal Explicitly defining oauth scopes is a relatively new feature, released on October 2017 (see release notes). And its extremely powerful. Use with care ;)
Well done @tehhowch. It worked by setting the scopes manually. Thank you very much for your prompt and sharp answer.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.