来自脚本编辑器的功能可以正常工作,但不能用于图纸自定义功能 [英] Function from script editor works fine but not from sheets custom function
问题描述
我编写了一个添加全天活动的函数.
I wrote a function that adds an all-day event.
从脚本编辑器运行的函数运行正常时,全天事件将插入日历.
When the function running from script editor it's working fine, the all-day event is inserted to the calendar.
但是当我在活动电子表格=addevent(a1,b1,c1,d1)
But when I'm running the function in the active spreadsheet =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行)
Exception: The script does not have permission to perform that action. Required permissions: (https://www.googleapis.com/auth/calendar || https://www.googleapis.com/auth/calendar.readonly || https://www.google.com/calendar/feeds) (line 11)
搜索网络后,我发现如何通过编辑JSON文件中的oauthScopes
手动添加那些权限:
After searching the net I found how to add manually those permission by editing the oauthScopes
in the JSON file:
{
"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"
}
但是我在电子表格中仍然遇到相同的错误...有什么想法吗?
But I am still getting the same error in the spreadsheet... any ideas?
推荐答案
答案:
虽然无法直接从工作表的单元格运行需要授权的脚本,但可以将脚本设置为单击按钮即可运行.
按钮没有相同的授权限制作为自定义函数,您可以使用它们在所需范围内运行代码.要进行设置:
As a button does not have the same authorisation restrictions as custom functions, you can use them to run code with the scopes you need. To set this up:
- 转到
Insert > Drawing
菜单项并创建形状;任何形状都可以,这将充当您的按钮. - 按
Save and Close
键将其添加到工作表中. - 将新添加的图形移动到您想要的位置.在图形的右上角,您将看到垂直省略号菜单(
⋮
).单击此,然后单击Assign script
. - 在新窗口中,键入函数名称而不带括号(例如
myFunction
),然后按OK
.
- Go to the
Insert > Drawing
menu item and create a shape; any shape will do, this will act as your button. - Press
Save and Close
to add this to your sheet. - Move the newly-added drawing to where you would like. In the top-right of the drawing, you will see the vertical ellipsis menu (
⋮
). Click this, and then clickAssign script
. - In the new window, type whatever the name of your function is, without parentheses (for example
myFunction
), and pressOK
.
希望对您有帮助!
这篇关于来自脚本编辑器的功能可以正常工作,但不能用于图纸自定义功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!