简体   繁体   English

使用 google 应用程序脚本通过 people api 将创建的联系人添加到组

[英]add a created contact to a group by people api using google apps script

I need to create a new group "Carpenters" (if the group doesn't exist) and add the created contact to "Carpenters" group我需要创建一个新组“Carpenters”(如果该组不存在)并将创建的联系人添加到“Carpenters”组

I have tried with我试过

function doGet(e) {
  var id = People.People.createContact(
  {
        "names": [
          {
            "displayNameLastFirst": "Smith Jefferson Jones",
            "familyName": "Jones",
          }
        ],
       /* "phoneNumbers": [
            {
                'value': "+12345679962"
            }
        ],
        "emailAddresses": [
            {
                'value': ' '
            }
        ]*/
    }
  ).metadata.sources[0].id;
  
 return ContentService.createTextOutput("Success");
}

You could do the following:您可以执行以下操作:

  1. Retrieve the resourceName of the created contact (to be used on step 4).检索已创建联系人的resourceName (将在步骤 4 中使用)。
  2. Check if the group exists by listing all contactGroups and looking for a group whose name is Carpenters (using find() ).通过列出所有contactGroups并查找nameCarpenters的组(使用find() )来检查该组是否存在。
  3. Create a contactGroup called Carpenters if it doesn't exist, using contactGroups.create .创建contactGroupCarpenters ,如果它不存在,使用contactGroups.create
  4. Use contactGroups.members.modify to add the created contact to the group.使用contactGroups.members.modify将创建的联系人添加到组中。

Code sample:代码示例:

function doGet(e) {
  // 1. CREATE CONTACT:
  var contactResource = {
    "names": [{
      "displayNameLastFirst": "Smith Jefferson Jones",
      "familyName": "Jones",
    }],
    /* "phoneNumbers": [{
      'value': "+12345679962"
    }],
    "emailAddresses": [{
      'value': ' '
    }]*/
  }
  var contactResourceName = People.People.createContact(contactResource)["resourceName"];
  // 2. CHECK IF GROUP EXISTS:
  var groupName = "Carpenters";
  var groups = People.ContactGroups.list()["contactGroups"];
  var group = groups.find(group => group["name"] === groupName);
  // 3. CREATE GROUP IF DOESN'T EXIST:
  if (!group) {
    var groupResource = {
      contactGroup: {
        name: groupName
      }
    }
    group = People.ContactGroups.create(groupResource);
  }
  var groupResourceName = group["resourceName"];
  // 4. ADD CONTACT TO GROUP:
  var membersResource = {
    "resourceNamesToAdd": [
      contactResourceName
    ]
  }
  People.ContactGroups.Members.modify(membersResource, groupResourceName);  
  return ContentService.createTextOutput("Success");
}

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Google Apps 脚本使用 People API 更新 Google 联系人照片 - Update google contact photo with People API using google apps script 在 Google Apps Script 中使用 Google People API 删除联系人返回 404 错误 - Deleting Contact using Google People API in Google Apps Script returns 404 error 如何使用 Apps Script 中的 People API 创建新的联系人组? - How do you create a new contact group with the People API in Apps Script? 使用 People API 更新联系人组 - Update a contact group by using a People API 如何使用Google Apps脚本获取用户创建的网上论坛信息 - How to fetch user created Group information using Google apps script 如何将Google应用脚本添加到使用API​​创建的电子表格中? - How can I add a Google apps script to a spreadsheet created using the API? 使用 Google Apps 脚本将用户添加到 Google 群组 - Add users to Google Group using Google Apps Script 将 Google 表格共享给特定人群的应用程序脚本 - Apps script that shares a google sheet to a specific group of people 使用 People API 将联系人添加到群组时出错 - Error when including a contact to group using People API 如何在Apps Script / HTML Service中使用Google Directory API创建人员搜索 - How to create a people search using Google Directory API in Apps Script/HTML Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM