简体   繁体   中英

Google Apps Script: Trigger a script when Contacts change?

I'm trying to keep a Google Group in sync with my Google Contacts.

I have written a Google Apps script that copies the emails from my Contacts to a Google Group, but I need a way to trigger it.

Is there an event when a Contact is edited?

Here's my sync code:

function copyContactsToGroups() {
  var contacts = ContactsApp.getContacts();
 Logger.log("found " + contacts.length + " contacts");
 var groupEmail = '[my group email]';
 for(var i = 0; i < contacts.length; i++) {
   var contact = contacts[i];
 // Name
 if (contact.getFullName() == null || contact.getFullName().length == 0)
 {
  continue;
 }

 // Email
 emails = contact.getEmails();
 for( var j = 0; j < emails.length; j++) {
   var existing_member;
   try {
     existing_member = AdminDirectory.Members.get(groupEmail, emails[j].getAddress());
   }
   catch (e) {
     existing_member = null;
   }

   if (existing_member == null) {
     var key = {
       email: emails[j].getAddress(),
       role: 'MEMBER'
     };
     AdminDirectory.Members.insert(key, groupEmail);
     Logger.log("Added: " + emails[j].getAddress() + " for " + contact.getFullName());
   }
   else {
     Logger.log("Already present: " + emails[j].getAddress());
   }
  }
 }
}

no, there isnt a contacts change event for the contacts api. you need to poll for changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM