编码级别:初级
时长:5 分钟
项目类型:通过自定义菜单和事件驱动型触发器实现自动化
目标
- 了解解决方案的用途。
- 了解 Apps 脚本服务在解决方案中的作用。
- 设置脚本。
- 运行脚本。
关于此解决方案
创建端到端事件注册系统。如果您有即将进行的活动(例如会议),则可以设置新的会议日历、创建报名表单,并自动向参与者发送电子邮件的个性化行程安排。

运作方式
此解决方案使用 Google 表格中的自定义菜单来实现自动事件注册系统。该脚本会使用 Google 表格电子表格中列出的会议活动创建日历。然后,脚本会创建一个表单,其中包含参加者可以报名参加的活动的列表。参加者填写表单后,脚本会将参加者添加到日历活动,并通过电子邮件向其发送行程。
Apps 脚本服务
此解决方案使用以下服务:
- 电子表格服务 - 向其他服务提供事件信息。
- 日历服务 - 为活动创建新日历,向日历添加活动,以及将参加者添加到他们注册的活动中。
- Properties service - 存储日历服务创建的日历 ID。当用户从自定义会议菜单中点击设置会议时,属性服务会检查日历 ID 属性是否存在,从而检查事件注册系统是否已设置。这样做有助于避免创建重复的表单和日历。
- 表单服务 - 根据电子表格中的信息创建表单,让参加者能够注册参加会议。
- 脚本服务 - 创建在参加者填写表单时触发的触发器。
- 文档服务 - 获取参加者注册的活动的活动信息,并将活动列表添加到新文档中。脚本会授予参加者编辑文档的权限。
- 邮件服务 - 通过电子邮件向参加者发送行程文档。
前提条件
如需使用此示例,您需要满足以下前提条件:
- Google 帐号(Google Workspace 帐号可能需要管理员批准)。
- 一个能够访问互联网的网络浏览器。
设置脚本
- 点击以下按钮创建 Create a sign-up for sessions at a meeting(创建会议中会话的注册)示例电子表格。此解决方案的 Apps 脚本项目已附加到电子表格中。
复制
- 依次点击会议 > 设置会议。您可能需要刷新页面,才能看到此自定义菜单。
出现提示时,为脚本授权。
如果 OAuth 同意屏幕显示 This app is not verify 警告,请依次选择 Advanced >
Go to {Project Name} (unsafe),以继续操作。
再次点击会议 > 设置会议。
运行脚本
- 依次点击 Tools(工具)> Manage Form
> 转到实时表单。
- 填写并提交表单。
- 前往 calendar.google.com。
- 确保在左侧,勾选会议日历旁边的复选框。
- 转到您报名的活动的日期,并确认您已添加为参加者。
(可选)重置解决方案
如果您想再次尝试此解决方案,或将其自定义为使用您自己的事件信息,则需要重置首次运行脚本时设置的一些项。如需查看重置解决方案的步骤,请点击下面的 Reset the solution(重置解决方案):
重置解决方案
第 1 步:重置存储的脚本属性
如果您尝试多次运行该脚本,系统会提示您“您的会议已经设置完毕。请在 Google 云端硬盘中找到您的注册表单!
之所以会发生这种情况,是因为创建会议日历后,日历 ID 会存储为脚本属性。脚本运行时,会检查日历 ID 属性是否已存在,如果属性已存在,则停止运行。
要移除现有的日历 ID 属性,请按以下步骤操作:
- 在电子表格中,依次点击扩展程序
> Apps 脚本。
- 在 Apps 脚本编辑器中,从函数下拉列表中选择
resetProperties
,然后点击运行。
第 2 步:删除会议日历
每次运行该脚本时,系统都会创建一个新日历。如果您不想保留创建的原始日历,请按以下步骤操作:
- 前往 calendar.google.com。
- 在“会议日历”旁边,依次点击“会议日历选项”图标
>
设置和共享。
- 滚动到设置的底部,然后点击删除。
每次运行该脚本时,该脚本都会为表单提交创建一个触发器。为避免多个触发器导致重复发送电子邮件,请移除原始触发器。请按以下步骤操作:
- 在电子表格中,依次点击扩展程序
> Apps 脚本。
- 在 Apps 脚本项目中,点击左侧的触发器图标
。
- 在该触发器旁边,依次点击“更多”图标
> 删除触发器。
每次运行该脚本时,系统都会创建一个新表单。要解除表单与您的电子表格的关联并将其删除,请按以下步骤操作:
- 在电子表格中,右键点击表单回复工作表,然后依次点击取消关联表单 > 确定。
- 再次右键点击表单回复工作表,然后依次点击删除
> 确定。
- 前往 forms.google.com。
- 右键点击会议表单,然后依次点击移除
> 移至回收站。
重置解决方案后,您可以添加自己的数据,或者继续使用示例数据并再次运行脚本。
查看代码
要查看此解决方案的 Apps 脚本代码,请点击下面的查看源代码:
查看源代码
Code.gs
// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/automations/event-session-signup
/*
Copyright 2022 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* Inserts a custom menu when the spreadsheet opens.
*/
function onOpen() {
SpreadsheetApp.getUi().createMenu('Conference')
.addItem('Set up conference', 'setUpConference_')
.addToUi();
}
/**
* Uses the conference data in the spreadsheet to create
* Google Calendar events, a Google Form, and a trigger that allows the script
* to react to form responses.
*/
function setUpConference_() {
let scriptProperties = PropertiesService.getScriptProperties();
if (scriptProperties.getProperty('calId')) {
Browser.msgBox('Your conference is already set up. Look in Google Drive for your'
+ ' sign-up form!');
return;
}
let ss = SpreadsheetApp.getActive();
let sheet = ss.getSheetByName('Conference Setup');
let range = sheet.getDataRange();
let values = range.getValues();
setUpCalendar_(values, range);
setUpForm_(ss, values);
ScriptApp.newTrigger('onFormSubmit').forSpreadsheet(ss).onFormSubmit()
.create();
}
/**
* Creates a Google Calendar with events for each conference session in the
* spreadsheet, then writes the event IDs to the spreadsheet for future use.
* @param {Array<string[]>} values Cell values for the spreadsheet range.
* @param {Range} range A spreadsheet range that contains conference data.
*/
function setUpCalendar_(values, range) {
let cal = CalendarApp.createCalendar('Conference Calendar');
// Start at 1 to skip the header row.
for (let i = 1; i < values.length; i++) {
let session = values[i];
let title = session[0];
let start = joinDateAndTime_(session[1], session[2]);
let end = joinDateAndTime_(session[1], session[3]);
let options = {location: session[4], sendInvites: true};
let event = cal.createEvent(title, start, end, options)
.setGuestsCanSeeGuests(false);
session[5] = event.getId();
}
range.setValues(values);
// Stores the ID for the Calendar, which is needed to retrieve events by ID.
let scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty('calId', cal.getId());
}
/**
* Creates a single Date object from separate date and time cells.
*
* @param {Date} date A Date object from which to extract the date.
* @param {Date} time A Date object from which to extract the time.
* @return {Date} A Date object representing the combined date and time.
*/
function joinDateAndTime_(date, time) {
date = new Date(date);
date.setHours(time.getHours());
date.setMinutes(time.getMinutes());
return date;
}
/**
* Creates a Google Form that allows respondents to select which conference
* sessions they would like to attend, grouped by date and start time in the
* caller's time zone.
*
* @param {Spreadsheet} ss The spreadsheet that contains the conference data.
* @param {Array<String[]>} values Cell values for the spreadsheet range.
*/
function setUpForm_(ss, values) {
// Group the sessions by date and time so that they can be passed to the form.
let schedule = {};
// Start at 1 to skip the header row.
for (let i = 1; i < values.length; i++) {
let session = values[i];
let day = session[1].toLocaleDateString();
let time = session[2].toLocaleTimeString();
if (!schedule[day]) {
schedule[day] = {};
}
if (!schedule[day][time]) {
schedule[day][time] = [];
}
schedule[day][time].push(session[0]);
}
// Creates the form and adds a multiple-choice question for each timeslot.
let form = FormApp.create('Conference Form');
form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId());
form.addTextItem().setTitle('Name').setRequired(true);
form.addTextItem().setTitle('Email').setRequired(true);
Object.keys(schedule).forEach(function(day) {
let header = form.addSectionHeaderItem().setTitle('Sessions for ' + day);
Object.keys(schedule[day]).forEach(function(time) {
let item = form.addMultipleChoiceItem().setTitle(time + ' ' + day)
.setChoiceValues(schedule[day][time]);
});
});
}
/**
* Sends out calendar invitations and a
* personalized Google Docs itinerary after a user responds to the form.
*
* @param {Object} e The event parameter for form submission to a spreadsheet;
* see https://developers.google.com/apps-script/understanding_events
*/
function onFormSubmit(e) {
let user = {name: e.namedValues['Name'][0], email: e.namedValues['Email'][0]};
// Grab the session data again so that we can match it to the user's choices.
let response = [];
let values = SpreadsheetApp.getActive().getSheetByName('Conference Setup')
.getDataRange().getValues();
for (let i = 1; i < values.length; i++) {
let session = values[i];
let title = session[0];
let day = session[1].toLocaleDateString();
let time = session[2].toLocaleTimeString();
let timeslot = time + ' ' + day;
// For every selection in the response, find the matching timeslot and title
// in the spreadsheet and add the session data to the response array.
if (e.namedValues[timeslot] && e.namedValues[timeslot] == title) {
response.push(session);
}
}
sendInvites_(user, response);
sendDoc_(user, response);
}
/**
* Add the user as a guest for every session he or she selected.
* @param {object} user An object that contains the user's name and email.
* @param {Array<String[]>} response An array of data for the user's session choices.
*/
function sendInvites_(user, response) {
let id = ScriptProperties.getProperty('calId');
let cal = CalendarApp.getCalendarById(id);
for (let i = 0; i < response.length; i++) {
cal.getEventSeriesById(response[i][5]).addGuest(user.email);
}
}
/**
* Creates and shares a personalized Google Doc that shows the user's itinerary.
* @param {object} user An object that contains the user's name and email.
* @param {Array<string[]>} response An array of data for the user's session choices.
*/
function sendDoc_(user, response) {
let doc = DocumentApp.create('Conference Itinerary for ' + user.name)
.addEditor(user.email);
let body = doc.getBody();
let table = [['Session', 'Date', 'Time', 'Location']];
for (let i = 0; i < response.length; i++) {
table.push([response[i][0], response[i][1].toLocaleDateString(),
response[i][2].toLocaleTimeString(), response[i][4]]);
}
body.insertParagraph(0, doc.getName())
.setHeading(DocumentApp.ParagraphHeading.HEADING1);
table = body.appendTable(table);
table.getRow(0).editAsText().setBold(true);
doc.saveAndClose();
// Emails a link to the Doc as well as a PDF copy.
MailApp.sendEmail({
to: user.email,
subject: doc.getName(),
body: 'Thanks for registering! Here\'s your itinerary: ' + doc.getUrl(),
attachments: doc.getAs(MimeType.PDF),
});
}
/**
* Removes the calId script property so that the 'setUpConference_()' can be run again.
*/
function resetProperties(){
let scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.deleteAllProperties();
}
贡献者
此示例由 Google 在 Google 开发者专家的帮助下进行维护。
后续步骤