0
votes

I'm new to scripting and have been trying to put this together without having to purchase an extension or use a chrome add-on.

I want to be able to take not only Goggle script form responses, but also extra columns of the form responses' spreadsheet (where I have manipulated some of the responses in a different column) and use that to set a calendar appointment in which the description is the column(s) of manipulated data (not an original response column of data). In the example below, I want the calendar description to be the column [Combo Description] that represents an arrayformula combination of two form responses: [Event Description] and [New Description], which upon each form response appears in the defined calendar.

Here's an example I'm trying to modified is a simplified version of something more detailed I'm attempting to do.

My example form is here https://docs.google.com/forms/d/1EcafKYmstMiPcIhYpEsnvmf47yyeWNiYZIyxr93QkPU/edit?usp=sharing

The supporting google spreadsheet is here: https://docs.google.com/spreadsheets/d/1db6n-d88KrCWKXMuaFYYOU75pNOAysqwBuc7aORkmKE/edit?usp=sharing

I'm using a modified version of a script from the following website: http://www.jessespevack.com/blog/2016/2/9/turn-a-google-form-response-into-a-calendar-event

Here's a copy of my/his script:

//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 : "1EcafKYmstMiPcIhYpEsnvmf47yyeWNiYZIyxr93QkPU",  

  //the id of the calendar we will create events on
  calendarId : "[email protected]",


  //a mapping of form item titles to sections of the calendar event
  formMap : {
    eventTitle: "Event Title",
    startTime : "Event Date and Start Time",
    endTime: "Event Date and End Time",
    description: "Event Description",  // Tried the following: + , & concate col[6]:which didn't throw an error but didn't make an appointment either
    location: "Event Location",
    email: "Add Guests",
  },
}

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], // The -1 goes after length 
      //get an array of responses to every question item 
      //within the form for which the respondent provided an answer
      itemResponses = lastResponse.getItemResponses()  // Took a comma off here
      //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<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.description:
        eventObject.description = thisResponse;
        break;
      case GLOBAL.formMap.location:
        eventObject.location = 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 = {
    description : eventObject.description,
    guests : eventObject.email,
    location: eventObject.location,
  };
  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;   
  Logger.log(event);
}



/*** 
For later to make the endDate be a fixed 2hr appointment
var submittedDate = new Date(userSubmission);
var parsedDate = Date.parse(submittedDate); 
var endDate = new Date(parsedDate + 120000); 
**/ 

Please any hints or suggestions to how to get the extra columns of the form spreadsheet into the calendar suggestions would be greatly appreciated!!!

1
A much simpler way to access the latest form response is to capture the event object directly, i.e. onFormSubmit(e) - the e variable is very useful. Also review the Spreadsheet reference.tehhowch

1 Answers

0
votes

Firstly, since you just combine the New Description and Event Description items to create a new description. You don't need to use an array formula and google sheets to combine it. You can do it in the app script itself.

Modification:
1) You will have to retrieve this new item (New Description) from the form. You will modify your formmap object, to include this new item

formMap : {
    eventTitle: "Event Title",
    startTime : "Event Date and Start Time",
    endTime: "Event Date and End Time",
    description: "Event Description",  // Tried the following: + , & concate col[6]:which didn't throw an error but didn't make an appointment either
    location: "Event Location",
    email: "Add Guests",
    newdescription : "New Description"  //New entery
  }

2) You will obtain the new entry made by the user in switch case statement like so:

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.description:
        eventObject.description = thisResponse;
        break;
      case GLOBAL.formMap.location:
        eventObject.location = thisResponse;
        break;
      case GLOBAL.formMap.email:
        eventObject.email = thisResponse;
        break;
      case GLOBAL.formMap.newdescription: 
        eventObject.newdescription = thisResponse; //New description entry
        break;
    } 

3) Then to make the combo description you just join the strings description and newdescription.

var comboDescription = eventObject.description+" "+eventObject.newdescription

And use this new comboDescription to create your calendar event.

var comboDescription = eventObject.description+" "+eventObject.newdescription
var options = {
    description : comboDescription,
    guests : eventObject.email,
    location: eventObject.location,
  };

Once you make all these modifications, your final code will be:

var GLOBAL = {
  //the id of the form we will use to create calendar events 
  formId : "1EcafKYmstMiPcIhYpEsnvmf47yyeWNiYZIyxr93QkPU",  

  //the id of the calendar we will create events on
  calendarId : "[email protected]",


  //a mapping of form item titles to sections of the calendar event
  formMap : {
    eventTitle: "Event Title",
    startTime : "Event Date and Start Time",
    endTime: "Event Date and End Time",
    description: "Event Description",  // Tried the following: + , & concate col[6]:which didn't throw an error but didn't make an appointment either
    location: "Event Location",
    email: "Add Guests",
    newdescription : "New Description"  //New entery
  },
}

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], // The -1 goes after length 
      //get an array of responses to every question item 
      //within the form for which the respondent provided an answer
      itemResponses = lastResponse.getItemResponses()  // Took a comma off here
      //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<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.description:
        eventObject.description = thisResponse;
        break;
      case GLOBAL.formMap.location:
        eventObject.location = thisResponse;
        break;
      case GLOBAL.formMap.email:
        eventObject.email = thisResponse;
        break;
      case GLOBAL.formMap.newdescription: 
        eventObject.newdescription = thisResponse; //New description entry
        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 comboDescription = eventObject.description+" "+eventObject.newdescription
    var options = {
        description : comboDescription,
        guests : eventObject.email,
        location: eventObject.location,
      };
  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;   
}