问题描述
我需要检测我的通讯录中的变化,不幸的是 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();
}
}