使用Google电子表格中的ContactsApp

时间:2018-12-04 18:08:22

标签: google-apps-script google-sheets google-contacts custom-function

概述::当我在GoogleAppsScript函数中调用ContactsApp.getContact()时,从脚本编辑器运行时,该函数可以正常工作。但是,当我尝试使用Google Spreadsheet中的函数时,出现权限错误。如何解决此权限错误并通过电子表格中的自定义功能使用ContactsApp?

详细信息: 我创建一个新的电子表格,然后打开“脚本编辑器”并定义以下简单功能:

function example() {
  ContactsApp.getContacts();
  return 5;
}

我第一次从脚本编辑器运行它时,系统会提示我批准对我的联系人的访问。我批准,该功能运行良好。我可以添加日志记录并验证其是否按预期工作。

现在回到电子表格中,我在单元格中使用公式'= example()',期望它将单元格的值设置为5。相反,该单元格显示“ #ERROR!”。并将鼠标悬停在单元格上会显示详细错误:

You do not have permission to call ContactsApp.getContacts. Required permissions: https://www.google.com/m8/feeds (line 2)

查看脚本的项目属性,可以看到https://www.google.com/m8/feeds确实被列为必需的oauth范围。

如何解决此权限错误并通过电子表格中的自定义功能使用ContactsApp?

1 个答案:

答案 0 :(得分:1)

我在这里的文档中找到了答案: https://developers.google.com/apps-script/guides/sheets/functions#advanced

Unlike most other types of Apps Scripts, custom functions never ask users to authorize access to personal data. Consequently, they can only call services that do not have access to personal data, specifically the following: Cache HTML JDBC Language Lock Maps Properties Spreadsheet URL Fetch Utilities XML

因此,当前不允许根据需要在自定义函数中使用ContactsApp,因为需要授权,而自定义函数不允许这样做。