Javascript 谷歌日历事件的颜色
我得到了一个脚本,它接受了谷歌表单的回复,并根据提交的内容创建了一个活动,供学校教师预订房间和设备。 我想将这些新事件添加到现有日历中,其中已经有一些事件以不同颜色手动添加。例如,日历的主颜色是红色,但我希望表单中的事件显示为蓝色 因为我没有学习Javascript,所以我无法找到插入指定新添加事件颜色的代码类型的位置Javascript 谷歌日历事件的颜色,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,我得到了一个脚本,它接受了谷歌表单的回复,并根据提交的内容创建了一个活动,供学校教师预订房间和设备。 我想将这些新事件添加到现有日历中,其中已经有一些事件以不同颜色手动添加。例如,日历的主颜色是红色,但我希望表单中的事件显示为蓝色 因为我没有学习Javascript,所以我无法找到插入指定新添加事件颜色的代码类型的位置 var GLOBAL = { //the id of the form we will use to create calendar events formId :
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 eventColour = 1
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, eventColour)
event.setColor = ('5');
colorID = 1
} 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;
}
变量全局={
//用于创建日历事件的表单的id
formId:“xxx”,
//我们将在其上创建活动的日历的id
日历ID:“xxx”,
//表单项标题到日历事件部分的映射
formMap:{
活动名称:“Mille võtad”,
开始时间:“米拉尔·沃塔德”,
结束时间:“Mis ajal tagastad”,
描述:“马尔库兹,利萨因福”,
地点:“阿修斯”,
电子邮件:“Kes võtab”,
},
}
函数onFormSubmit(){
var eventObject=getFormResponse();
var event=createCalendarEvent(eventObject);
}
函数getFormResponse(){
//通过使用
//存储在全局变量对象中的表单id
var form=FormApp.openById(GLOBAL.formId),
//从表单中获取所有响应。
//此方法返回表单响应的数组
responses=form.getResponses(),
//查找响应数组的长度
长度=响应。长度,
//查找最新表单响应的索引
//由于数组的索引为零,因此最后一个响应
//是响应的总数减去1
lastResponse=响应[长度-1],
//获取每个问题项的回答数组
//在被申请人提供答复的表格内
itemResponses=lastResponse.getItemResponses(),
//创建一个空对象以存储上一个
//形式反应
//将用于创建日历事件的
eventObject={};
//循环遍历项目响应数组中的每个项目响应
for(var i=0,x=itemResponses.length;i是一种方法,因此应该将值作为函数传递给它,而不是用等号赋值
event.setColor(5);
请看一下枚举
- 要使事件
为蓝色
,需要为其指定值9
- 此外,在创建事件时,不能将通风颜色作为参数之一传递
- 就是
var event=calendar.createEvent(标题、开始时间、结束时间、选项、事件颜色)
行不通
- 相反,在创建事件后将颜色传递给事件
样本:
var eventColor=9;
var event=calendar.createEvent(标题、开始时间、结束时间、选项);
event.setColor(eventColor);
你提供的代码是你的吗?你有没有努力使它适应你的需要?你的困难是什么?我没有完整地编写这段代码。我是从一个例子中得到的。我需要找到一个放置的位置和一段插入的代码,以便能够编辑创建的事件的颜色。是的,你可以因此,请传递enum.setColor(CalendarApp.EventColor.BLUE)