正如 tehhowch 所说,关键是你must为此,请使用高级 Google API。然而,文字上这一页 https://developers.google.com/calendar/concepts/sharing#inviting_attendees_to_events说的是:
这会向与会者发送一封邀请电子邮件,并将该活动放在他们的日历上。
……很有误导性。发送电子邮件的唯一方法是您设置sendUpdates
to all
对于请求。
首先,您可以验证sendUpdates
是发送电子邮件的关键部分new通过高级 API 的事件:
- 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.
-
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"
}
]
}
-
改变[email protected] /cdn-cgi/l/email-protection
到您的电子邮件地址。
请注意,这不能是您登录时使用的确切电子邮件地址。否则,脚本会注意到您正在尝试向自己发送电子邮件,但不会发送。相反,如果您有 Google 电子邮件帐户,则应该使用加地址 https://gmail.googleblog.com/2008/03/2-hidden-ways-to-get-more-from-your.html反而。
-
点击执行按钮。
授予权限后,您的收件箱将收到一封电子邮件,其中包含完全填充的日历事件,就像使用 Google 日历的 UI 时发送的一样。
- 如果你想明确地证明这一点
sendUpdates
很重要,将其设置为空白而不是all
。发送邀请。请注意,它没有到达您的收件箱。
如果它不起作用,请确保遵循这些操作caveats:
- 您不得向同一登录的 Google 帐户发送电子邮件。例如。要么使用加号地址,要么将其发送到其他帐户。
- 确保这件事
sendUpdates
被设定为all
.
- 确保该事件发生在未来的某个时间。 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,我们必须遵守这个注意事项:
注意:这是一项高级服务,必须在使用前启用。
这意味着有一些先决条件在进入代码之前:
-
首先,在您的 Cloud Platform 项目中启用 Google Calendar API:
- 在脚本中,导航到资源 > 云平台项目...
-
执行以下操作之一:
-
接下来,为脚本启用 Google Calendar API:
- 返回脚本,资源 > 高级 Google 服务
-
点击 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:
- 当您传递 eventId 时,您需要删除
@google.com
在 eventId 的末尾。
-
你必须打电话get
首先获取当前的受邀者列表。如果不这样做,您将意外删除最初参加活动的所有人员。看这个答案 https://stackoverflow.com/a/55518975/35690了解更多信息。
Note: patch
很聪明,只会向实际添加的人员发送电子邮件更新。例如。如果当前参加活动的人员是[alice, bob]
,然后你发送一个补丁[alice, bob, charlie]
, only charlie
将收到一封电子邮件。这正是您想要的!
- 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.