每当我通过Google日历用户界面添加访客时,都会弹出以下对话框:
如果我选择“发送”,它会向用户发送格式正确的电子邮件,并提供响应日历事件的选项:
当您手动使用Google日历用户界面时,这可以完美运行。问题是我正在尝试使用Google Apps脚本自动将人员添加到活动中。
我是通过使用addGuest()以编程方式将guest虚拟机添加到CalendarEvent:
event.addGuest("[email protected]");
但是,似乎没有发送电子邮件的选项。
我能找到的最接近的是,当你programmatically create an event时,你可以设置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]"});
Logger.log(event.getId());
这会正确发送格式正确的电子邮件。
但是,sendInvites
选项仅在新创建事件时有效。如果以后我打电话:
var event = CalendarApp.getEventById("insert_id_here");
event.addGuest("[email protected]");
...它不会发送电子邮件更新(即使事件是使用sendInvites=true
创建的)。这意味着sendInvites
是暂时的,不会持续存在。
有什么办法让sendInvites
在赛事上打电话给addGuest()
吗?例如。 event.addGuest("[email protected]", {sendInvite: true})
?
是否有任何其他解决方法可以生成在上面的UI中点击“发送”按钮时发送的相同电子邮件?
注意:我不想使用MailApp发送电子邮件,因为它的格式不会像Google自动发送的那样(例如嵌入式日历邀请,与会者,说明,响应链接等等)。
更新:
sendInvites
密钥,但事实并非如此。即使对于sendInvites
设置为true的事件,它也是空的。正如tehhowch所提到的,关键是你必须使用Advanced Google API。但是,this page上的文字说:
这会向与会者发送邀请电子邮件,并将活动放在他们的日历上。
......非常误导。它发送电子邮件的唯一方法是将sendUpdates
设置为all
以获取请求。
首先,您可以验证sendUpdates
是通过高级API发送新事件电子邮件的关键部分:
primary
sendUpdates:all
请求机构:
{
"start": {
"date": "2029-04-04"
},
"end": {
"date": "2029-04-04"
},
"summary": "Test",
"attendees": [
{
"email": "[email protected]"
}
]
}
[email protected]
更改为您的电子邮件地址。
请注意,这不是您登录时的确切电子邮件地址。否则,脚本会注意到您尝试向自己发送电子邮件而不发送。相反,如果您有Google电子邮件帐户,则应使用plus address。sendUpdates
很重要,那就把它设置为空白而不是all
。发送邀请。请注意,它没有到达您的收件箱。如果它不起作用,请确保遵循以下警告:
sendUpdates
设置为all
。既然最初的问题已经说明了如何使用标准的CalendarApp
来完成我们上面所做的事情,那么我们还没有取得多少成就。既然我们知道sendUpdates
很重要,那么现在是时候考虑添加一个客人了。
正如最初的问题所指出的那样,在使用sendInvite
时,无法指定sendUpdates
(就像我们使用标准Calendar API那样)或CalendarApp
(就像我们使用高级Calendar API一样)。我们唯一的方法是addGuest()。
幸运的是,我们可以使用类似于我们上面所做的高级日历API,并向事件发送更新(也称为PATCH)。我们可以使用Google围绕高级API(又名Calendar
)的好包装,而不是与原始JSON和高级日历API接口。
但是,由于我们使用的是advanced Calendar API,我们必须遵守这一说明:
注意:这是一项必须在使用前启用的高级服务。
这意味着在进入代码之前有一些先决条件:
This script has an Apps Script-managed Cloud Platform project.
,congratulations, you're done with this step!
如果您看到This script is currently associated with project:
,请执行以下操作:
点击This script is currently associated with project:
下面的链接
这将带您进入Google云端平台上的当前项目。
API和服务>仪表板
启用API和服务
添加Google Calendar API
点击“启用”按钮。
您可以放心地忽略警告:To use this API, you may need credentials. Click 'Create credentials' to get started.
如果上述两种方法都失败了,您可以尝试使用核路径并重新创建电子表格/ doc / script。有了这些先决条件,您现在可以创建一个函数:
// 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);
}
一些警告:
@google.com
。get
才能获得当前的被邀请者列表。如果不这样做,您将意外删除最初在活动中的所有人。有关更多信息,请参阅this answer。
注意:patch
很聪明,只会向实际添加的人发送电子邮件更新。例如。如果活动中的当前人员是[alice, bob]
,并且您发送了[alice, bob, charlie]
的补丁,则只有charlie
会收到一封电子邮件。这正是你想要的!同样,关键是知道如何将sendUpdates
传递给insert()
方法。我找不到有关补丁方法1的任何文档,但自动完成显示有一个可以添加的optionalArgs
参数。我随机尝试了你上面看到的格式,它有效!
如果它不起作用,请务必阅读上面的所有警告。
注意:我很高兴这与谷歌的API有关,因为如果没有,可能意味着需要手动发送电子邮件,并查看how to add calendar metadata to a message so that Gmail parses it properly。
1我们有general patch API,但这仅在发送原始JSON时有用。我们正在使用Google's Calendar
wrapper,它没有完全相同的地图。