如何为Google Forms转Calendar事件设置专属颜色
表单提交生成的日历事件设置专属蓝色的修正方案
原脚本的代码错误点
- Google Apps Script提供的
calendar.createEvent()方法仅支持4个有效入参:事件标题、开始时间、结束时间、可选配置对象,额外传入的第5个颜色参数会被系统直接丢弃,完全起不到设置颜色的作用 - 原代码里写的
event.setColor = ('5')是纯错误语法:这行代码只是给事件对象的setColor属性赋值了一个字符串,根本没有调用事件实例自带的颜色设置方法,实际不会对事件颜色产生任何修改 - 异常捕获分支(当表单填的邮箱格式无效时,触发的不带访客参数的兜底创建逻辑)没有补充颜色设置代码,走这个分支生成的事件会完全沿用日历默认的红色,没法实现表单事件统一为蓝色的要求
- 靠硬编码数字指定颜色很容易出现匹配偏差,直接用框架内置的颜色常量可以彻底规避这个问题
修正逻辑说明
- 不管走哪个分支创建事件,拿到返回的事件对象实例后,直接调用实例自带的
setColor()方法指定颜色 - 所有表单生成的事件都统一设置为蓝色,和日历里手动添加的红色默认事件形成区分
- 不需要记具体的颜色数字ID,直接用
CalendarApp.Color.BLUE常量就能指定蓝色
修正后的完整可运行脚本
var GLOBAL = { //the id of the form we will use to create calendar events formId : "xxx", //the id of the calendar we will create events on calendarId : "xxx", //a mapping of form item titles to sections of the calendar event formMap : { eventTitle: "Mille võtad", startTime : "Millal võtad", endTime: "Mis ajal tagastad", description: "Märkused, lisainfo", location: "Asutus", email: "Kes võtab", }, } 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; 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, }; var createdEvent; try { //create a calendar event with given title, start time, //end time, and description and guests stored in an //options argument createdEvent = calendar.createEvent(title, startTime, endTime, options) // 给正常创建的事件设置蓝色 createdEvent.setColor(CalendarApp.Color.BLUE); } 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 createdEvent = calendar.createEvent(title, startTime, endTime, options) // 给兜底分支创建的事件同样设置蓝色,避免颜色不一致 createdEvent.setColor(CalendarApp.Color.BLUE); } return createdEvent; }
备注:如果后续需要调整表单事件的颜色,直接替换
setColor()方法里的颜色常量即可,框架支持的所有事件颜色常量都对应明确的视觉效果,不需要硬记数字ID。
内容的提问来源于stack exchange,提问作者Margo Metsoja
相关产品推荐
相关产品推荐

