Mlemort
Mlemort

Reputation: 11

GAS - "You do not have permission to call SpreadsheetApp.openById"

One of my scripts uses SpreadsheetApp.openById(ID) to get data from a spreadsheet. The script has been given the permissions to open the file, and the user of the script has write access on the file. However, sometimes, the script returns the following error :

You do not have permission to call SpreadsheetApp.openById. Required permissions: https://www.googleapis.com/auth/spreadsheets at [unknown function](Code:11)

Other times, there are no issue and the script works perfectly. Any idea/help on how to fix this?

Script example :

var spreadsheet = SpreadsheetApp.openById("1m65QVfk9Xx2zOwUxewdSjWCDK-g_jHj7ETFVFF0rjxY")

function myFunc() {
  sheet = spreadsheet.getActiveSheet()
  prepareHours()
  inputDisplays()
}

Upvotes: 0

Views: 3735

Answers (1)

tehhowch
tehhowch

Reputation: 9872

You can't "give the script permission to open the file," because the script is not autonomous: a user executes the script. If the user that causes the script to execute does so in a manner that provided no authorization--such as the execution of a simple trigger like a function named onOpen or onEdit--then any calls that require authorization will fail with a permission-based error like the one you share.

Since globals are evaluated for every Apps Script instance, it is always recommended to avoid invoking API / Service calls in global scope. Additionally, since simple triggers execute for any user with permission to edit the document, it is recommended to avoid using methods that require user permissions in situations that have restricted AuthModes.

  • Simple Trigger Restrictions
  • Executing User
  • Accessible services

    Warning: When your onOpen(e) function runs, the entire script is loaded and any global statements are executed. These statements execute under the same authorization mode as onOpen(e) and fail if the mode prohibits them. This prevents onOpen(e) from running. If your published add-on fails to add its menu items, look in the browser's JavaScript console to see if an error was thrown, then examine your script to see whether the onOpen(e) function or global variables call services that aren't allowed in AuthMode.NONE.

  • AuthMode

Upvotes: 2

Related Questions