Google Sheets "You do not have permission to call appendRow"

时间:2017-04-24 17:20:49

标签: javascript google-apps-script google-spreadsheet-api

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?

1 个答案:

答案 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');
}

编写并保存此代码后:

  1. 关闭并重新打开您的Google表格文档。
  2. 几秒钟后,新菜单“我的自定义菜单”将出现在 顶部,“文件”,“编辑”,“视图”,“帮助”旁边。
  3. 单击“我的自定义菜单”,然后单击“运行我的功能”以调用 函数myFunction。