【问题标题】:Function from script editor works fine but not from sheets custom function脚本编辑器中的功能可以正常工作,但不能来自工作表自定义功能
【发布时间】:2020-12-12 18:08:39
【问题描述】:

我写了一个添加全天事件的函数。

当函数从脚本编辑器运行正常时,全天事件被插入到日历中。

但是当我在活动电子表格=addevent(a1,b1,c1,d1)中运行函数时

我收到一条错误消息:

例外:脚本无权执行该操作。所需权限:(https://www.googleapis.com/auth/calendar || https://www.googleapis.com/auth/calendar.readonly || https://www.google.com/calendar/feeds)(第 11 行)

在网上搜索后,我发现如何通过编辑 JSON 文件中的 oauthScopes 手动添加这些权限:

{
  "oauthScopes": [
    "https://www.googleapis.com/auth/calendar",
    "https://www.googleapis.com/auth/calendar.readonly",
    "https://www.google.com/calendar/feeds",
    "https://www.googleapis.com/auth/spreadsheets.currentonly",
    "https://www.googleapis.com/auth/spreadsheets"
    
  ],
  
  "timeZone": "censored",
  "dependencies": {
    "enabledAdvancedServices": [{
      "userSymbol": "Calendar",
      "serviceId": "calendar",
      "version": "v3"
    }]
  },
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8"
  


}

但我仍然在电子表格中遇到同样的错误...

【问题讨论】:

    标签: google-apps-script google-sheets permissions google-calendar-api custom-function


    【解决方案1】:

    这是因为您不能使用自定义函数来执行需要授权的操作,例如将活动添加到您的日历。

    来自official documentation

    与大多数其他类型的 Apps 脚本不同,自定义函数从不询问 用户授权访问个人数据。

    但是,您可以尝试一些解决方法。例如,您可以在电子表格文件或时间驱动触发器中创建自定义菜单选项。

    【讨论】:

      【解决方案2】:

      答案:

      虽然无法直接从工作表中的单元格运行需要授权的脚本,但您可以将脚本设置为在单击按钮时运行。

      更多信息和解决方法:

      由于按钮没有与自定义函数相同的authorisation restrictions,因此您可以使用它们在所需范围内运行代码。要进行设置:

      1. 转到Insert > Drawing菜单项并创建一个形状;任何形状都可以,这将充当您的按钮。
      2. Save and Close 将此添加到您的工作表中。
      3. 将新添加的绘图移动到您想要的位置。在图形的右上角,您将看到垂直省略号菜单 ()。点击这个,然后点击Assign script
      4. 在新窗口中,输入您的函数名称,不带括号(例如myFunction),然后按OK

      参考资料:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-28
        • 2023-03-23
        相关资源
        最近更新 更多