问题描述
使用 appscript 创建或更新 google 联系人已有详细记录。但是,我没有找到有关如何在 appscript 中向联系人添加照片的信息。
情况: 一个谷歌表行:名称、地址、ImgSrcUrl 每个都在一个单独的列中。
/******************************************************* */
function addContact() {
/******************************************************* */
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getActiveCell();
var active_row = cell.getRow();
var range = sheet.getDatarange();
const CNAME = 3;
const CADDR = 4;
const CEMAIL = 5;
const CimgurL = 6;
var name = range.getCell(active_row,CNAME).getValue();
var addr = range.getCell(active_row,CADDR).getValue();
var email = range.getCell(active_row,CEMAIL).getValue();
var imgurl = range.getCell(active_row,CimgurL).getValue();
contact = ContactsApp.createContact(first_name,last_name,email); // create contact
非常感谢
解决方法
我相信你的目标如下。
- 您想将照片添加到现有联系人。
- 我认为您认为的
a contact-photo
可能是coverPhotos
。
- 我认为您认为的
- 您希望使用 Google Apps 脚本来实现这一目标。
虽然我使用Contact service检查了您的目标是否可以实现,但不幸的是,我找不到它。所以,在你的情况下,为了实现你的目标,我想建议使用 People API。当使用 People API 时,脚本如下。
示例脚本:
在使用此脚本之前,please enable People API at Advanced Google services。并且,请设置变量。
function myFunction() {
const familyName = "###";
const givenName = "###";
const emailAddress = "###";
const imageUrl = "###";
// 1. Create contact.
const resource1 = { emailAddresses: [{ value: emailAddress }],names: [{ familyName: familyName,givenName: givenName }] }
const resourceName = People.People.createContact(resource1).resourceName;
// 2. Add cover photo to the created contact.
const resource2 = { photoBytes: Utilities.base64Encode(UrlFetchApp.fetch(imageUrl).getContent()),personFields: "coverPhotos" };
People.People.updateContactPhoto(resource2,resourceName);
}
- 在这个脚本中,它假设您的图像 URL 是图像文件的直接链接。请注意这一点。
注意:
-
当您想将封面照片添加到现有联系人时,您也可以使用以下脚本。在此示例脚本中,使用电子邮件地址搜索现有联系人。
function myFunction2() { const emailAddress = "###"; const imageUrl = "###"; // 1. Search contact using the email address. const contacts = People.People.searchContacts({ query: emailAddress,readMask: "emailAddresses,names" }).results.filter(c => c.person.emailAddresses.map(m => m.value).includes(emailAddress)); // 2. Add cover photo to the retrieved contact. if (contacts.length > 0) { const resource2 = { photoBytes: Utilities.base64Encode(UrlFetchApp.fetch(imageUrl).getContent()),personFields: "coverPhotos" }; People.People.updateContactPhoto(resource2,contacts[0].person.resourceName); } }