【问题标题】:Google Apps Script - Contacts - Query on multiple stringsGoogle Apps 脚本 - 联系人 - 查询多个字符串
【发布时间】:2018-07-18 18:53:44
【问题描述】:

我正在编写一个脚本来删除和重新添加大约 100 个联系人。我对ContactsApp.getContactsByEmailAddress 有 12 种不同的搜索条件,最初知道这些条件需要 30 多秒才能运行。有什么方法我只能运行一次并搜索我的所有条件吗?我一直在寻找其他人尝试做同样的事情,但没有成功。

以下是我的函数中的搜索之一(重复 12 次,各种搜索词被传递给 ContactsApp.getContactsByEmailAddress)。我添加了try-catch 块,因为脚本在各种删除循环期间似乎无缘无故地抛出错误。

不胜感激。

var apa = ContactsApp.getContactsByEmailAddress('apa.')
  try{
for (var i in apa) {
apa[i].deleteContact()
}
  } catch(e){
      Logger.log(e)
}

【问题讨论】:

标签: google-apps-script google-contacts-api


【解决方案1】:

使用Contacts Service,您只能使用一个搜索条件。因此,搜索多个模式的唯一方法是使用每个搜索参数调用一次该方法。幸运的是,您可以使用标准的编程实践来最大限度地减少重复代码的数量:

function getContactsWithEmails(emailSearchCriteria) {
  if (!emailSearchCriteria || !emailSearchCriteria.length)
    throw new Error("No search inputs given");

  // Collect the results of each search into a single Array.
  const matches = [];
  emailSearchCriteria.forEach(function (email) {
    var results = ContactsApp.getContactsByEmailAddress(email);
    if (results.length)
      Array.prototype.push.apply(matches, results);
    else
      console.log({message: "No results for search query '" + email + "'", query: email, resultsSoFar: matches});
  });

  return matches;
}

function deleteContacts(arrayOfContacts) {
  if (!arrayOfContacts || !arrayOfContacts.length)
    throw new Error("No contacts to delete");

  arrayOfContacts.forEach(function (contact) {
    ContactsApp.deleteContact(contact);
  });
}

// Our function that uses the above helper methods to do what we want.
function doSomething() {
  // Define all email searches to be performed.
  const emailFragmentsToSearchWith = [
    "email1",
    ...
    "emailN"
  ];
  const matchingContacts = getContactsWithEmails(emailFragmentsToSearchWith);
  if (matchingContacts.length) {
    /** do something with the contacts that matched the search.
     * someMethodThatSavesContacts(matchingContacts);
     * someMethodThatModifiesContacts(matchingContacts);
     * deleteContacts(matchingContacts);
     * ...
     */
  }
  /** do other stuff that doesn't need those contacts. */
}

this SO question 中提到的 Google Calendar v3 GData API 确实支持 multiple query parameters。但是,与此 API 没有简单的集成 - 您需要编写适当的 URL 请求并使用UrlFetchApp 执行它们。

除了 Google 联系人 API,您还可以使用 Google People REST API,特别是 people.connections#list 端点。

这两个 API 都要求您将您的 Apps 脚本项目与启用了相应 API 的 Google Cloud 项目相关联,并且可能需要您在其清单文件中手动设置您的 Apps 脚本项目所需的范围,此外为您向 API 端点发出的相关 HTTP 请求提供 OAuth2 授权。

参考文献

【讨论】:

  • 感谢您的彻底回复。我认为我无法处理日历 API 或人员 REST API,但我一定会尝试您的建议来改进我的删除功能。为我的无知道歉,但我对一些事情感到困惑,如果你能这么好,一个快速的解释会对我很有帮助。在第 12 行,为什么没有将变量传递给循环“emailsearches”?我想我不明白功能(电子邮件)部分。另外,在第 19 行,我希望输入变量匹配......我不明白 arrayOfContacts 或“联系人”是如何工作的。
  • @ReD_Cl0uD 我对forEach 方法文档的回答中的链接应该非常有用。在我的答案中包含的示例中,不需要绑定对传递给未命名回调函数的第二个、第三个或第四个参数的引用。您还应该查看有关变量范围的示例和教程(尽管重要的是要记住,Apps 脚本使用的 JavaScript 版本比您可能找到的许多交互式在线资源要旧)。
  • 我实际上是在写完之前的评论后立即开始查看该链接的。非常感谢。您有什么资源可以推荐以进一步阅读您提到的序列化技术吗?
  • @ReD_Cl0uD 这只是一个抽象的想法。如果收集操作“昂贵”,则将结果保存为“便宜”的收集可能是明智的。也许您将联系人存储在电子表格、属性服务、缓存等中。
  • 明白了。您希望您提供的代码能够正常工作吗?当我按顺序运行这些函数时,deleteContacts 函数在 0 秒内运行,实际上并没有删除任何联系人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多