在 CalendarEvent 上调用 addGuest 时如何发送标准邀请电子邮件?

2024-02-23

每当我通过 Google 日历 UI 添加访客时,都会弹出以下对话框:

如果我选择“发送”,它会向用户发送一封格式良好的电子邮件,并提供响应日历事件的选项:

当您手动使用 Google 日历 UI 时,此功能可以完美运行。问题是我正在尝试使用 Google Apps 脚本自动将人员添加到活动中。

我通过使用以编程方式将客人添加到 CalendarEvent添加来宾() https://developers.google.com/apps-script/reference/calendar/calendar-event#addGuest(String):

event.addGuest("[email protected] /cdn-cgi/l/email-protection");

但是,似乎没有发送电子邮件的选项。

我能找到的最接近的是当你以编程方式创建事件 https://developers.google.com/apps-script/reference/calendar/calendar#createEvent(String,Date,Date,Object),你可以设置sendInvites:

  var event = CalendarApp.getDefaultCalendar().createEvent(
    'Test',
    new Date('April 5, 2029 20:00:00 UTC'),
    new Date('April 5, 2029 21:00:00 UTC'),
    {sendInvites: true, guests:"[email protected] /cdn-cgi/l/email-protection"});
  Logger.log(event.getId());

这会正确发送一封格式良好的电子邮件。

但是,那sendInvites选项仅在新创建事件时才有效。如果稍后我打电话:

  var event = CalendarApp.getEventById("insert_id_here");
  event.addGuest("[email protected] /cdn-cgi/l/email-protection");

...它不会发送电子邮件更新(即使该事件是使用sendInvites=true)。这意味着sendInvites是暂时的并且不会持续存在于事件中。

有什么办法可以sendInvites打电话时addGuest()在一个活动上?例如。event.addGuest("[email protected] /cdn-cgi/l/email-protection", {sendInvite: true})?

是否有其他解决方法可以生成与点击上面 UI 中的“发送”按钮时发送的电子邮件相同的电子邮件?

注:我愿意not想要使用 MailApp 发送电子邮件,因为它的格式不像 Google 自动发送的那样良好(例如,嵌入日历邀请、与会者、描述、回复链接等)。


Update:

  • 我刚看到获取所有标签键 https://developers.google.com/apps-script/reference/calendar/calendar-event#getAllTagKeys()在 API 中,并希望我可以使用它来获取sendInvites关键,但事实并非如此。即使对于已经发生的事件,它也是空的sendInvites设置为 true。

正如 tehhowch 所说,关键是你must为此,请使用高级 Google API。然而,文字上这一页 https://developers.google.com/calendar/concepts/sharing#inviting_attendees_to_events说的是:

这会向与会者发送一封邀请电子邮件,并将该活动放在他们的日历上。

……很有误导性。发送电子邮件的唯一方法是您设置sendUpdates to all对于请求。


首先,您可以验证sendUpdates是发送电子邮件的关键部分new通过高级 API 的事件:

  1. Go to 这个链接 https://developers.google.com/calendar/v3/reference/events/insert?apix_params=%7B%22calendarId%22%3A%22primary%22%2C%22sendUpdates%22%3A%22all%22%2C%22resource%22%3A%7B%22start%22%3A%7B%22date%22%3A%222029-04-04%22%7D%2C%22end%22%3A%7B%22date%22%3A%222029-04-04%22%7D%2C%22summary%22%3A%22Test%22%2C%22attendees%22%3A%5B%7B%22email%22%3A%22your.email%2B1%40example.com%22%7D%5D%7D%7D#try-it.
  2. Hit the 现在就试试按钮向上。

    这应该会向您显示一个填充了以下信息的表单:

    • 日历ID:primary
    • 发送更新:all
    • 请求正文:

      {
        "start": {
          "date": "2029-04-04"
        },
        "end": {
          "date": "2029-04-04"
        },
        "summary": "Test",
        "attendees": [
          {
            "email": "[email protected] /cdn-cgi/l/email-protection"
          }
        ]
      }
      
  3. 改变[email protected] /cdn-cgi/l/email-protection到您的电子邮件地址。

    请注意,这不能是您登录时使用的确切电子邮件地址。否则,脚本会注意到您正在尝试向自己发送电子邮件,但不会发送。相反,如果您有 Google 电子邮件帐户,则应该使用加地址 https://gmail.googleblog.com/2008/03/2-hidden-ways-to-get-more-from-your.html反而。

  4. 点击执行按钮。

    授予权限后,您的收件箱将收到一封电子邮件,其中包含完全填充的日历事件,就像使用 Google 日历的 UI 时发送的一样。

  5. 如果你想明确地证明这一点sendUpdates很重要,将其设置为空白而不是all。发送邀请。请注意,它没有到达您的收件箱。

