Created
April 16, 2013 18:39
-
-
Save twotix/5398414 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // Source: http://www.pipetree.com/qmacro/blog/2011/10/automated-email-to-task-mechanism-with-google-apps-script/ | |
| // ----------------------------------------------------- | |
| // Globals, contants | |
| // ----------------------------------------------------- | |
| TASKLIST = "Ittichai's list"; | |
| LABEL_PENDING = "newtask"; | |
| LABEL_DONE = "newtaskdone"; | |
| // ----------------------------------------------------- | |
| // getTasklistId_(tasklistName) | |
| // Returns the id of the tasklist specified | |
| // Oddly, we should be able to use: | |
| // Tasks.Tasklists.get(tasklistName) | |
| // but it always gives an error "Invalid Value". | |
| // ----------------------------------------------------- | |
| function getTasklistId_(tasklistName) { | |
| var tasklistsList = Tasks.Tasklists.list(); | |
| var taskLists = tasklistsList.getItems(); | |
| for (tl in taskLists) { | |
| var title = taskLists[tl].getTitle(); | |
| if (title == tasklistName) { | |
| return taskLists[tl].getId(); | |
| } | |
| } | |
| } | |
| // ----------------------------------------------------- | |
| // processPending(sheet) | |
| // Process any pending emails and then move them to done | |
| // ----------------------------------------------------- | |
| function processPending_(sheet) { | |
| var label_pending = GmailApp.getUserLabelByName(LABEL_PENDING); | |
| var label_done = GmailApp.getUserLabelByName(LABEL_DONE); | |
| // The threads currently assigned to the 'pending' label | |
| var threads = label_pending.getThreads(); | |
| // Process each one in turn, assuming there's only a single | |
| // message in each thread | |
| for (var t in threads) { | |
| var thread = threads[t]; | |
| // Grab the task data | |
| // This email's subject will be task's title. | |
| var taskTitle = thread.getFirstMessageSubject(); | |
| // This email's body will be task's body. Most email message now is HTML-based. | |
| // It is desirable to remove all HTML tags so that only actual content will be populated. | |
| // Use a custom function getTextFromHtml to strip out all HTML tags. | |
| // In addition, the string size will be limited to 3000 characters in case an email is very long. | |
| var taskNote = getTextFromHtml((thread.getMessages()[0]).getBody()).substring(0,3000); | |
| // Insert the task | |
| //addTask_(taskTitle, TASKLIST); | |
| addTask_(taskTitle, taskNote, TASKLIST); | |
| // Set to 'done' by exchanging labels | |
| thread.removeLabel(label_pending); | |
| thread.addLabel(label_done); | |
| } | |
| // Increment the processed tasks count | |
| var processedRange = sheet.getRange("B1"); | |
| processedRange.setValue(processedRange.getValue() + threads.length) | |
| } | |
| // ----------------------------------------------------- | |
| // addTask_(title, taskListId) | |
| // Create new task and insert into given tasklist | |
| // ----------------------------------------------------- | |
| function addTask_(title, message, tasklistId) { | |
| //var newTask = Tasks.newTask().setTitle(title); | |
| var newTask = Tasks.newTask().setTitle(title).setNotes(message); | |
| Tasks.Tasks.insert(newTask, getTasklistId_(tasklistId)); | |
| } | |
| // ----------------------------------------------------- | |
| // main() | |
| // Starter function; to be scheduled regularly | |
| // ----------------------------------------------------- | |
| function main_taskconverter() { | |
| // Get the active spreadsheet and make sure the first | |
| // sheet is the active one | |
| var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
| var sh = ss.setActiveSheet(ss.getSheets()[0]); | |
| // Process the pending task emails | |
| processPending_(sh); | |
| } | |
| // Strip out all HTML tags. | |
| // Source: http://stackoverflow.com/questions/8936092/remove-formatting-tags-from-string-body-of-email | |
| function getTextFromHtml(html) { | |
| return getTextFromNode(Xml.parse(html, true).getElement()); | |
| } | |
| function getTextFromNode(x) { | |
| switch(x.toString()) { | |
| case 'XmlText': return x.toXmlString(); | |
| case 'XmlElement': return x.getNodes().map(getTextFromNode).join(''); | |
| default: return ''; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment