-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_email.gs
117 lines (105 loc) · 4.16 KB
/
send_email.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
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
/////////////////////////////////////////////////////////////////////***
//
// UtilApp-Call | send_email.gs | RC2
//
/////////////////////////////////////////////////////////////////////***
//
// Send Emails on Google Form Submit, get content, parse and send
// to given Address List
//
//
// by T. Ledermueller (derlemue.com) | License: GNU GPL-3.0
//
/////////////////////////////////////////////////////////////////////***
///
/// Trigger to fire when new row is added to Google Sheet
///
function onFormSubmit(e){
Logger.log("[METHOD] onFormSubmit");
emailSenderBR();
}
/// Script to parse Email from Sheet and send by alias
///
function emailSenderCall(){
/// set variables (shortcuts for Google App Script Classes)
var email_draft = HtmlService.createTemplateFromFile("email_text");
var ws_input = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Input");
var ws_mail = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Mail_Util");
Logger.log(ws_input.getLastRow())
/// set variables (define sources on Google Sheet)
/// set range from column A-Q and set row from "last"-"last"
var data_input = ws_input.getRange("A" + ws_input.getLastRow() + ":Q" + ws_input.getLastRow()).getValues();
/// set range from column A-C and set row from 2-"last"
var data_mail = ws_mail.getRange("A2:C" + ws_mail.getLastRow()).getValues();
/// set variables (link name and cloumn in Google Sheet "Input")
var vTimestamp = 0;
var vCase = 1;
var vReporter = 2;
var lastRow = ws_input.getLastRow();
var vDate = new Date(ws_input.getSheetValues(lastRow, 4, 1, 1));
var vDateTemp = Utilities.formatDate(vDate, "Europe/Berlin", "dd.MM.yyyy");
var vTime1 = new Date(ws_input.getSheetValues(lastRow, 5, 1, 1));
var vTime2 = new Date(ws_input.getSheetValues(lastRow, 6, 1, 1));
var vBuilding = 6;
var vMedium = 7;
var vMachine = 8;
var vError = 9;
var vCause = 10;
var vAction =11;
var vInterrup = 12;
var vOtherService = 13;
var vLogBook = 14;
var vComment = 15;
var vInfoSpread = 16;
/// set variables (link name and cloumn in Google Sheet "Mail_Util")
var vAddr = 1;
var vName = 0;
/// Show Variables in Log for debug
Logger.log("Last Row");
Logger.log(lastRow);
Logger.log("vDate Var");
Logger.log(vDate);
Logger.log("vDateTemp Var");
Logger.log(vDateTemp);
Logger.log("vReporter Var");
Logger.log(vReporter);
Logger.log("vTime1 & vTime2 Var");
Logger.log(vTime1);
Logger.log(vTime2);
Logger.log("Script Finished");
/// Parse Emails from Google Sheet
/// get Email address and name from Google Sheet "Mail_Util", repeat for each row)
data_mail.forEach(function get1(row){
var vAdd = row[vAddr];
var vNameTemp = row[vName];
Logger.log(vAdd);
/// get variables for Email body from Google Sheet "Input" (last row) and send to Email draft (email_text.html)
data_input.forEach(function get2(row){
email_draft.vReporter = row[vReporter];
email_draft.vCase = row[vCase];
email_draft.vDate = Utilities.formatDate(vDate, "Europe/Berlin", "dd.MM.yyyy");
email_draft.vTime1 = Utilities.formatDate(vTime1, "Europe/Berlin", "HH:mm");
email_draft.vTime2 = Utilities.formatDate(vTime2, "Europe/Berlin", "HH:mm");
email_draft.vBuilding = row[vBuilding];
email_draft.vMachine = row[vMachine];
email_draft.vMedium = row[vMedium];
email_draft.vError = row[vError];
email_draft.vCause = row[vCause];
email_draft.vAction = row[vAction];
email_draft.vInterrup = row[vInterrup];
email_draft.vOtherService = row[vOtherService];
email_draft.vInfoSpread = row[vInfoSpread];
email_draft.vComment = row[vComment];
email_draft.vName = vNameTemp
email_draft.vDuration = calcTime()
/// get variables for Email subject, assemble Email and send by alias
var vInfoSpreadTemp = row[vInfoSpread];
var vBody = email_draft.evaluate().getContent();
var vBuildingTemp = row[vBuilding]
Logger.log(vBody);
var vSub = "Einsatz im Gebäude: " + vBuildingTemp + " | Verteiler: " + vInfoSpreadTemp
var vBody = email_draft.evaluate().getContent();
GmailApp.sendEmail(vAdd, vSub, 'HTML seems to be unsupported by your Email', {htmlBody: vBody})
})
})
};