如果它不起作用,请确保遵循这些操作caveats:

  1. 您不得向同一登录的 Google 帐户发送电子邮件。例如。要么使用加号地址,要么将其发送到其他帐户。
  2. 确保这件事sendUpdates被设定为all.
  3. 确保该事件发生在未来的某个时间。 Google 绝不会针对过去发送的活动邀请发送电子邮件。即使您手动使用 Google 日历的界面也是如此。它会询问您是否要向用户发送更新,您选择“是”,但由于该事件是过去的,因此它实际上不会发送电子邮件。

由于原来的问题已经展示了如何使用该标准CalendarApp就我们上面所做的事情而言,我们还没有取得多大成就。现在我们知道了sendUpdates不过,这很重要,是时候考虑添加一位客人了。

正如最初的问题所指出的,没有办法指定sendInvite(正如我们将与standard日历 API)或sendUpdates(正如我们将与advanced日历API)使用时CalendarApp。我们唯一的方法是添加来宾() https://developers.google.com/apps-script/reference/calendar/calendar-event#addGuest(String).

幸运的是,我们可以使用与上面类似的高级日历 API,并向事件发送更新(也称为 PATCH)。我们可以使用 Google 围绕高级 API 的漂亮包装器(又名Calendar).

但是,由于我们使用的是高级日历 API https://developers.google.com/apps-script/advanced/calendar,我们必须遵守这个注意事项:

注意:这是一项高级服务,必须在使用前启用。

这意味着有一些先决条件在进入代码之前:

  1. 首先,在您的 Cloud Platform 项目中启用 Google Calendar API:

    1. 在脚本中,导航到资源 > 云平台项目...
    2. 执行以下操作之一:

      • 如果你看到This script has an Apps Script-managed Cloud Platform project., 恭喜,您已完成此步骤 https://stackoverflow.com/a/55805258/35690!
      • 如果你看到This script is currently associated with project:, 请执行下列操作:

        1. 点击下面的链接This script is currently associated with project:

          这将带您进入 Google Cloud Platform 上的当前项目。

        2. API 和服务 > 仪表板
        3. 启用 API 和服务
        4. 添加谷歌日历API
        5. 点击启用按钮。
        6. 您可以安全地忽略该警告:To use this API, you may need credentials. Click 'Create credentials' to get started.
      • 如果上述两种方法都失败,您可以尝试核路线并重新创建电子表格/文档/脚本。
  2. 接下来,为脚本启用 Google Calendar API:

    1. 返回脚本,资源 > 高级 Google 服务
    2. 点击 Google Calendar API 旁边的“打开”。

      您现在已准备好在脚本中使用高级 API。

满足这些先决条件后,您现在可以创建一个函数:

// Note: requires advanced API
function addGuestAndSendEmail(calendarId, eventId, newGuest) {
  var event = Calendar.Events.get(calendarId, eventId);
  var attendees = event.attendees;
  attendees.push({email: newGuest});

  var resource = { attendees: attendees };
  var args = { sendUpdates: "all" };

  Calendar.Events.patch(resource, calendarId, eventId, args);
}

Some caveats:

  1. 当您传递 eventId 时,您需要删除@google.com在 eventId 的末尾。
  2. 你必须打电话get首先获取当前的受邀者列表。如果不这样做,您将意外删除最初参加活动的所有人员。看这个答案 https://stackoverflow.com/a/55518975/35690了解更多信息。

    Note: patch很聪明,只会向实际添加的人员发送电子邮件更新。例如。如果当前参加活动的人员是[alice, bob],然后你发送一个补丁[alice, bob, charlie], only charlie将收到一封电子邮件。这正是您想要的!

  3. All the 之前的警告在上面的列表中适用。

Again, the key is knowing how to pass in sendUpdates to the insert() method. I couldn't find any documentation about the patch method1, but the auto-complete revealed that there is an optionalArgs parameter that can be added. I randomly tried the format you see above, and it worked!

如果它不起作用,请确保您阅读全部内容caveats above.

注意:我很高兴这可以与 Google 的 API 配合使用,因为如果不能,则可能意味着需要手动发送电子邮件,并查看如何将日历元数据添加到邮件中以便 Gmail 正确解析它 https://developers.google.com/gmail/markup/getting-started.


1 We have the general patch API https://developers.google.com/calendar/v3/reference/events/patch, but this is only useful when sending the raw JSON. We're using Google's Calendar wrapper https://developers.google.com/apps-script/advanced/calendar, which doesn't map exactly the same.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 CalendarEvent 上调用 addGuest 时如何发送标准邀请电子邮件? 的相关文章

随机推荐