-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.js
More file actions
125 lines (106 loc) · 4.46 KB
/
Copy pathCode.js
File metadata and controls
125 lines (106 loc) · 4.46 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
function main() {
//find the sheet and get all of its data
var formResp = getSheetbyID(978379940);
//get all of its data
var formData = formResp.getRange(1, 1, formResp.getLastRow(), formResp.getLastColumn()).getDisplayValues();
// Logger.log(formData);
//get the latest responses (aka the last row)
var recentResponse = formResp.getRange(formResp.getLastRow(), 1, 1, formResp.getLastColumn()).getDisplayValues();
// Logger.log(recentResponse);
//define fields to replace (birthday is later)
var nameCN = recentResponse[0][1];
var nameENG = recentResponse[0][2];
var age = recentResponse[0][4];
var address = recentResponse[0][5];
var emailRecipient = recentResponse[0][6];
var grade = recentResponse[0][7];
var mando1 = recentResponse[0][8];
var mando2 = recentResponse[0][9];
var mando3 = recentResponse[0][10];
var mando4 = recentResponse[0][11];
var mando5 = recentResponse[0][12];
var mandoOther = recentResponse[0][13];
var gpa9 = recentResponse[0][14];
var gpa10 = recentResponse[0][15];
var gpa11 = recentResponse[0][16];
var questionOne = recentResponse[0][17];
var questionTwo = recentResponse[0][18];
var questionuThree = recentResponse[0][19];
var questionFour = recentResponse[0][20];
var gender = recentResponse[0][21];
//split the birthday into its corresponding values, instead of one large string
var bdayWhole = recentResponse[0][3];
var bdayArray = [{}];
bdayArray = bdayWhole.split("/");
// Logger.log(bdayArray);
//Change gender to chinese character
// Logger.log(gender);
if (gender == "Male") {
gender = "男";
} else {
gender = "女";
}
// Logger.log(gender);
//then create the copy
var file = DriveApp.getFileById('1QNGbtm24GD-tAHl6MYMEuGgj3ZiaVUjuxgQDTUXC1ac');
var folder = DriveApp.getFolderById('1_ttA3-eJrQfy0jUJ63uxDNkxQMZz0DRW')
var copy = file.makeCopy(nameENG + ' - National Chinese Honors Society Student Application Form', folder);
//open the copy of the template doc and prepare for updates
//THE DOC IS FOUND UNDER THE 'COPY' VARIABLE, 'DOC' SIMPLY ALLOWS FOR EDITS TO HAPPEN TO THE COPY
var doc = DocumentApp.openById(copy.getId());
var body = doc.getBody();
//replace stuff :sob:
body.replaceText('{{chineseName}}', nameCN);
body.replaceText('{{engName}}', nameENG);
body.replaceText('{{birthYear}}', bdayArray[2]);
body.replaceText('{{birthMonth}}', bdayArray[0]);
body.replaceText('{{birthDay}}', bdayArray[1]);
body.replaceText('{{age}}', age);
body.replaceText('{{address}}', address);
body.replaceText('{{email}}', emailRecipient);
body.replaceText('{{grade}}', grade);
body.replaceText('{{1grade}}', mando1);
body.replaceText('{{2grade}}', mando2);
body.replaceText('{{3grade}}', mando3);
body.replaceText('{{4grade}}', mando4);
body.replaceText('{{5grade}}', mando5);
body.replaceText('{{otherGrade}}', mandoOther);
body.replaceText('{{9gpa}}', gpa9);
body.replaceText('{{10gpa}}', gpa10);
body.replaceText('{{11gpa}}', gpa11);
body.replaceText('{{questionOne}}', questionOne);
body.replaceText('{{questionTwo}}', questionTwo);
body.replaceText('{{questionThree}}', questionuThree);
body.replaceText('{{questionFour}}', questionFour);
//Find the mention of the text, get its index start and stop
var nannu = body.findText(gender);
var nannuStart = nannu.getStartOffset();
var nannuEnd = nannu.getEndOffsetInclusive();
//get the text as element to be able to edit it as text
var nannuE = nannu.getElement();
//as text, set only the selected text bold and highlighted
nannuE.asText()
.setBold(nannuStart, nannuEnd, true)
.setBackgroundColor(nannuStart, nannuEnd, '#b3b3b3');
//save doc, keeps changes made
doc.saveAndClose();
//create email template from email.html
var htmlTemplate = HtmlService.createTemplateFromFile('email');
//make the values we want to use in the email objects of the template
htmlTemplate.nameENG = nameENG;
//evaluate the template and prep it for emailing
var htmlEmail = htmlTemplate.evaluate().getContent();
//send email
MailApp.sendEmail(
emailRecipient,
'NCHS Student Application Form',
'This email contains html', {
htmlBody: htmlEmail,
attachments: [copy.getAs(MimeType.PDF)],
name: 'National Chinese Honors Society'
}
);
//delete the file
//if you would prefer to keep all of the generated forms in the selected folder instead of deleteing them immediately, then comment out the line below
copy.setTrashed(true);
}