-
Notifications
You must be signed in to change notification settings - Fork 1
/
Code.gs
68 lines (58 loc) · 2.09 KB
/
Code.gs
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
function getAnswers(e) {
// Get answers from form submission
var response = e.response;
var itemResponses = response.getItemResponses();
// Create object where: keys = item titles, values = item answers
var answers = {};
for (var i = 0; i < itemResponses.length; i++) {
var ir = itemResponses[i];
var title = ir.getItem().getTitle();
var answer = ir.getResponse();
answers[title] = answer;
}
return answers;
}
function runWizard(e) {
// Use default values when testing script
var default_answers = {
'Name': 'Sample City',
'Short Name': 'City',
'Who': 'Mayor',
'Manager': 'Chief Information Officer',
'Existing or New Position': 'Existing position',
'Number': 'Executive Order 2017-1',
'Email': 'gjordandetamore@sunlightfoundation.com' // Change this to your email
};
if (typeof e !== 'undefined') {
var answers = getAnswers(e);
} else {
var answers = default_answers;
}
// Fill out policy template with data from form submission
var template = HtmlService.createTemplateFromFile('Text');
template.data = answers;
var htmlBody = template.evaluate().getContent();
// Create the Google Doc
var blob = DriveApp.createFile('dummy', htmlBody, 'text/html').getBlob();
var resource = {
title: "Sample open-data policy for the " + answers['Name'],
convert: true,
mimeType: 'application/vnd.google-apps.document'
};
var file = Drive.Files.insert(resource, blob);
// Add space after paragraphs
var doc = DocumentApp.openById(file.id);
var pars = doc.getBody().getParagraphs();
for (p = 0; p < pars.length; p++) {
pars[p].setSpacingAfter(14);
}
// Give edit permissions to anyone with the link
DriveApp.getFileById(doc.getId()).setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT);
// Send the email
var doc_url = doc.getUrl();
GmailApp.sendEmail(answers['Email'], 'Your sample open-data policy', '', {
name: 'Sunlight Foundation',
htmlBody: '<p>See a Google Docs version of this text: <a href="'+ doc_url + '">' +
doc_url + '</a></p><br>' + htmlBody
});
}