Difference between revisions of "Create Google Tasks by sending email to Google GMail"

From Ittichai Chammavanijakul's Wiki
Jump to navigation Jump to search
Line 23: Line 23:
 
         var taskTitle = thread.getFirstMessageSubject();   
 
         var taskTitle = thread.getFirstMessageSubject();   
 
         var taskNote = getTextFromHtml((thread.getMessages()[0]).getBody());
 
         var taskNote = getTextFromHtml((thread.getMessages()[0]).getBody());
 +
</pre>
 +
 +
Most email message now is HTML-based. Without stripping out the HTML tags, the task's note will be populated with many useless tags. I use a function from http://stackoverflow.com/questions/8936092/remove-formatting-tags-from-string-body-of-email. Basically, add these two functions in the script.
 +
 +
<pre>
 +
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 '';
 +
  }
 +
}
 
</pre>
 
</pre>
  

Revision as of 06:20, 10 August 2012

Goal:

  • Send email to a specified address - in this case to Gmail address named yourgmail+task@gmail.com. (If Gmail ID is ittichai, the email to send to will be ittichai+task@gmail.com.)
  • Script will parse that email based on Gmail label assigned by filter, and grab the email's subject to create a new Google task.

Follow this step-by-step of this page. http://www.pipetree.com/qmacro/blog/2011/10/automated-email-to-task-mechanism-with-google-apps-script/

This is just an overview of what happens:

  1. Create Gmail new labels - newtask and newtaskdone.
  2. Create Gmail filter based on newtask label.
  3. Create script from Google Spreadsheet.
  4. Integrate the script with Google API.
  5. Schedule it via the time-driven even trigger.

Pay attention to the *Integrate the script with Google API* in the *Script Code* section especially if this is your first time coding this. You will have to enable Google API service to allow access the Task API - https://developers.google.com/apps-script/articles/google_apis_reading_list. If not doing so, you will receive an error message saying "Tasks - not defined" when running the script.

In addition to above sample, I'd like to add the email's body into the G! task's note.

Here is what I do:

  • Add this new variable to grab the email's body
        // Grab the task data  
        var taskTitle = thread.getFirstMessageSubject();  
        var taskNote = getTextFromHtml((thread.getMessages()[0]).getBody());

Most email message now is HTML-based. Without stripping out the HTML tags, the task's note will be populated with many useless tags. I use a function from http://stackoverflow.com/questions/8936092/remove-formatting-tags-from-string-body-of-email. Basically, add these two functions in the script.

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 '';
  }
}
  • Add new parameter of task's note to addTask_
        // Insert the task  
        //addTask_(taskTitle, TASKLIST);  
        addTask_(taskTitle, taskNote, TASKLIST);  
  • Update the addTask_ to include setNotes
    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));  
    }