Monday, October 21, 2013

Function for quick adding tasks or events to google calendar.

I have written a function so that you can quick add tasks as well as events to google calendar, using Google script.

"use strict";
/*
01234567890123456789012345678901234567890123456789012345678901234567890123456789
*/
/*
 * @author Matthias Pall Gissurarson
 * @usage quickAddTaskOrEventFromString(quickadd);
 * @param quickadd A String describing the event to quickadd
 */
function quickAddTaskOrEventFromString(quickadd) {
  //Add a task if the string includes " due ", otherwise add an event.
  if(quickadd.indexOf(" due ") !== -1){
    //Use the already implemented quickAdd to create an event.
    var event = CalendarApp.createEventFromDescription(quickadd);
    //This event gets automatically added to the users calendar, remove it.
    event.deleteEvent();

    /*
    Create a new task, and have it have the same properties as the event.
    Tasks don't have dueTime as of now, so we add that to the note of the task.
    Also add the events location and description, these are often empty.
    */
    var task = Tasks.newTask();
    task.setDue(event.getStartTime().toISOString());
    task.setTitle(event.getTitle());

    task.setNotes([event.getStartTime().toString(),
                   event.getLocation(),
                   event.getDescription()].join("\n"));

    //Add to the users default tasklist.
    Tasks.Tasks.insert(task,'@default');
  } else {

    //Add the event as normal
    CalendarApp.createEventFromDescription(quickadd);
  } 
}

function main() {
  var s = "Homework due tomorrow";
  var e = quickAddTaskOrEventFromString(s);
}
This function takes in a string, and if it contains " due ", it creates a task instead of an event on the default task list. Might this get implemented in the actual calendar?
I have posted a topic for this on the Google product forums. Let's see what happens.