将Google表单表单回复组合到Google日历活动的说明中

时间:2018-03-08 22:57:50

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

我是脚本新手,并且一直试图将它放在一起而无需购买扩展程序或使用chrome附加程序。

我希望不仅可以使用Goggle脚本表单响应,还可以使用表单响应电子表格的额外列(我在其他列中操作了一些响应)并使用它来设置日历约会其描述是操纵数据的列(不是数据的原始响应列)。在下面的示例中,我希望日历描述为[Combo Description]列,表示两个表单响应的arrayformula组合:[Event Description]和[New Description],每个表单响应出现在定义的日历中。

这是我试图修改的一个例子,是我试图做的更详细的简化版本。

我的示例表单就在这里 https://docs.google.com/forms/d/1EcafKYmstMiPcIhYpEsnvmf47yyeWNiYZIyxr93QkPU/edit?usp=sharing

支持谷歌电子表格在这里: https://docs.google.com/spreadsheets/d/1db6n-d88KrCWKXMuaFYYOU75pNOAysqwBuc7aORkmKE/edit?usp=sharing

我正在使用以下网站的修改版脚本: 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 : "1EcafKYmstMiPcIhYpEsnvmf47yyeWNiYZIyxr93QkPU",  

  //the id of the calendar we will create events on
  calendarId : "81hoju2sgoldcjom4888opngk0@group.calendar.google.com",


  //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); 
**/ 

如果有任何关于如何将表单电子表格的额外列添加到日历建议中的提示或建议,将非常感谢!!!

1 个答案:

答案 0 :(得分:0)

首先,由于您只需合并New DescriptionEvent Description项目来创建新描述。您不需要使用数组公式和Google工作表来组合它。您可以在应用程序脚本本身中执行此操作。

<强>修饰:
1)您必须从表单中检索此新项目(新描述)。您将修改您的formmap对象,以包含此新项目

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)您将获得用户在switch case语句中创建的新条目,如下所示:

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)然后制作组合描述,您只需加入字符串描述和新描述。

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

使用这个新的comboDescription来创建日历活动。

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

完成所有这些修改后,您的最终代码将是:

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 : "81hoju2sgoldcjom4888opngk0@group.calendar.google.com",


  //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;   
}