从电子表格创建事件
getEvent()
读取下面的电子表格,该电子表格与您提供的电子表格非常相似,我将对其进行一些修改。我添加了事件标题和描述列。 Scheduled列提供了防止再次运行脚本时重复事件的功能。计划列是一个数据验证(“是” /“否”)。
function getEventData() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('EventData');
var sr=4;//data start row
var lc=8;//last column
var hA=sh.getRange(sr-1,1,1,lc).getValues()[0];
var idx={};
hA.forEach(function(e,i){idx[e]=i;});//This creates an object for identifying array indices through the use of Column header name making it a bit easier to associate data with their value descriptions
var rg=sh.getRange(sr,1,sh.getLastRow()-sr+1,lc );
var evA=rg.getDisplayValues();
evA.forEach(function(r,i){
if(r[idx.Scheduled]!='Yes') {
var eObj={};
eObj.start=r[idx.Start];
eObj.end=r[idx.End];
eObj.title=r[idx.Title];
eObj.description=Utilities.formatString('<b>Name:</b> %s,%s<br /><b>Age:</b> %s<br /><b>Description:</b> %s',r[idx.Last],r[idx.First],r[idx.Age],r[idx.Description]);
if(createEventWithReminders(eObj)) {
sh.getRange(i+sr,idx.Scheduled+1).setValue('Yes');
}else{
sh.getRange(i+sr,idx.Scheduled+1).setValue('No');
}
}
});
}
createEventWithReminders函数获取对象并在您指定的任何日历上创建一个事件,并使用该日历ID必须提供
//{start:dd/MM/yyyy,end:dd/MM/yyyy,title:string,description:string}
function createEventWithReminders(eObj) {
if(eObj.start && eObj.end && eObj.title && eObj.description) {
var minute=1;
var hour=60*minute;
var day=24*hour;
var week=7*day;
var cal=CalendarApp.getCalendarById('Enter your calendar id');
var sA=eObj.start.split('/');//splits the date string into fullyear,month and day
var start=new Date(sA[2],sA[1]-1,sA[0]);//creates the start date notice the month-1
var eA=eObj.end.split('/');
var end=new Date(eA[2],eA[1]-1,eA[0]);
var event=cal.createEvent(eObj.title, start, end);
event.setDescription(eObj.description)
event.setTitle(eObj.title)
event.addEmailReminder(day);//creates a one day prior reminder
event.addEmailReminder(week);//creates a one week prior reminder
return true;
}else{
return false;
}
}