首页 > 解决方案 > 使用 Google 电子表格中的 ContactsApp

问题描述

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

标签: google-apps-scriptgoogle-sheetsgoogle-contacts-apicustom-function

解决方案


我在这里的文档中找到了答案: 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,因为需要授权,而自定义函数不允许这样做。


推荐阅读