从一个Google表单提交中创建两个Google日历活动

时间:2017-05-01 19:15:56

标签: javascript google-apps-script google-sheets google-calendar-api google-form

我正在尝试使用Google表单和Google日历创建一个Team On-​​call计划程序。每个团队都可以选择同时接听主要和辅助电话。到目前为止,我已经能够成功获取将主要选项放入Google日历的代码,但我无法弄清楚如何选择将代码放入日历中的代码。我已经搜索过其他人的回答而无法找到任何内容,或者我只是不明白,因为我对编码很新。提前感谢任何可能提供帮助的人。

我应该清楚地告诉一位名叫杰西·斯佩瓦克(http://www.jessespevack.com/blog/2016/2/9/turn-a-google-form-response-into-a-calendar-event)的绅士,他提供了基础。

到目前为止,这是我的代码......



//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 : "xxxxx",
  
  //the id of the calendar we will create events on
  calendarId : "xxxx",
  
  //a mapping of form item titles to sections of the calendar event
  formMap : {
    eventTitle: "Team Primary On-call",
    startTime : "Primary start date/time",
    endTime: "Primary end date/time",
    description: "Primary On-call Contact",
  },
}

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<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;
    } 
  }
  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,
  };
  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;   
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

更改此功能:

function onFormSubmit() {
  var eventObject = getFormResponse();
  var event = createCalendarEvent(eventObject);
}

要:

function onFormSubmit() {
  var eventObject, calendarTwoID;

  eventObject = getFormResponse();

  calendarTwoID = 'Put the ID of the second calendar here';

  createCalendarEvent(eventObject);//Create first event
  createCalendarEvent(eventObject, calendarTwoID);//Create second event
}

createCalendarEvent功能更改为:

function createCalendarEvent(eventObject, calendarID) {
  //Get a calendar object by opening the calendar using the
  //calendar id stored in the GLOBAL variable object
  var calendar;

  if (calendarID) {//If ID is passed in
    calendar = CalendarApp.getCalendarById(calendarID);//use the passed in ID
  } else {//Otherwise use the global ID
    calendar = CalendarApp.getCalendarById(GLOBAL.calendarId);
  }

  //The title for the event that will be created
  var 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();