-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautomatedEmail
More file actions
84 lines (72 loc) · 3.09 KB
/
automatedEmail
File metadata and controls
84 lines (72 loc) · 3.09 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
function myFunction(e) {
//test
populateDocument(e);
}
function getExpirationDate(communityName, timeStamp) {
var expirationDate = ""
var date = new Date(timeStamp);
//date = date.getDate();
var MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
if(communityName === null || communityName === ""){
expirationDate = "Error: Not valid"
}
else if(timeStamp === null || timeStamp === ""){
expirationDate = "Error: Not valid"
}
else{
switch(communityName){
case "Location1": //1 Day until it expires
var expireDate = new Date(date.getTime() + (MILLIS_PER_DAY *1));
Logger.log(expireDate)
expirationDate = Utilities.formatDate(expireDate, "EST", "dd-mmm-yyyy HH:MM:SS.ss");
break;
case "Location2": //2 Days until it expires
var expireDate = new Date(date.getTime() + (MILLIS_PER_DAY *2));// num of days
expirationDate = Utilities.formatDate(expireDate, "EST", "mm/dd/yyyy h:mm am/pm");
break;
case "Location3": //3 Days until it expires
var expireDate = new Date(date.getTime() + (MILLIS_PER_DAY *3));// num of days
expirationDate = Utilities.formatDate(expireDate, "EST", "mm/dd/yyyy h:mm am/pm");
break;
}
}
return expirationDate;
}
function populateDocument(e){
//e.values is an array of form values
var timestamp = e.values[0];
var community = e.values[1];
var fullname = e.values[2];
var license = e.values[3];
var make = e.values[4];
var model = e.values[5];
var phone = e.values[6];
var email = e.values[7];
//file is the template file, and you get it by ID
var file = DriveApp.getFileById('YOUR FILE ID');
//We can make a copy of the template, name it, and optionally tell it what folder to live in
//file.makeCopy will return a Google Drive file object
var folder = DriveApp.getFolderById('YOUR FILE ID')
var copy = file.makeCopy(fullname + '' + timestamp, folder);
//Once we've got the new file created, we need to open it as a document by using its ID
var doc = DocumentApp.openById(copy.getId());
//Since everything we need to change is in the body, we need to get that
var body = doc.getBody();
//Call the replace text methods to input vehicle information in document
body.replaceText('{{Make}}', make); //Replaces {{Make}} with the inputed make of the car
body.replaceText('{{Model}}', model);
body.replaceText('{{Tag}}', license);
body.replaceText('{{Expire}}', getExpirationDate(community, timestamp));
//Lastly we save and close the document to persist our changes
doc.saveAndClose();
var docblob = doc.getAs('application/pdf');
var body = 'Attached is your visitor pass \n\n\t\tThank You So Much For Using Miami Parking Enforcement.';
// MailApp.sendEmail(email, 'Visitor Pass', body)
// Send an email with two attachments: a file from Google Drive (as a PDF)
var file = DriveApp.getFileById(copy.getId());
MailApp.sendEmail( email , 'Visitor Pass',body ,
{
name: 'Automatic Emailer Script',
attachments: [file.getAs(MimeType.PDF)]
});
}