使用人员 API 更新联系人组

Update a contact group by using a People API

您好,我需要更新已创建的联系人并从所有先前分配的组中删除。在此示例中,创建了一个联系人,稍后将其移至 Carpenter,现在开始实际问题

如何将已创建的联系人移动到其他组。也就是说,我需要将联系人移至 BLACKSMITHS 组(如果不存在)创建 BLACKSMITHS 并分配 BLACKSMITHS 并从之前分配的 CARPENTERS

中删除
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
    ]
  }

  //HOW DO I MODIFY A CONTACT so that its must be removed from "CARPENTERS" group and ADDED TO "BLACKSMITHS" group
 
  
  //??????????????????


  
  People.ContactGroups.Members.modify(membersResource, groupResourceName);  
  return ContentService.createTextOutput("Success");
}


 

您只需要:

  1. 将联系人添加到新组 (BLACKSMITHS),就像您在共享代码中所做的那样。
  2. 使用 contactGroups.members.modify 上的 resourceNamesToRemove 从上一组 (CARPENTERS) 中删除联系人。

查看下面的示例。

代码片段:

// 1. Create Blacksmiths group if doesn't exist, add contact to group:
var groupNameToAdd = "BLACKSMITHS";
var groupToAdd = groups.find(group => group["name"] === groupNameToAdd);
if (!groupToAdd) { // Create group if doesn't exist
  var groupToAddResource = {
    contactGroup: {
      name: groupNameToAdd
    }
  }
  groupToAdd = People.ContactGroups.create(groupToAddResource);
}
var groupToAddResourceName = groupToAdd["resourceName"];
var membersToAddResource = {
  "resourceNamesToAdd": [
    contactResourceName
  ]
}
People.ContactGroups.Members.modify(membersToAddResource, groupToAddResourceName);
// 2. Remove contact from Carpenters:
var groupNameToRemove = "CARPENTERS";
var groupToRemove = groups.find(group => group["name"] === groupNameToRemove);
if (groupToRemove) { // Check that group exists
  var groupToRemoveResourceName = groupToRemove["resourceName"];
  var membersToRemoveResource = {
    "resourceNamesToRemove": [
      contactResourceName
    ]
  }
  People.ContactGroups.Members.modify(membersToRemoveResource, groupToRemoveResourceName);
}