function myFunction() {
var url = 'https://api.github.com/users/chaimf90/repos'
var response = UrlFetchApp.fetch(url);
var json = response.getContentText();
var data = JSON.parse(json)
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(['Repo Name', data[0].name]);
}
When I execute this function from the script editor it runs as expected, but when I try to call this function in the sheet itself by calling =myFunction()
, I get an error saying that I do not have permission to call appendRow
.
Why can I call this function from the script editor, but not from the sheet itself?
答案 0 :(得分:1)
我有同样的问题。解决方案似乎是创建一个运行Apps脚本功能的自定义菜单,而不是编写自定义功能并从电子表格的单元格中调用它。
从菜单中触发的功能将在必要时询问用户授权,因此可以使用所有Apps Script服务。我学会了here。
示例:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('My Custom Menu')
.addItem('Run My Function', 'myFunction')
.addToUi();
}
function myFunction() {
SpreadsheetApp.getUi()
.alert('Running My Function');
}
编写并保存此代码后: