forked from googleworkspace/apps-script-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.gs
More file actions
232 lines (217 loc) · 8.15 KB
/
forms.gs
File metadata and controls
232 lines (217 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* Copyright 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.
*/
// [START apps_script_sheets_custom_form_responses_quickstart]
/**
* A special function that inserts a custom menu when the spreadsheet opens.
*/
function onOpen() {
const menu = [{name: 'Set up conference', functionName: 'setUpConference_'}];
try {
SpreadsheetApp.getActive().addMenu('Conference', menu);
} catch (e) {
// TODO (Developer) - Handle Exception
Logger.log('Failed with error: %s' + e.error);
}
}
/**
* A set-up function that 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_() {
if (ScriptProperties.getProperty('calId')) {
Browser.msgBox('Your conference is already set up. Look in Google Drive!');
}
try {
const ss = SpreadsheetApp.getActive();
const sheet = ss.getSheetByName('Conference Setup');
const range = sheet.getDataRange();
const values = range.getValues();
setUpCalendar_(values, range);
setUpForm_(ss, values);
ScriptApp.newTrigger('onFormSubmit').forSpreadsheet(ss).onFormSubmit()
.create();
ss.removeMenu('Conference');
} catch (e) {
// TODO (Developer) - Handle Exception
Logger.log('Failed with error: %s' + e.error);
}
}
/**
* 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) {
try {
const cal = CalendarApp.createCalendar('Conference Calendar');
for (var i = 1; i < values.length; i++) {
const session = values[i];
const title = session[0];
const start = joinDateAndTime_(session[1], session[2]);
const end = joinDateAndTime_(session[1], session[3]);
const options = {location: session[4], sendInvites: true};
const event = cal.createEvent(title, start, end, options)
.setGuestsCanSeeGuests(false);
session[5] = event.getId();
}
range.setValues(values);
// Store the ID for the Calendar, which is needed to retrieve events by ID.
ScriptProperties.setProperty('calId', cal.getId());
} catch (e) {
// TODO (Developer) - Handle Exception
Logger.log('Failed with error: %s' + e.error);
}
}
/**
* 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.
*
* @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.
const schedule = {};
for (let i = 1; i < values.length; i++) {
const session = values[i];
const day = session[1].toLocaleDateString();
const time = session[2].toLocaleTimeString();
if (!schedule[day]) {
schedule[day] = {};
}
if (!schedule[day][time]) {
schedule[day][time] = [];
}
schedule[day][time].push(session[0]);
}
try {
// Create the form and add a multiple-choice question for each timeslot.
const form = FormApp.create('Conference Form');
form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId());
form.addTextItem().setTitle('Name').setRequired(true);
form.addTextItem().setTitle('Email').setRequired(true);
for (const day of schedule) {
const header = form.addSectionHeaderItem().setTitle(
'Sessions for ' + day);
for (const time of schedule[day]) {
const item = form.addMultipleChoiceItem().setTitle(time + ' ' + day)
.setChoiceValues(schedule[day][time]);
}
}
} catch (e) {
// TODO (Developer) - Handle Exception
Logger.log('Failed with error: %s' + e.error);
}
}
/**
* A trigger-driven function that 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) {
const 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.
const response = [];
try {
values = SpreadsheetApp.getActive()
.getSheetByName('Conference Setup').getDataRange().getValues();
for (let i = 1; i < values.length; i++) {
const session = values[i];
const title = session[0];
const day = session[1].toLocaleDateString();
const time = session[2].toLocaleTimeString();
const 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);
} catch (e) {
// TODO (Developer) - Handle Exception
Logger.log('Failed with error: %s' + e.error);
}
}
/**
* 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) {
try {
const id = ScriptProperties.getProperty('calId');
const cal = CalendarApp.getCalendarById(id);
for (let i = 0; i < response.length; i++) {
cal.getEventSeriesById(response[i][5]).addGuest(user.email);
}
} catch (e) {
// TODO (Developer) - Handle Exception
Logger.log('Failed with error: %s' + e.error);
}
}
/**
* Create and share 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) {
try {
const doc = DocumentApp.create('Conference Itinerary for ' + user.name)
.addEditor(user.email);
const 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();
// Email 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)
});
} catch (e) {
// TODO (Developer) - Handle Exception
Logger.log('Failed with error: %s' + e.error);
}
}
// [END apps_script_sheets_custom_form_responses_quickstart]