通过Apps脚本添加的Google联系人无法按预期工作

问题描述 投票:0回答:1

我正在尝试第一篇谷歌应用程序脚本代码来创建一个新的联系人。目标是将来从谷歌表自动创建联系人并存储在“联系人”中,以便所有联系人同步到我的手机。但是,创建的联系人将在“其他联系人”下创建,而不是“联系人”。 “其他联系人”下可用的联系人不会自动同步到手机。我在做错误在哪里?

我尝试使用简单的代码在Google Apps脚本中添加联系人。 (参考文件)

function CreateContact() {
  var contact = ContactsApp.createContact('Rahul', 'Kumar', '[email protected]');
}

预期输出:在“联系人”下创建联系人

实际输出:在“其他联系人”下创建联系人

google-apps-script google-contacts
1个回答
0
投票

Add New Contacts to a Label

我发现如果您将它们添加到标签中,那么它们会很快添加到您的联系人中。所以这里有一个功能就是这样。

function addContact(first,last,email,label) {
  var label=label || 'New';//default label
  if(first && last && email && label) {
    var contact=ContactsApp.createContact(first,last,email);
    var allgroups=ContactsApp.getContactGroups();//gets all groups
    var grpnames=getGroupNamesArray();//Get all the names of your groups in an array
    var index=grpnames.indexOf(label);
    if(index==-1) {
      var grp=ContactsApp.createContactGroup(label);
    }else{
      var grp=allgroups[index];//if group is already there then use it
    }
    contact.addToGroup(grp);  
  }
  //var html=Utilities.formatString('<br />Add New Contact<br />First: %s, Last: %s Email: %s', contact.getGivenName(),contact.getFamilyName(),contact.getEmails()[0].getAddress());//debugging
  //var userInterface=HtmlService.createHtmlOutput(html);//debugging
  //SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Contact');/debugging
}

function getGroupNamesArray() {
  var allGrps=ContactsApp.getContactGroups();
  var allNames=[];
  for(var i=0;i<allGrps.length;i++) {
    allNames.push(allGrps[i].getName());
  }
  return allNames;
}

您可以添加以下联系人:

function testAddContact1(){addContact('Kenny','Corral','kenny @ missyou.com',null); // null取默认值。您可以将其更改为您想要的任何内容。 }

© www.soinside.com 2019 - 2024. All rights reserved.