【问题标题】:How to add a contact-photo to google-contact with appscript如何使用 appscript 将联系人照片添加到 google-contact
【发布时间】:2021-04-30 09:00:17
【问题描述】:

使用 appscript 创建或更新 google 联系人已有详细记录。但是,我没有找到有关如何在 appscript 中向联系人添加照片的信息。

情况: 一个谷歌表格行:姓名、地址、ImgSrcUrl 每个都在一个单独的列中。

如何创建联系人并向其添加照片?源码sn-p如下:

/******************************************************* */
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

非常感谢

【问题讨论】:

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


    【解决方案1】:

    我相信你的目标如下。

    • 您想将照片添加到现有联系人。
      • 我认为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);
          }
        }
      

    参考资料:

    【讨论】:

    • 非常感谢田池。我测试了代码并使用了几个站点/图像。它完成了这项工作。但是,我遇到了一个特定站点,其中 UrlFetchApp.fetch(img503.jpg) 生成了 503 错误响应(请注意,在 wget img503.jpg 可以正常使用相同的图像)。我可能会将此作为后续问题发布。
    • @skaleio 感谢您的回复。我很高兴你的问题得到了解决。关于你的新问题,当我看到你的新帖子时,我想检查一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 2011-01-23
    • 1970-01-01
    相关资源
    最近更新 更多