简体   繁体   English

Google Apps 脚本 URLFetchApp 权限

[英]Google Apps Script URLFetchApp Permission

I'm new to GAS and I struggle with the permission system.我是 GAS 的新手,我在权限系统方面很挣扎。

I'm a normal Google drive user and I started a spreadsheet and tried to add some code to it.我是一个普通的 Google Drive 用户,我启动了一个电子表格并尝试向其中添加一些代码。 My code is working, but only if I'm in the code editor.我的代码可以工作,但前提是我在代码编辑器中。 I want to use the onEdit() function so it's important for me that it works within the sheet as well.我想使用 onEdit() 函数,所以它也可以在工作表中工作对我来说很重要。 When I ran my code in the editor for the first time it opened a new window where I needed to enter my credentials to allow the script, then it worked.当我第一次在编辑器中运行我的代码时,它打开了一个新窗口,我需要在其中输入我的凭据以允许脚本,然后它就可以工作了。 If I do some changes to a cell in my sheet and the onEdit() function is triggered I receive an error message that says something like this(translated):如果我对工作表中的单元格进行一些更改并触发 onEdit() 函数,我会收到一条错误消息,内容如下(已翻译):

Exception: You are not permitted to call UrlFetchApp.fetch. Required permission: https://www.googleapis.com/auth/script.external_request

In the editor I displayed the manifest file and added the permission to the oauthScopes but within the sheet I still receive the message.在编辑器中,我显示了清单文件并向 oauthScopes 添加了权限,但在工作表中我仍然收到消息。 This is how my code looks like (simplified):这是我的代码的样子(简化):

function onEdit(e)
{
    var data = {
        'key1': 'value1',
        'key2': 'value2'
    };
    var options = {
        'method' : 'post',
        'contentType': 'application/json',
        'payload' : JSON.stringify(data)
    };
    try{
        var response = UrlFetchApp.fetch('https://a-working-url.com', options); //error happening in this line
        //some more data wizardry
    }catch(error)
    {
        Browser.msgBox(error)
    }

}

Any ideas how I can open this permission screen in my sheet or any hints how to solve it in a different way?任何想法如何在我的工作表中打开此权限屏幕或任何提示如何以不同的方式解决它? I want to create a sheet with some code running in the back online.我想创建一个工作表,其中一些代码在后台运行。 I want to share the sheet with some friends, tried it with Excel and VBA before until I realized that it's not working with Excel Online, so I switched to GAS.我想和一些朋友分享表格,之前用 Excel 和 VBA 尝试过,直到我意识到它不适用于 Excel Online,所以我切换到 GAS。

Thanks in advance for your help :)在此先感谢您的帮助 :)

onEdit() , like all simple triggers, is bound by the following restrictions ( see official documentation ): onEdit()和所有简单的触发器一样,受到以下限制( 参见官方文档):

  • The script must be bound to a Google Sheets, Slides, Docs, or Forms file, or else be an add-on that extends one of those applications.该脚本必须绑定到 Google Sheets、Slides、Docs 或 Forms 文件,或者是扩展这些应用程序之一的附加组件。
  • They do not run if a file is opened in read-only (view or comment) mode.如果文件以只读(查看或评论)模式打开,则它们不会运行。
  • Script executions and API requests do not cause triggers to run.脚本执行和 API 请求不会导致触发器运行。 For example, calling Range.setValue() to edit a cell does not cause the spreadsheet's onEdit trigger to run.例如,调用 Range.setValue() 来编辑单元格不会导致电子表格的 onEdit 触发器运行。
  • They cannot access services that require authorization.他们无法访问需要授权的服务。 For example, a simple trigger cannot send an email because the Gmail service requires authorization, but a simple trigger can translate a phrase with the Language service, which is anonymous.例如,简单触发器无法发送电子邮件,因为 Gmail 服务需要授权,但简单触发器可以使用匿名的语言服务翻译短语。
  • They can modify the file they are bound to, but cannot access other files because that would require authorization.他们可以修改绑定到的文件,但不能访问其他文件,因为这需要授权。
  • They may or may not be able to determine the identity of the current user, depending on a complex set of security restrictions.他们可能会也可能无法确定当前用户的身份,具体取决于一组复杂的安全限制。
  • They cannot run for longer than 30 seconds.它们的运行时间不能超过 30 秒。
  • In certain circumstances, editor add-ons run their onOpen(e) and onEdit(e) simple triggers in a no-authorization mode that presents some additional complications .在某些情况下,编辑器附加组件在无授权模式下运行它们的 onOpen(e) 和 onEdit(e) 简单触发器,这会带来一些额外的复杂性 For more information, see the guide to the add-on authorization lifecycle.有关更多信息,请参阅附加授权生命周期指南。
  • Simple triggers are subject to Apps Script trigger quota limits.简单触发器受 Apps 脚本触发器配额限制的约束。

The ones highlighted in bold apply to your question.以粗体突出显示的内容适用于您的问题。

Basically, it boils down to this - UrlFetchApp.fetch() is a service that requires authorization, so you won't be able to execute it from your onEdit(e) trigger, even if you have its associated scope set in your manifest file.基本上,它归结为 - UrlFetchApp.fetch()是一项需要授权的服务,因此您将无法从onEdit(e)触发器执行它,即使您在清单文件中设置了其关联范围.

Use installable trigger instead and write your own "onEdit" function (with a different name) that you bind to your installable trigger.改用可安装触发器并编写自己的“onEdit”函数(使用不同的名称),绑定到可安装触发器。

https://developers.google.com/apps-script/guides/triggers/installable#g_suite_application_triggers https://developers.google.com/apps-script/guides/triggers/installable#g_suite_application_triggers

This solved the issue for me.这为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM