微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用应用程序脚本轮询 Google 通讯录更改

如何解决使用应用程序脚本轮询 Google 通讯录更改

我需要检测我的通讯录中的变化,不幸的是 People API 没有提供这样的功能。我在一些博客上读过需要投票。我找不到任何 API 来轮询 https://developers.google.com/contacts/v3/reference 谁能建议我如何实现此功能

解决方法

如果您的联系人数量或联系人电子邮件数量发生变化,您可以使用此类功能接收和发送电子邮件。

function sendEmailIfContactChange() {
  const gObj = PropertiesService.getScriptProperties().getProperties();
  const contacts=ContactsApp.getContacts();
  var n=0;
  contacts.forEach((c)=>{c.getEmails().forEach((e)=>{n++;});});
  if(!gObj.hasOwnProperty('contacts')) {
    gObj.contacts=contacts.length;
    gObj.emails=n;
    PropertiesService.getScriptProperties().setProperties(gObj);
    return;
  } else {
    let pc = gObj.contacts;
    let pe = gObj.emails;
    gObj.contacts = contacts.length;
    gObj.emails = n;
    PropertiesService.getScriptProperties().setProperties(gObj);
    if(pc != contacts.length || pe != n) {
      GmailApp.sendEmail(gobj.globals.privateemail,'Contacts Have Change',`Old Contact: ${pc} New Contacts: ${contacts.length} \nOld Emails: ${pe} New Emails: ${n}`);
    }
  } 
}

您可以像这样创建轮询触发器:

function createPollingTrigger() {
  const ts = ScriptApp.getProjectTriggers().map(t=>t.getHandlerFunction());
  if(!~ts.indexOf('sendEmaiIfContactChange')) {
    ScriptApp.newTrigger('sendEmailIfContactChange').timeBased().everyHours(2).create();
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。