function doGet(e){
var triggers = [Link]();
for (var i=0; i<[Link]; i++) {
[Link](triggers[i]);
}
[Link]("deleteOld")
.timeBased().everyMinutes(30).create();
[Link]("followupEmails")
.timeBased().everyHours(1).create();
[Link]("archiveRead")
.timeBased().everyMinutes(1).create();
deleteOld();
return HtmlService
.createTemplateFromFile('index')
.evaluate();
}
function createLabels() {
var labels = ["Important","Promotions", "Social", "Updates", "Forums","Block
list",unrespondedLabel, ignoreLabel];
var gmail, parent = "+";
for (var i=0; i<[Link]; i++) {
label = parent + "/" + labels[i];
gmail = [Link](label) ?
[Link](label) : [Link](labe
l);
}
}
function isTimeUp_(start) {
var now = new Date();
return [Link]() - [Link]() > 300000; // 5 minutes
}
/* archive all inbox emails that
1) have been read and are not important
2) are social, promotions, updates and forums emails
*/
function archiveRead() {
var batchSize = 100 // Process up to 100 threads at once
var threads = [Link]('in:inbox is:read category:{social promotions up
dates forums} '); //-is:{important starred}
for (j = 0; j < [Link]; j+=batchSize) {
if(threads[j].isImportant() || threads[j].hasStarredMessages()) {
threads[j].addLabel('+/Important')
} else {
[Link]([Link](j, j+batchSize));
}
}
}
/* delete all social/promotions emails that
1) have not been replied to, starred or flagged
2) older than 30d
3) subject does not have -notice -receipt -login -license -order -"writing tip"
-version
category descriptions: [Link]
*/
function deleteOld() {
var batchSize = 100; // Process up to 100 threads at once
var searchquery = 'older_than:180d is:unread category:{social promotions updat
es forums} -in:{trash spam} -from:me -cc:me -has:attachment -is:{important starr
ed} -label:important -{itinerary flight notice receipt login license # writing c
onfirmation password bill refund due payment invoice details transaction reminde
r} '
// 'older_than:180d is:unread category:{social promotions updates forums} -in:
{trash spam sent primary inbox} -from:me -cc:me -has:attachment -is:{important s
tarred} -label:important -subject:{itinerary flight notice receipt login license
order writing version password bill refund due confirm payment shipping invoice
details transaction purchase reminder}'
var start = new Date();
try{
var threads = [Link](searchquery);
for (j = 0; j < [Link]; j+=batchSize) {
if (isTimeUp_(start)) {
[Link]("Time up");
break;
}
[Link]([Link](j, j+batchSize));
[Link]('finished move %d', j);
}
} catch (e) {
[Link](e);
}
}
/*
* This script goes through your Gmail Inbox and finds recent emails where you
* were the last respondent. It applies a nice label to them, so you can
* see them in Priority Inbox or do something else.
*
* To remove and ignore an email thread, just remove the unrespondedLabel and
* apply the ignoreLabel.
*
* This is most effective when paired with a time-based script trigger.
*
* For installation instructions, read this blog post:
* [Link]
*/
// Edit these to your liking.
var unrespondedLabel = '@Followup',
ignoreLabel = 'NonResponse',
minTime = '5d', // 5 days
maxTime = '10d'; // 10 days
// Mapping of Gmail search time units to milliseconds.
var UNIT_MAPPING = {
h: 36e5, // Hours
d: 864e5, // Days
w: 6048e5, // Weeks
m: 263e7, // Months
y: 3156e7 // Years
};
var ADD_LABEL_TO_THREAD_LIMIT = 100;
function followupEmails() {
processUnresponded();
cleanUp();
}
function processUnresponded() {
var threads = [Link]('is:sent from:me -in:chats older_than:' + minTim
e + ' newer_than:' + maxTime),
threadMessages = [Link](threads),
unrespondedThreads = [],
minTimeAgo = new Date();
[Link](subtract(minTimeAgo, minTime));
[Link]('Processing ' + [Link] + ' threads.');
// Filter threads where I was the last respondent.
[Link](function(messages, i) {
var thread = threads[i],
lastMessage = messages[[Link] - 1],
lastFrom = [Link](),
lastTo = [Link](), // I don't want to hear about it when I a
m sender and receiver
lastMessageIsOld = [Link]().getTime() < [Link](
);
if (isMe(lastFrom) && !isMe(lastTo) && lastMessageIsOld && !threadHasLabel(t
hread, ignoreLabel)) {
[Link](thread);
}
else if (isMe(lastFrom) && isMe(lastTo)) { //star all emails from self to se
lf
[Link]()[0].markUnread().star();
}
})
// Mark unresponded in bulk.
markUnresponded(unrespondedThreads);
[Link]('Updated ' + [Link] + ' messages.');
}
function subtract(date, timeStr) {
// Takes a date object and subtracts a Gmail-style time string (e.g. '5d').
// Returns a new date object.
var re = /^([0-9]+)([a-zA-Z]+)$/,
parts = [Link](timeStr),
val = parts && parts[1],
unit = parts && parts[2],
ms = UNIT_MAPPING[unit];
return [Link]() - (val * ms);
}
function isMe(fromAddress) {
var addresses = getEmailAddresses();
for (i = 0; i < [Link]; i++) {
var address = addresses[i],
r = RegExp(address, 'i');
if ([Link](fromAddress)) {
return true;
}
}
return false;
}
function getEmailAddresses() {
// Cache email addresses to cut down on API calls.
if (![Link]) {
[Link]('No cached email addresses. Fetching.');
var me = [Link]().getEmail(),
emails = [Link]();
[Link](me);
[Link] = emails;
[Link]('Found ' + [Link] + ' email addresses that belong to
you.');
}
return [Link];
}
function threadHasLabel(thread, labelName) {
var labels = [Link]();
for (i = 0; i < [Link]; i++) {
var label = labels[i];
if ([Link]() == labelName) {
return true;
}
}
return false;
}
function markUnresponded(threads) {
var label = getLabel(unrespondedLabel);
// addToThreads has a limit of 100 threads. Use batching.
if ([Link] > ADD_LABEL_TO_THREAD_LIMIT) {
for (var i = 0; i < [Link]([Link] / ADD_LABEL_TO_THREAD_LIMIT); i
++) {
[Link]([Link](100 * i, 100 * (i + 1)));
}
} else {
[Link](threads);
}
}
function getLabel(labelName) {
// Cache the labels.
[Link] = [Link] || {};
label = [Link][labelName];
if (!label) {
[Link]('Could not find cached label "' + labelName + '". Fetching.', thi
[Link]);
var label = [Link](labelName);
if (label) {
[Link]('Label exists.');
} else {
[Link]('Label does not exist. Creating it.');
label = [Link](labelName);
}
[Link][labelName] = label;
}
return label;
}
function cleanUp() {
var label = getLabel(unrespondedLabel),
iLabel = getLabel(ignoreLabel),
threads = [Link](),
expiredThreads = [],
expiredDate = new Date();
[Link](subtract(expiredDate, maxTime));
if (![Link]) {
[Link]('No threads with that label');
return;
} else {
[Link]('Processing ' + [Link] + ' threads.');
}
[Link](function(thread) {
var lastMessageDate = [Link]();
// Remove all labels from expired threads.
if ([Link]() < [Link]()) {
[Link]('Thread expired');
[Link](thread);
} else {
[Link]('Thread not expired');
}
});
[Link](expiredThreads);
[Link](expiredThreads);
[Link]([Link] + ' unresponded messages expired.');
}