forked from theodoretus/Google-Sheets-Unlimited-Mail-Merge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
responses.gs
183 lines (172 loc) · 7.45 KB
/
responses.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
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
function labelResponses() {
//error handling wrap
var maxRetries = 5;
for (var currentRetry = 0; currentRetry < maxRetries; currentRetry++) {
try {
deleteAllTriggers();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mailSenderSheet = ss.getSheetByName('Mail Sender');
var countersSheet = ss.getSheetByName("Counters (Ignore)");
var labelName = mailSenderSheet.getRange("B2").getValue();
var label = GmailApp.getUserLabelByName(labelName);
var totalThreads = threadCounter(label, "A2");
var startTime= new Date().getTime();
var cutoffTime = (1000 * 60 * 4);
//create reply label for campaign
if(!(GmailApp.getUserLabelByName(labelName + "/Reply"))) {
GmailApp.createLabel(labelName +"/Reply");
}
var replyLabel = GmailApp.getUserLabelByName(labelName + "/Reply");
//determine batch index
if (countersSheet.getRange("A2").getValue()) {
var batchIndex = countersSheet.getRange("A2").getValue();
}
else {
batchIndex = 1;
countersSheet.getRange("A2").setValue(batchIndex);
}
//get threads in groups and cycle through
var threadStart = (Math.round(batchIndex * 500) - 500);
var threads = label.getThreads(threadStart,500);
for (var i = 0; i < threads.length; i++) {
var currentTime = new Date().getTime();
//set trigger if cutoff time is reached
if (currentTime - startTime >= cutoffTime) {
deleteFirstTrigger();
ScriptApp.newTrigger("labelResponses")
.timeBased()
.at(new Date(currentTime + (1000 * 60)))
.inTimezone(ss.getSpreadsheetTimeZone())
.create();
batchIndex = batchIndex + (i / 500);
countersSheet.getRange("A2").setValue(batchIndex);
break;
}
//otherwise, add reply label
else {
var messageCount = threads[i].getMessageCount();
if (messageCount > 1) {
threads[i].addLabel(replyLabel);
}
//check for completion of job; move to printing responses if so
if ((batchIndex * 500) - 500 + (i + 1) == totalThreads) {
deleteFirstTrigger();
ScriptApp.newTrigger("printResponses")
.timeBased()
.at(new Date(currentTime + (1000 * 60)))
.inTimezone(ss.getSpreadsheetTimeZone())
.create();
countersSheet.getRange("A2").setValue(1);
return;
}
}
}
}
//error scenario
catch(e) {
MailApp.sendEmail("YOUR EMAIL HERE", "Error report", e.message); //SET YOUR EMAIL
Utilities.sleep(1000);
}
if (currentRetry == (maxRetries - 1)) {
ss = SpreadsheetApp.getActiveSpreadsheet();
countersSheet = ss.getSheetByName("Counters (Ignore)");
countersSheet.getRange("A2").setValue(1);
}
}
}
function printResponses() {
//error handling wrap
var maxRetries = 5;
for (var currentRetry = 0; currentRetry < maxRetries; currentRetry++) {
try {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mailSenderSheet = ss.getSheetByName('Mail Sender');
var countersSheet = ss.getSheetByName("Counters (Ignore)");
var labelName = mailSenderSheet.getRange("B2").getValue() + "/Reply";
var label = GmailApp.getUserLabelByName(labelName);
var totalThreads = threadCounter(label, "C2");
var startTime= new Date().getTime();
var cutoffTime = (1000 * 60 * 4);
var templateGrabberSheet = ss.getSheetByName("Template Grabber");
var name = templateGrabberSheet.getRange("E2").getValue();
var responsesSheet = ss.getSheetByName("Responses");
var responsesRowDataIndex = getRowDataIndex(responsesSheet);
var responderEmailData = responsesSheet.getRange(3, 3, responsesRowDataIndex, 2).getValues();
//determine batch index
if (countersSheet.getRange("A2").getValue()) {
var batchIndex = countersSheet.getRange("A2").getValue();
}
else {
batchIndex = 1;
countersSheet.getRange("A2").setValue(batchIndex);
}
//get threads in groups and cycle through
var threadStart = (Math.round(batchIndex * 500) - 500);
var threads = label.getThreads(threadStart,500);
for (var i = 0; i < threads.length; i++) {
var currentTime = new Date().getTime();
//set trigger if cutoff time is reached
if (currentTime - startTime >= cutoffTime) {
deleteFirstTrigger();
ScriptApp.newTrigger("printResponses")
.timeBased()
.at(new Date(currentTime + (1000 * 60)))
.inTimezone(ss.getSpreadsheetTimeZone())
.create();
batchIndex = batchIndex + (i / 500);
countersSheet.getRange("A2").setValue(batchIndex);
break;
}
//otherwise, add email address, reply to sheet
else {
for (var k = 0; k < threads[i].getMessageCount(); k++) {
responsesRowDataIndex = getRowDataIndex(responsesSheet);
responderEmailData = responsesSheet.getRange(3, 3, responsesRowDataIndex, 2).getValues();
var added = false;
var sendername = Session.getActiveUser().getEmail();
if (name) {
sendername = name + " <" + Session.getActiveUser().getEmail() + ">";
}
if (threads[i].getMessages()[k].getFrom() !== sendername) {
var responderEmailAddress = threads[i].getMessages()[k].getFrom();
var responderEmailBody = threads[i].getMessages()[k].getPlainBody();
for (var j = 0; j < responderEmailData.length; j++) {
if(responderEmailData[j][0] == responderEmailAddress) {
added = true;
break;
}
}
if(!added) {
responderEmailData[responderEmailData.length - 1] = [responderEmailAddress, responderEmailBody];
responsesSheet.getRange(3, 3, responsesRowDataIndex, 2).setValues(responderEmailData);
}
}
}
//check for completion of job; move to next day repetition of process if so
if ((batchIndex * 500) - 500 + (i + 1) == totalThreads) {
deleteFirstTrigger();
ScriptApp.newTrigger("labelResponses")
.timeBased()
.at(new Date(currentTime + (1000 * 60 * 60 * 24)))
.inTimezone(ss.getSpreadsheetTimeZone())
.create();
countersSheet.getRange("A2").setValue(1);
responsesRowDataIndex = getRowDataIndex(responsesSheet);
responsesSheet.getRange("C2").setValue(responsesRowDataIndex - 1);
return;
}
}
}
}
//error scenario
catch(e) {
MailApp.sendEmail("YOUR EMAIL HERE", "Error report", e.message); //SET YOUR EMAIL
Utilities.sleep(1000);
}
if (currentRetry == (maxRetries - 1)) {
ss = SpreadsheetApp.getActiveSpreadsheet();
countersSheet = ss.getSheetByName("Counters (Ignore)");
countersSheet.getRange("A2").setValue(1);
}
}
}