Skip to content

Instantly share code, notes, and snippets.

@mhawksey
Created June 16, 2011 13:47
Show Gist options
  • Save mhawksey/1029258 to your computer and use it in GitHub Desktop.
Save mhawksey/1029258 to your computer and use it in GitHub Desktop.
Google Apps Script to display email notification
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment