public
Created

Google Apps Script to display email notification

  • Download Gist
gistfile1.js
JavaScript
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
function doGet(e) {
try{
var app = UiApp.createApplication();
var mainPanel = app.createVerticalPanel();
var emailTable = app.createFlexTable().setId("emailTable");
app.add(mainPanel);
mainPanel.add(emailTable);
var mail = GmailApp.getInboxThreads(0, 10);
for (var i = mail.length; i > 0; i--) {
var msg = mail[i-1];
var subject = msg.getFirstMessageSubject();
var msgDate = modDate(msg.getLastMessageDate());
var msgCount = msg.getMessageCount();
if (msgCount > 1){
var msgCountTxt = "("+msgCount+")";
} else {
var msgCountTxt = "";
}
var rec = msg.getLastMessageDate();
var status = msg.isUnread();
var msgs = msg.getMessages();
var msgTxt = msgs[msgs.length-1].getBody().replace(/<[^>]*>?/g, '');
//msgTxt = msgTxt.substring(1,30);
msgTxt = msgTxt.replace(/\n/g, ' ');
var frm = getEmailFromString(msgs);
//Logger.log("why "+msgTxt);
var textString = frm + " " + msgCountTxt + "- " + subject + " - " + msgTxt;
var textString = textString.substring(0,70);
Logger.log(textString);
var textLabel = app.createLabel().setText(textString);
var dateLabel = app.createLabel().setText(msgDate);
emailTable.setWidget(mail.length-i, 0, textLabel);
emailTable.setWidget(mail.length-i, 1, dateLabel);
}
//Logger.log(date);
return app;
} catch(error){
ScriptProperties.setProperty('debug', error);//errorsAndTesting('function doGet', error);
}
 
}
function modDate(aDate){
var today = new Date();
today.setHours(0,0,0,0);
if (today <= aDate){
var resp = Utilities.formatDate(aDate, "GMT", "HH:mma");
resp = resp.toLowerCase();
} else {
var resp = Utilities.formatDate(aDate, "GMT", "dd MMM");
}
return resp;
}
function getEmailFromString(msgs){
var users = [];
var fromStr = "";
for (var i = 0; i < msgs.length; i++) {
var frm = msgs[i].getFrom();
users.push(frm);
}
for (var u in users){
if (users[u] !='') {
temp = users[u].split(' ');
fromStr += temp[0] +", ";
}
}
fromStr = fromStr.substr(0,fromStr.length-2);
return fromStr
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.