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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将Google表单表单回复组合到Google日历活动的说明中相关的知识,希望对你有一定的参考价值。

我是脚本新手,并且一直试图将它们放在一起,而无需购买扩展程序或使用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); 
**/ 

如果将表单电子表格的额外列添加到日历建议中,请提供任何提示或建议,我们将不胜感激!

答案

首先,因为你只需要组合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;   
}

以上是关于将Google表单表单回复组合到Google日历活动的说明中的主要内容,如果未能解决你的问题,请参考以下文章

Google 表单:将数据发送到电子表格

Google Calendar API v3 - 使用硬编码凭据进行身份验证

添加到联系人,例如添加到日历按钮/ html/ Ajax 选项

Google 电子表格 - 用于编辑和附加新数据的主表副本

php 使用AJAX将google recaptcha添加到我们的表单中

Google Forms文件上传完整示例