【问题标题】:How to sync google sheets database with google contacts如何将谷歌表格数据库与谷歌联系人同步
【发布时间】:2021-02-07 08:21:58
【问题描述】:

我有一个包含联系人的谷歌表格数据库

这是我的数据库示例

我需要使用 google 应用脚本将此联系人同步到我的 googlee 联系人

如果有新联系人上传

如果现有联系人更改了他的任何数据信息以在谷歌联系人中更新它

对这个问题有什么帮助吗?

-- 更新:

到目前为止,我已经让这个脚本工作了

 function myFunction() {
  // Sheet Variables Below
  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var sheet = ss.getSheetByName('xx')
  var headerRows = 1;
  var MaxRow = sheet.getLastRow();
  var dataRange = sheet.getDataRange();
  var data = dataRange.getValues();     // Read all data
  data.splice(0, headerRows);            // Remove header rows

  function addContact() {
    for (var i = 0; i < data.length; i++) {
      var row = data[i];
      var firstName = row[1];
      var lastName = row[2];
      var email = row[3];
      var phone = row[4];
      var group = row[5];
      var atiende = row[6];
      var address1 = row[7];
      var address2 = row[8];
      var address3 = row[9]
      var notes = row[10];
      var customfields = row[11];
      var status = row[12];
      var contactid = row[13];

      var contact = ContactsApp.createContact(firstName, lastName, email);
      contact.addPhone(ContactsApp.Field.MOBILE_PHONE, phone);
      contact.addAddress(ContactsApp.Field.HOME_ADDRESS, address1);
      contact.setNotes(address2 + ' ' + address3)
      contact.addCompany(atiende)

      var group = ContactsApp.getContactGroup("System Group: My Contacts");
      group.addContact(contact);
   
    }
  }
  addContact();    
}

我已经把数据结构改成了这个

这是为了将所有联系人上传到谷歌联系人

但我现在需要通过电话号码检查联系人是否存在,如果存在则更新它,如果没有更改则跳过它,否则我在谷歌联系人中有重复

我认为值得一提的是,这是一个自动生成的数据库,而不是我手动更改数据的东西

【问题讨论】:

  • 到目前为止,您为实现目标做了哪些工作?
  • 我已经编辑了,我现在有什么,谢谢

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


【解决方案1】:

您可以在使用事件对象向 yr ss 添加新行时更新联系人

警告:我是一个业余爱好者,但对 gscripter 很热情,所以请谨慎使用我提供的任何建议

function newContact(e) {
  
 var sht = e.source.getActiveSheet();
 var row = e.range.getRow();

 var drng = sht.getRange(row, 2, 1, 6).getValues();
 // drng is a single row 2D array
 // adjust indexes to suit 
  
    var first = drng[0][0];
    var surname = drng[0][1];
    var phone = drng[0][2];
    var email = drng[0][3];
    var consentDate = drng[0][5];
    var grp = 'group-name'; 
  
    //create contact
    var contact = ContactsApp.createContact(first, surname, email);
    var contactID = contact.getId();
          
    //add info via bug workaround ie getting the new contact via contactID 
    contact = ContactsApp.getContactById(contactID); 
    contact.addPhone('mobile', phone);    
    
    //update contact
    var group = ContactsApp.getContactGroup(grp);
    contact = contact.addToGroup(group);
}

【讨论】:

  • 喜欢警告片! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-09
  • 1970-01-01
  • 1970-01-01
  • 2016-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多