Google App Script Contact API 无法删除联系人

时间:2021-02-09 06:06:22

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

我正在 GSuite/Workspace 通讯录中清理我的联系人,因为 Gmail 收件人: 桌面和移动应用程序的自动完成列表中没有显示联系人。我在文本应用中也遇到了类似的问题,找不到联系人。

事实证明,如果有多个联系人具有相同电子邮件地址,这将导致自动完成无法显示/查找联系人(三个联系人有两个联系人,只有一个电子邮件地址和另一个全名和电子邮件地址)

Gmail 自动完成

Gmail autocomplete

我有 ~3200 个联系人,其中一些是

  • 具有相似联系信息的重复联系人
  • 只有一个电子邮件地址的重复联系人
  • 没有联系信息的联系人(没有姓名、没有电子邮件地址、没有电话)

大部分重复的原因是 Gmail 中的 "Create contacts for auto-complete" 功能。需要将其设置为 "I'll add contacts myself" 以免导致重复。

Add contacts myself

我尝试了联系人中的“合并和修复”功能,但没有找到所有需要合并的联系人。因此,我正在创建一个 Google Apps 脚本来修复重复的联系人。

但是我遇到了大约 200 个联系人的问题,这些问题导致我的脚本失败。我尝试重新标记并删除联系人,但服务器失败并显示

Error We're sorry, a server error occurred. Please wait a bit and try again.

当我尝试访问 ID、姓名、电子邮件等时,我从服务器收到不同的错误。这是一个示例

Error: Could not locate target object while calling method getId on object with id 979
Error: Could not locate target object while calling method getFamilyName on object with id 1344
Error: Could not locate target object while calling method getEmails on object with id 3279

这是我的脚本,它只是遍历每个组的所有联系人:

function main() {
  const allGroups = ContactsApp.getContactGroups()

  let counter = 0
  allGroups.forEach(group => {
    const contacts = group.getContacts()
    Logger.log('=====================\n' + group.getGroupName() + ' ' + contacts.length)
    contacts.forEach((contact, index) => {
      try {
        // After trying to execute a method on a contact, an error is generated
        // contact.getEmails()
        // contact.getId()
        contact.getFamilyName()
      } catch(err) {
        Logger.log(index + ' ' + err)
        /*
          deleteContact fails with 
          "We're sorry, a server error occurred. Please wait a bit and try again."
          ContactsApp.deleteContact(contact) 
        */
        counter++
      }
    })
  })
  Logger.log('Total count: ' + counter)
}

联系方式调用

11:38:53 PM Info    =====================
System Group: Family 0
11:38:53 PM Info    =====================
System Group: Friends 0
11:38:56 PM Info    =====================
System Group: My Contacts 3280
11:38:56 PM Info    233 Error: Could not locate target object while calling method getFamilyName on object with id 1344.
11:38:56 PM Info    234 Error: Could not locate target object while calling method getFamilyName on object with id 1345.
11:38:56 PM Info    243 Error: Could not locate target object while calling method getFamilyName on object with id 1346.
11:38:56 PM Info    244 Error: Could not locate target object while calling method getFamilyName on object with id 1347.
11:38:56 PM Info    304 Error: Could not locate target object while calling method getFamilyName on object with id 1348.
11:38:56 PM Info    308 Error: Could not locate target object while calling method getFamilyName on object with id 1349.
11:38:56 PM Info    311 Error: Could not locate target object while calling method getFamilyName on object with id 1350.
11:38:56 PM Info    313 Error: Could not locate target object while calling method getFamilyName on object with id 1351.
11:38:56 PM Info    316 Error: Could not locate target object while calling method getFamilyName on object with id 1352.

尝试删除联系人

11:48:15 PM Notice  Execution started
11:48:15 PM Info    =====================
~Error 0
11:48:16 PM Info    =====================
~On Hold 502
11:48:17 PM Info    =====================
Intake Form 781
11:48:17 PM Info    =====================
Intake Form Received 211
11:48:18 PM Info    =====================
Salesforce Sync 0
11:48:18 PM Info    =====================
Starred in Android 12
11:48:18 PM Info    =====================
System Group: Coworkers 0
11:48:18 PM Info    =====================
System Group: Family 0
11:48:18 PM Info    =====================
System Group: Friends 0
11:48:21 PM Info    =====================
System Group: My Contacts 3280
11:48:21 PM Info    634 Error: Could not locate target object while calling method getFamilyName on object with id 1526.
11:48:21 PM Info    Trying to delete user
11:48:21 PM Error   
We're sorry, a server error occurred. Please wait a bit and try again.

调试脚本截图

Debug info

我联系了 GSuite/Workspace 支持人员并被定向到这里。这似乎是通讯录中的一个错误,但我无法删除该联系人,也不知道我是如何获得这些无效联系人的。

  • 有没有办法让我摆脱这些无效的联系人?怎么样?
  • 有人了解这些无效联系人是如何生成的吗?

0 个答案:

没有答案
相关问题