Created
August 31, 2017 12:52
-
-
Save eloycoto/db87a998bbf78ea36adcbf1dbeeb7f5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sort_object(obj){ | |
var result = {} | |
keys = Object.keys(obj), | |
len = keys.length; | |
keys.sort(); | |
for (i = 0; i < len; i++) { | |
result[keys[i]] = obj[keys[i]]; | |
} | |
return result | |
} | |
var account = new function() { | |
this.me = Session.getActiveUser().getEmail(); | |
this.dates = {} | |
this._outbound = 0; | |
this._inbound = 0; | |
this._add_to_date = function(date, msg_type){ | |
var key = date.getFullYear().toString() + | |
('0' + (date.getMonth()+1)).slice(-2) + | |
('0' + (date.getDate())).slice(-2); | |
var type = ((msg_type==1) ? 'inbound' : 'outbound'); | |
if(this.dates.hasOwnProperty(key)){ | |
this.dates[key][type]++; | |
} else { | |
this.dates[key] = {"outbound": 0, "inbound": 0}; | |
} | |
} | |
this._cleanup = function(){ | |
this.dates = {}; | |
this._inbound = 0; | |
this._outbound = 0; | |
} | |
this._get_messages_from_thread = function(thread){ | |
var messages = GmailApp.getMessagesForThread(thread); | |
var outbound = 0; | |
var inbound = 0; | |
for (var i = 0 ; i < messages.length; i++) { | |
var from = messages[i].getFrom(); | |
if( from.indexOf(this.me) == -1){ | |
this._add_to_date(messages[i].getDate(), 1) | |
inbound++ | |
}else { | |
this._add_to_date(messages[i].getDate(), 0) | |
outbound++; | |
} | |
} | |
return [inbound, outbound] | |
} | |
this.get_info_from_label = function(label){ | |
this._cleanup(); | |
var label = GmailApp.getUserLabelByName(label); | |
var threads = label.getThreads(); | |
for (var i = 0; i < threads.length; i++) { | |
var result = this._get_messages_from_thread(threads[i]); | |
this._inbound += result[0]; | |
this._outbound += result[1]; | |
} | |
return this.dates; | |
} | |
} | |
function init(){ | |
dates = sort_object(account.get_info_from_label("hacksb")) | |
Logger.log(dates); | |
for (var key in dates) { | |
Logger.log(key); | |
} | |
//Logger.log(dates); | |
} | |
function doGet() { | |
var info = sort_object(account.get_info_from_label("hacksb")); | |
var data = Charts.newDataTable() | |
.addColumn(Charts.ColumnType.STRING, 'Dates') | |
.addColumn(Charts.ColumnType.NUMBER, 'Inbound') | |
.addColumn(Charts.ColumnType.NUMBER, 'Outbound') | |
for (var key in info) { | |
data.addRow([ | |
key, | |
account.dates[key]["inbound"], | |
account.dates[key]["outbound"]]); | |
} | |
var chart = Charts.newAreaChart() | |
.setDataTable(data) | |
.setStacked() | |
.setRange(0, 40) | |
.setTitle('Emails per month') | |
.build(); | |
var uiApp = UiApp.createApplication().setTitle('My Chart'); | |
uiApp.add(chart); | |
var button = uiApp.createButton("click me!").setId("button"); | |
button.addClickHandler(uiApp.createServerHandler("handlerFunction")); | |
uiApp.add(button); | |
return uiApp; | |
} | |
function handlerFunction(eventInfo) { | |
var app = UiApp.getActiveApplication(); | |
app.getElementById("button").setText("I was clicked!"); | |
return app; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment