title: How can I autoupdate events in google calendar from sheets?
body: <p>I have a script that generates an event when a form response is submitted. But I also want it to update an event by adding an email as a guest, if the event is at the same time. </p>
  
  <p>I think I need to add an If statement and go that route but I'm not sure thanks for all your help</p>
  
  <p>I followed a tutorial to get this far and it is: <a href="http://www.jessespevack.com/blog/2016/2/9/turn-a-google-form-response-into-a-calendar-event" rel="nofollow noreferrer">Turn a google form response into a calendar event</a></p>
  
  <p>my code:</p>
  
  <pre><code>//Load the Moment.js library once.
  var moment = Moment.load();
  var GLOBAL = {
    //the id of the form we will use to create calendar events 
    formId : ("1SfePtYKkseN3Pzdy0wxqVoStKMCJuijW9NT0W9aI1qQ"),
    //the id of the calendar we will create events on
    calendarId : "processing@marketingmobileads.com",
  
    //a mapping of form item titles to sections of the calendar event
    formMap : {
      eventTitle: "Event Title",
      startTime : "Start",
      endTime: "End",
      email: "Email Address",
    },
  }
  function onFormSubmit() {
    var eventObject = getFormResponse();
    var event = createCalendarEvent(eventObject);
  }
  function getFormResponse() {
    // Get a form object by opening the form using the
    // form id stored in the GLOBAL variable object
    var form = FormApp.openById(GLOBAL.formId),
        //Get all responses from the form. 
        //This method returns an array of form responses
        responses = form.getResponses(),
        //find the length of the responses array
        length = responses.length,
        //find the index of the most recent form response
        //since arrays are zero indexed, the last response 
        //is the total number of responses minus one
        lastResponse = responses[length-1],
        //get an array of responses to every question item 
        //within the form for which the respondent provided an answer
        itemResponses = lastResponse.getItemResponses(),
        //create an empty object to store data from the last form response
        //that will be used to create a calendar event
        eventObject = {};
    //Loop through each item response in the item response array
    for (var i = 0, x = itemResponses.length; i&lt;x; i++) {
      //Get the title of the form item being iterated on
      var thisItem = itemResponses[i].getItem().getTitle(),
          //get the submitted response to the form item being
          //iterated on
          thisResponse = itemResponses[i].getResponse();
      //based on the form question title, map the response of the 
      //item being iterated on into our eventObject variable
      //use the GLOBAL variable formMap sub object to match 
      //form question titles to property keys in the event object
      switch (thisItem) {
        case GLOBAL.formMap.eventTitle:
          eventObject.title = thisResponse;
          break;
        case GLOBAL.formMap.startTime:
          eventObject.startTime = thisResponse;
          break;
        case GLOBAL.formMap.endTime:
          eventObject.endTime = thisResponse;
          break; 
        case GLOBAL.formMap.email:
          eventObject.email = thisResponse;
          break;
      }
    }
    return eventObject;
  }
  function createCalendarEvent(eventObject) {
    //Get a calendar object by opening the calendar using the
    //calendar id stored in the GLOBAL variable object
    var calendar = CalendarApp.getCalendarById(GLOBAL.calendarId),
        //The title for the event that will be created
        title = eventObject.title,
        //The start time and date of the event that will be created
        startTime = moment(eventObject.startTime).toDate(),
        //The end time and date of the event that will be created
        endTime = moment(eventObject.endTime).toDate();
        //an options object containing the description and guest list
        //for the event that will be created
    var options = {
        guests : eventObject.email,
    };
    try {
      //create a calendar event with given title, start time,
      //end time, and description and guests stored in an 
      //options argument
      var event = calendar.createEvent(title, startTime, endTime, options)
    } catch (e) {
      //delete the guest property from the options variable, 
      //as an invalid email address with cause this method to 
      //throw an error.
      delete options.guests
      //create the event without including the guest
      var event = calendar.createEvent(title, startTime, endTime, options)
    }
    return event;   
  }
  </code></pre>