why apps script execution failed only when it is triggered by onEdit event on Google Spreadsheet

寵の児 提交于 2020-07-31 04:33:24

问题


I am working on Google Apps Script. I have apps script that I have wrote that is supposed to be executed by onEdit event on specific sheet.

onEdit.gs

function onEdit(e) {
    var syokuninIchiran = new SyokuninIchiran();
    syokuninIchiran.syncTable();
}

When the above is executed by edit on the sheet, this error occurs:

You do not have permission to call openById. Required privilege: https://www.googleapis.com/auth/spreadsheets

But, it works with no problem when the script is triggered from Google Apps Script development window. I call the same code by making a test function as follows:

function test(){
  var syokuninIchiran = new SyokuninIchiran();
  syokuninIchiran.syncTable();
}

It might be problem for this issue? The spreadsheet's owner is not mine. My account has right to edit as the image below.

Try1: I tried to add the scope manually into the manifest file(appsscript.json). And I approved the authorization. But, nothing changed. It still have the same error message.

{
  "oauthScopes": [
    "https://www.googleapis.com/auth/sqlservice",
    "https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/script.external_request"
    ]
}

回答1:


Answer:

Simple Triggers can not open files aside from the file it is bound to. For this, you need to use an installable trigger.

More Information:

As per the documentation on Simple Trigger restrictions:

Because simple triggers fire automatically, without asking the user for authorization, they are subject to several restrictions:

They can modify the file they are bound to, but cannot access other files because that would require authorization.

So an installable trigger must be used.

Setting up the Installable Trigger:

Follow the Edit > Current project's triggers menu item, which will open a new page in the G Suite Developer Hub. Click the + Add Trigger button in the bottom right and set up the trigger settings as follows:

  • Choose which function to run: onEdit
  • Choose which deployment should run: Head
  • Select event source: From Spreadsheet
  • Select type of time based trigger: On Edit

And press save. This will increase the range of functionality of your edit triggers.

I hope this is helpful to you!

References:

  • Simple Triggers | Apps Script | Google Developers
  • Installable Triggers | Apps Script | Google Developers


来源:https://stackoverflow.com/questions/62083138/why-apps-script-execution-failed-only-when-it-is-triggered-by-onedit-event-on-go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!