Google apps script 如何使用appscript将联系人照片添加到google联系人
使用appscript创建或更新google联系人有很好的文档记录。但是,我在appscript中找不到有关如何向联系人添加照片的信息 情况: 包含以下内容的google工作表行:名称、地址、ImgSrcUrl 每个都在一个单独的列中 如何创建联系人并向其添加照片?下面是源代码片段:Google apps script 如何使用appscript将联系人照片添加到google联系人,google-apps-script,photo,google-contacts-api,Google Apps Script,Photo,Google Contacts Api,使用appscript创建或更新google联系人有很好的文档记录。但是,我在appscript中找不到有关如何向联系人添加照片的信息 情况: 包含以下内容的google工作表行:名称、地址、ImgSrcUrl 每个都在一个单独的列中 如何创建联系人并向其添加照片?下面是源代码片段: /******************************************************* */ function addContact() { /**********************
/******************************************************* */
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
非常感谢我相信你的目标如下
- 您想将照片添加到现有联系人中。
- 我以为
你认为可能是一张联系人照片
封面照片
- 我以为
- 您希望使用谷歌应用程序脚本实现这一点
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); } }