【问题标题】:Google contacts addition via Apps Script not working as expected [duplicate]通过 Apps 脚本添加的 Google 联系人无法按预期工作 [重复]
【发布时间】:2020-07-31 01:57:18
【问题描述】:

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

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

function CreateContact() {
  var contact = ContactsApp.createContact('Rahul', 'Kumar', 'rahul.kumar@gmail.com');
}

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

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

【问题讨论】:

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


    【解决方案1】:

    将新联系人添加到标签

    我发现,如果您将它们添加到标签中,它们会很快添加到您的联系人中。所以这里有一个函数可以做到这一点。

    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;
    }
    

    您可以像这样添加联系人:

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

    【讨论】:

      【解决方案2】:

      我们需要将它添加到我的联系人组 var mainGroup = ContactsApp.getContactGroup("系统组:我的联系人");

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多