Upload to Calendar script

    /*
    This code is adapted from examples and directions given in
    http://code.google.com/googleapps/appsscript/class_calendar.html
    http://code.google.com/googleapps/appsscript/articles/getting-started-with-sites.html
    http://code.google.com/googleapps/appsscript/class_listitem.html


    #####################
    # Preconditions:
    #####################

    #1# Create a Google calendar that will host the tasks that you want to
    keep track of using a Google Sites list page. Note the calendar's ID
    (see http://drupal.org/node/589310 for how to do this).

    #2# Note the name of the page that holds the list. In my case, there are two:
    https://sites.google.com/site/pmtemplatebydiegomaranan/goals
    https://sites.google.com/site/pmtemplatebydiegomaranan/tasks
    In this script I use two calendars, one for each list

    #3# Make sure that all the other members of your group
    have access to your calendar.

    #####################
    # Pseudocode
    #####################
    Open the calendar
    Delete all existing events in the calendar
    For each entry in the list page, make a new entry in the calendar
    */

    ///////////////////////////
    // doGet(e) is a required
    // function that needs to be overwritten
    ///////////////////////////
    function doGet(e){
    var app = UiApp.createApplication();
    // First calendar
    deleteAndUpdate("18fe6d5okjhtm5vklicgh8ujks@group.calendar.google.com", "tasks", app, "TASK");
    // Second calendar
    deleteAndUpdate("47kd9b5o696qjplipe40p52ccc@group.calendar.google.com", "goals", app, "GOAL OR MILESTONE");
    return app;
    }


    ///////////////////////////
    // The important bit
    ///////////////////////////
    function deleteAndUpdate(calendarID, pageName, app, columnName)
    {
    // Delete all calendar events
    var cal = CalendarApp.getCalendarById(calendarID);
    print(": Deleting existing tasks in calendar ...", app);
    var events = cal.getEvents(new Date("January 1, 1950 00:00:00"), new Date("December 31, 2050 00:00:00"));
    for (j = 0; j < events.length; j++)
    {
    print(": : Deleting event [ = " + events[j].getTitle() + "] (UID: " + events[j].getTag("UID") + ")", app);
    events[j].deleteEvent();
    }

    //Grab the tasks from the task lists
    var site = SitesApp.getSite("site", "project4bprojectmanagement");
    var page = site.getChildByName(pageName);
    var items = page.getListItems();
    var dateLastUpdatedInList;
    var uid = "";

    print(": Adding tasks to calendar...", app);

    for (i=0; i < items.length;i++)
    {
    // Add the task to the calendar

    // ... task name
    var entryName = items[i].getValueByName(columnName);
    if (!entryName || (entryName.length < 1)) entryName = "(untitled)";
    //... task due date
    var due;
    var tempdate = items[i].getValueByName('DUE DATE');
    if (!tempdate)
    {
    due = new Date();
    print(": : : " + columnName + " has no due date. Temporarily assigning due date to today. PLEASE MAKE EVERY ENTRY HAS AN ASSIGNED DUE DATE.", app);
    }
    else
    due = new Date(tempdate.substring(0, 4),tempdate.substring(5, 7)-1, tempdate.substring(8), 0,0,0, 0);
    //... task ownership
    var owner = items[i].getValueByName('ASSIGNED TO');
    if (!owner) owner = "";
    //... task description
    var desc = items[i].getValueByName('WHERE CAN YOU FIND MORE INFO?');
    if (!desc) desc = "";
    //... task priority
    var priority = items[i].getValueByName('PRIORITY');
    if (!priority) priority = "";
    //... task status
    var status = items[i].getValueByName('STATUS');
    if (!priority) priority = "";
    var details = "Assigned to: " + owner + "\nPriority: " + priority + "\nStatus: " + status + "\nDetails: " + desc + "\n";

    // Inform the user of what's happening
    print(": : Adding " + columnName + " [" + entryName + "] to calendar, due " + due.getFullYear() + "-" + due.getMonth() + "-" + due.getDate(), app);
    var event = cal.createAllDayEvent(entryName, due, {description:details});
    event.setTag("UID", uid);

    }


      // Done! print("=====================================", app);
    print("= " + columnName + " CALENDAR UPDATE COMPLETE!", app);
    print("= GO to 'Timeline' to see the updated calendar", app);
    print("=====================================", app);
    }


    function print(x, app){
    var l = app.createLabel(x).setStyleAttribute("fontSize","14px");
    app.add(l);
    }


    // UID generator that we use to tag each unique task. Just for fun.
    function S4() {
    return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
    }
    function guid() {
    return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
    }