在CalendarEvent上调用addGuest时,如何发送标准邀请电子邮件? [英] How do I send the standard invitation email when calling addGuest on a CalendarEvent?
问题描述
每当我通过Google日历用户界面添加访客时,都会弹出以下对话框:
Whenever I add a guest via the Google Calendar UI, the following dialog pops up:
如果我选择发送",它会向用户发送格式正确的电子邮件,并且还可以选择响应日历事件:
If I choose "Send" it sends a nicely formatted email to the user with an option to respond to the calendar event too:
当您手动使用Google日历用户界面时,此功能可完美运行.问题是我正在尝试使用Google Apps脚本自动将人员添加到事件中.
This works flawlessly when you're manually using the Google Calendar UI. Problem is I'm trying to use Google Apps Scripts to automate adding people to events.
我正在通过使用
但是,似乎没有发送电子邮件的选项. However, there doesn't seem to be an option to send an email. The closest I could find is that when you programmatically create an event, you can set 这将正确发送格式正确的电子邮件. This sends a nicely formatted email correctly. 但是, However, the ...它不会发送电子邮件更新(即使该事件是使用 ... it does not send an email update (even though the event was created with 在事件上调用 Is there any way to 还有其他解决方法,它们会产生与在上面的UI中单击发送"按钮时发送的电子邮件相同的电子邮件吗? Are there any other workarounds that will produce the same email that is sent when hitting that "Send" button in the UI above? 注意:我不不想使用MailApp发送电子邮件,因为它的格式设置不如Google自动发送的格式(例如,带有嵌入式日历邀请,参与者,描述,响应)链接等等等). Note: I do not want to use MailApp to send an email because it won't be nicely formatted like the one Google automatically sends out (e.g. with embedded calendar invite, attendees, description, response links, etc, etc, etc). 更新: 正如tehhowch所述,关键是您必须为此使用高级Google API.但是,此页面上的文字为: As tehhowch mentioned, the key is you must use the Advanced Google API for this. However, the text on this page that says: 这会向参与者发送邀请电子邮件,并将事件放置在他们的日历上. This sends an invitation email to the attendees and places the event on their calendar. ...极具误导性.发送电子邮件的唯一方法是将请求的 ... is very misleading. The only way it'll send email is if you set 首先,您可以验证 First, you can verify that Hit the Try it now button up top. 这应该向您显示一个表格,其中填充了以下信息: This should show you a form with the following info populated: 请求正文: Request body:
将 Change the 请注意,这不能是您登录时使用的确切电子邮件地址.否则,脚本将注意到您正在尝试向自己发送电子邮件,而不是发送电子邮件.相反,如果您拥有Google电子邮件帐户,则应使用加地址代替. Note that this cannot be your exact email address that you are logged in with. Otherwise the script will notice that you're trying to send an email to yourself and not send it. Instead, if you have a Google email account, you should use a plus address instead. 点击执行"按钮. 授予权限后,您将收到一封电子邮件,其中将完全填充日历事件,就像使用Google日历的用户界面发送日历事件一样. After giving it permissions, you will receive an email to your inbox with the calendar event fully populated just as it is sent when using Google Calendar's UI. 如果它不起作用,请确保遵循以下腔室: If it's not working, make sure that you follow these caveats:
由于最初的问题已经说明了如何使用标准的
Since the original question already shows how to use the standard 正如原始问题所指出的那样,无法指定 As the original question points out, there is no way to specify 幸运的是,我们可以使用与上面类似的方式使用高级日历API,并向事件发送更新(也称为PATCH).不必与原始JSON和Advanced Calendar API交互,我们可以使用Google的高级API(又称为 Luckily, we can use the Advanced Calendar API similar to how we did above and send an update (aka PATCH) to the event. Rather than interfacing with raw JSON and the Advanced Calendar API, we can use Google's nice wrapper around the advanced API (aka 但是,由于我们使用的是高级日历API ,因此我们必须遵守此说明: However, since we're using the advanced Calendar API, we must abide by this note: 注意:这是一项高级服务,必须在使用前启用. Note: This is an advanced service that must be enabled before use. 这意味着在进入代码之前有一些先决条件: This means there are some prerequisites before jumping into code: 首先,在您的Cloud Platform项目中启用Google Calendar API: First, enable Google Calendar API in your Cloud Platform project: 执行下列操作之一: Do one of the following: 如果看到 If you see 单击 这将带您进入Google Cloud Platform上的当前项目. This will take you to the current project on Google Cloud Platform.
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:"your.email+1@example.com"});
Logger.log(event.getId());
sendInvites
选项仅在新创建事件时有效.如果以后再打给我:sendInvites
option only works when the event is newly created. If at a later time I call: var event = CalendarApp.getEventById("insert_id_here");
event.addGuest("your.email+2@example.com");
sendInvites=true
创建的).这意味着sendInvites
是瞬态的,不会在事件上持续存在.sendInvites=true
). This means sendInvites
is transient and doesn't persist on the event.addGuest()
时,是否有任何方法可以到达sendInvites
?例如. event.addGuest("user@example.com", {sendInvite: true})
?sendInvites
when calling addGuest()
on an event? E.g. event.addGuest("user@example.com", {sendInvite: true})
?
sendInvites
密钥,但事实并非如此.即使对于sendInvites
设置为true的事件,它也为空.
sendInvites
key, but that's not the case. It is empty even for events that have sendInvites
set to true.推荐答案
sendUpdates
设置为all
.sendUpdates
to all
for the request.sendUpdates
是通过高级API为 new 事件发送电子邮件的关键部分:sendUpdates
is the key piece to sending email for new events via the advanced API:
primary
all
primary
all
{
"start": {
"date": "2029-04-04"
},
"end": {
"date": "2029-04-04"
},
"summary": "Test",
"attendees": [
{
"email": "your.email+1@example.com"
}
]
}
your.email+1@example.com
更改为您的电子邮件地址.your.email+1@example.com
to your email address.
sendUpdates
设置为all
.
sendUpdates
is set to all
.
CalendarApp
来完成我们在上文中所做的工作,所以到目前为止,我们并没有取得太大的成就.现在,我们已经知道sendUpdates
很重要,现在该考虑添加一个来宾了.
CalendarApp
to do what we did above, we didn't accomplish much, yet. Now that we know that sendUpdates
is important, though, it's time to look at adding a guest.sendInvite
(就像我们使用 standard 日历API一样)或sendUpdates
(就像我们使用 advanced一样) 日历API)使用CalendarApp
时.我们唯一的方法是 addGuest().sendInvite
(as we would with the standard Calendar API) or sendUpdates
(as we would with the advanced Calendar API) when using CalendarApp
. The only method we have is addGuest().Calendar
)周围的包装器.Calendar
).
This script has an Apps Script-managed Cloud Platform project.
,恭喜,您已完成此步骤!This script is currently associated with project:
,请执行以下操作:
This script has an Apps Script-managed Cloud Platform project.
, congratulations, you're done with this step!This script is currently associated with project:
, do the following:
This script is currently associated with project:
接下来,为脚本启用Google Calendar API:
Next, enable Google Calendar API for the script:
- 返回脚本,资源>高级Google服务
-
在Google Calendar API旁边点击打开".
- Back in your script, Resources > Advanced Google services
Hit "On" next to Google Calendar API.
您现在可以在脚本中使用高级API.
You're now ready to use the advanced API in a script.
有了这些先决条件,您现在可以创建一个函数:
With those prerequisites out of the way, you can now create a function:
// 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);
}
一些洞穴:
- 传递eventId时,您需要在eventId的末尾删除
@google.com
. -
您必须先致电
get
才能获取当前的被邀请者列表.如果不这样做,您将不小心删除所有原本参与该活动的人员.有关更多信息,请参见此答案.
- When you pass an eventId, you will need to remove the
@google.com
at the end of the eventId. You must call
get
first to get the current list of invitees. If you don't, you'll accidentally remove all the people that were on the event originally. See this answer for more info.
注意:patch
很聪明,只会将电子邮件更新发送给实际添加的人员.例如.如果当前正在处理事件的人员是[alice, bob]
,并且您发送了[alice, bob, charlie]
补丁,则只有charlie
会收到电子邮件.这正是您想要的!
Note: patch
is smart and will only send email updates to the people that were actually added. E.g. if the current people on an event are [alice, bob]
, and you send a patch with [alice, bob, charlie]
, only charlie
will receive an email. This is exactly what you want!
同样,关键是要知道如何将sendUpdates
传递给insert()
方法.我找不到有关修补程序方法 1 的任何文档,但是自动完成功能显示可以添加optionalArgs
参数.我随机尝试了上面看到的格式,并且有效!
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!
如果它不起作用,请确保已阅读上面的所有腔室.
If it's not working, make sure you read all caveats above.
注意:我很高兴此功能可与Google的API一起使用,因为如果不支持,则可能意味着需要手动发送电子邮件,并调查
Note: I'm glad this worked with Google's API, because if it didn't, it likely meant needing to manually send the email(s), and look into how to add calendar metadata to a message so that Gmail parses it properly.
1 我们有常规补丁API ,但这仅在发送原始JSON时有用.我们使用的是 Google的Calendar
包装器,它不能准确地映射一样.
1 We have the general patch API, but this is only useful when sending the raw JSON. We're using Google's Calendar
wrapper, which doesn't map exactly the same.
这篇关于在CalendarEvent上调用addGuest时,如何发送标准邀请电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!