【问题标题】:Create two Google Calendar events from one Google Form submission从一个 Google 表单提交创建两个 Google 日历活动
【发布时间】:2017-05-01 19:15:56
【问题描述】:

我正在尝试使用 Google 表单和 Google 日历创建团队 On-call 调度程序。每个团队都可以选择同时拥有主要和次要待命人员。到目前为止,我已经能够成功获取将主要选项放入 Google 日历的代码,但我无法弄清楚如何获取代码以将次要选项放入日历中(如果已选择)。我已经从其他人那里寻找答案并且找不到任何东西,或者我只是不明白,因为我对编码很陌生。提前感谢任何可以提供帮助的人。

我应该清楚并感谢一位名叫 Jesse Spevack (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;   
}

【问题讨论】:

  • 您可能不想使用附加组件,但如果这是您可以接受的解决方案,您可以尝试:Chrome Web Store
  • 感谢您的意见,很遗憾,我无法使用插件,因为它们已被我们的 Google Apps 管理员阻止。

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


【解决方案1】:

更改此功能:

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();

【讨论】:

  • 您好桑迪,感谢您的意见,这是我考虑创建两个日历的想法,一个用于主要日历,另一个用于辅助日历。但是,我真的希望将所有内容都放在一个日历中以方便使用。知道怎么做吗?
猜你喜欢
  • 1970-01-01
  • 2023-04-04
  • 2014-10-22
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 2019-10-24
  • 2017-01-24
  • 1970-01-01
相关资源
最近更新 更多