从Google通讯录中删除重复或重复的联系人 [英] Delete duplicated or multiplied contacts from Google Contacts

查看:107
本文介绍了从Google通讯录中删除重复或重复的联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是制作一个包含客户的电子表格,其中包含他们的联系信息,地址和便笺,以创建新的Google联系人条目,并将联系人ID和"addtedAlready"回退到工作表中,以将已输入到Google联系人的联系人标记为已成功添加到工作表中".一切正常,问题只在于在Google通讯录中同一人的多个错误条目,因为每升级一次联系人,都会输入新条目.

My goal was to make a spreadsheet of customers with their contact information, addresses, and notes that creates new Google Contacts entries and pulls back contact id and "addedAlready" on to the sheet which marks contacts already entered to Google Contacts with "ADDED" on the sheet, which I successfully did. Everything works fine, problem is only with multiple wrong entries of same person in Google Contacts, because whenever the contact is upgraded, it makes new entry.

function createContact() {
  var alreadyAdded = "ADDED";
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
  var startRow = 5;  // First row of data to process
  var numRows = sheet.getRange('J2').getValue(); // Last row of data to process

  // Fetch the range of cells A5:J
  var dataRange = sheet.getRange(startRow, 1, numRows, 10)

  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
    for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var addedAlready = row[0]
    var firstName = row[1]
    var lastName = row[2]
    var company = row[3]  
    var phone = row[4]
    var notes = row[5]
    var address = row[6]
    var email = row[7];

      if (addedAlready != alreadyAdded) {
      // Create contact in Google Contacts and retrieve ID
      var contact = ContactsApp.createContact(firstName, lastName, phone);
      var group = ContactsApp.getContactGroup("System Group: My Contacts");
      group.addContact(contact);
      var id = contact.getId();

      // Add values to new contact
      contact.addCompany(company, "");
      contact.addPhone(ContactsApp.Field.WORK_PHONE, phone);
      contact.setNotes(notes);
      contact.addAddress(ContactsApp.Field.WORK_ADDRESS, address);
      contact.addEmail(ContactsApp.Field.WORK_EMAIL, email);
      sheet.getRange(startRow + i, 1).setValue(alreadyAdded);
      sheet.getRange(startRow + i, 9).setValue(id);
      }; 
    };

};

下面还有"onEdit"脚本,该脚本删除了已添加"状态并允许更新联系人,或者换句话说,在Google通讯录中将其翻倍.

Below also "onEdit" script which removes "ADDED" status and enables the contact to be updated or in another words, doubled in Google Contacts.

function onEdit(e){
  var sh=e.range.getSheet();
  if(sh.getName()=="Sheet1" && e.range.columnStart==2,4,5,6 && e.range.rowStart>4) {
    var sh2=e.source.getSheetByName("Sheet2");
    sh2.getRange(e.range.rowStart,1,e.range.rowEnd-e.range.rowStart+1,1).clearContent()  
  }
} 

为解决和消除这个问题,我现在请大家问,因为我在这里完全迷失了,如果有人可以帮助我创建一个脚本,该脚本将从Google联系人中检索所有现有的联系人ID,并将其与连接ID进行比较"I"列,并删除ID与"I"列中的ID不匹配的Google通讯录中的所有条目.

To solve and eleminate this problem I will now kindly ask you guys, because I'm completely lost here, if anyone can help me create a script that would retrieve all existing contact IDs from Google contacts and compare them with conatct ID's in the "I" column and delete all entries in Google Contacts which ID doesnt match to the ID's in the "I" column.

任何帮助将不胜感激!非常感谢!

Any help would be greatly appreciated! Many thanks!

推荐答案

如果我对您的理解正确:

If I understand you correctly:

  • 您在 I 列中具有不同的联系人ID.
  • 您要从其 id 不在 I 列中的Google联系人中删除任何联系人.
  • You have different contact ID's in column I.
  • You want to delete any contact from Google Contacts whose id is not in column I.

如果是这种情况,那么您可以这样做:

If that's the case, then you can do this:

function deleteContacts() {
  var contactIds = ContactsApp.getContacts().map(function(contact) {
    return contact.getId(); // Get current contact ids
  });
  var sh = SpreadsheetApp.getActive().getSheetByName("Sheet1"); // Please change accordingly
  var currentIds = sh.getRange("I:I").getValues().map(function(value) {
    return value[0];
  }).filter(function(val) {
    return val != "";
  })
  for (var i = 0; i < contactIds.length; i++) {
    if (currentIds.indexOf(contactIds[i]) == -1) {
      var contact = ContactsApp.getContactById(contactIds[i]);
      ContactsApp.deleteContact(contact);
    }
  }
}

此脚本执行以下操作:

  • Get the id's of all the current Contacts (variable contactIds) with getContacts().
  • Get all the values in column I of Sheet1 (variable currentIds).
  • For each id in Contacts, check if it exists in column I via indexOf(). If that's not the case, remove the contact (first retrieve it via getContactById(id) and then delete it via deleteContact(contact)).

我希望这会有所帮助.

这篇关于从Google通讯录中删除重复或重复的联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