Last active
August 29, 2015 14:05
-
-
Save wesalvaro/01cc403b92ab91ac622e to your computer and use it in GitHub Desktop.
Inbox Zero Tattler
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
/* | |
A Google Apps Script for reporting your Inbox Zero progress/procrastination. | |
https://gist.github.com/wesalvaro/01cc403b92ab91ac622e | |
Installation: | |
1. Use this file to create a new Google Apps Script. | |
2. Run the `checkInboxThreadCount` function in your new app to authorize it. | |
3. Setup a trigger to run the `checkInboxThreadCount` function however you like. | |
4. Deploy your script as a web app. | |
Note: The app should be set to run as _you_. | |
5. Put the link to your app in your email signature! | |
Obviously, feel free to alter the script as much as you want! | |
But please comment here! | |
*/ | |
var THREAD_COUNT_KEY = 'threadCount'; | |
var THREAD_COUNT_HISTORY_KEY = 'threadCountHistory'; | |
var THREAD_COUNT_UPDATE_KEY = 'threadCountUpdateTime'; | |
var MAX_SIZE = 30; | |
var TEMPLATE = '<h2>I currently have a backlog of <?= threadCount ?> emails!</h2>'; | |
var CHART_TITLE = 'Number of Emails I need to handle!' | |
// Clear the current inbox zero history data. | |
function clearHistory() { | |
PropertiesService.getUserProperties().deleteProperty(THREAD_COUNT_HISTORY_KEY); | |
} | |
// Updates your current inbox zero count. | |
// Safe to run as often as you want your status updated. | |
function checkInboxThreadCount() { | |
var userProperties = PropertiesService.getUserProperties(); | |
// Get the current inbox zero status | |
var inboxThreadCount = GmailApp.getInboxThreads().length; | |
// If we've already updated the history data today, just save the current status and stop | |
var lastUpdated = new Date(parseInt(userProperties.getProperty(THREAD_COUNT_UPDATE_KEY), 10)); | |
if (lastUpdated.getDate() == new Date().getDate()) { | |
userProperties.setProperty(THREAD_COUNT_KEY, '' + inboxThreadCount); | |
return; | |
} | |
// It's a new day, update the history data | |
var threadCountHistory = JSON.parse(userProperties.getProperty(THREAD_COUNT_HISTORY_KEY) || '[]'); | |
// Add the new data point and limit the data size | |
var historySize = threadCountHistory.push([new Date().getTime(), inboxThreadCount]); | |
threadCountHistory = threadCountHistory.slice(historySize - MAX_SIZE, historySize); | |
// Save the data | |
var data = {}; | |
data[THREAD_COUNT_KEY] = '' + inboxThreadCount; | |
data[THREAD_COUNT_HISTORY_KEY] = JSON.stringify(threadCountHistory); | |
data[THREAD_COUNT_UPDATE_KEY] = '' + new Date().getTime(); | |
userProperties.setProperties(data); | |
} | |
// Display current inbox zero status and history. | |
function doGet() { | |
var userProperties = PropertiesService.getUserProperties(); | |
var app = UiApp.createApplication(); | |
// Insert template | |
var inboxThreadCount = parseInt(userProperties.getProperty(THREAD_COUNT_KEY), 10); | |
var template = HtmlService.createTemplate(TEMPLATE); | |
template.threadCount = inboxThreadCount; | |
app.add(app.createHTML(template.evaluate().getContent())); | |
// Build history chart data | |
var threadCountHistory = JSON.parse(userProperties.getProperty(THREAD_COUNT_HISTORY_KEY) || '[]'); | |
var dataBuilder = Charts.newDataTable(). | |
addColumn(Charts.ColumnType.STRING, 'Date'). | |
addColumn(Charts.ColumnType.NUMBER, 'Emails'); | |
var ticks = []; | |
threadCountHistory.forEach(function(v) { | |
dataBuilder.addRow([new Date(parseInt(v[0], 10)).toDateString(), v[1]]); | |
ticks.push(v[1]); | |
}); | |
// Configure chart | |
var chart = Charts.newLineChart(). | |
setCurveStyle(Charts.CurveStyle.SMOOTH). | |
setOption('vAxis.format', '0'). | |
setOption('vAxis.ticks', ticks). | |
setDimensions(1000, 300). | |
setDataTable(dataBuilder). | |
setTitle(CHART_TITLE). | |
build(); | |
app.add(chart); | |
return app; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment