我有一个脚本可以将来宾添加到电子表格中的事件中,但它只是从循环中添加第一个来宾。记录器显示人员列表,但日历中只添加一个。这是我的代码:
for (var i = 0; i < registrationData.length; i++) {
var session = registrationData[i][0];
var email = registrationData[i][2];
var eventId = session.toString().replace("@google.com","");
var event = Calendar.Events.get(sharedCalendar, eventId);
var add = calendarApp.getCalendarById(sharedCalendar).getEventSeriesById(session).addGuest(email);
我还创建了一个包含新日历高级服务的链接,这些服务在电子邮件中发布,但我只是收到了从用户日历中删除该事件的消息。这可能是因为他们没有作为嘉宾首先正确添加吗?
var link = Calendar.Events.get(usersCalendar, eventId).htmlLink;
感谢任何可以提供帮助的人!
编辑:使用Serge的精彩帮助进行新的工作循环
for (var i = 0; i < registrationData.length; i++) {
var guestEmail = registrationData[i][2];
var eventId = registrationData[i][0];
var session = eventId.toString().replace("@google.com","");
var event = CalendarApp.getCalendarById(sharedCalendar).getEventSeriesById(eventId);
var link = Calendar.Events.get(sharedCalendar, session).htmlLink;
event.addGuest(guestEmail);
Logger.log('guest for i='+i+' is '+guestEmail);
}
答案 0 :(得分:1)
编辑:关注您的修改:
尝试这样:
for (var i = 0; i < registrationData.length; i++) {
var session = registrationData[i][0];
var eventId = session.toString().replace("@google.com","");// is that really the event Id ?
var event = sharedCalendar.getEventSeriesById(session);
var inviteList = registrationData[i][2].split(',');//assuming your guestlist is comma separated
if (inviteList.length>0){
for(var n in inviteList){
event.addGuest(email);
}
}
}
尝试添加一些日志来检查您的数据,修改下面的代码:
for (var i = 0; i < registrationData.length; i++) {
var email = registrationData[i][2];
var session = registrationData[i][0];
var event = CalendarApp.getCalendarById(sharedCalendar).getEventSeriesById(session);
var inviteList = email.split(',');//assuming your guestlist is comma separated
Logger.log('email='+email+' ID='+session);
if (inviteList.length>0){
for(var n in inviteList){
var link = event.htmlLink;// I don't know this method ... where is it documented ?
Logger.log('event Title='+event.getTitle()+' - guest='+inviteList[n]);
event.addGuest(inviteList[n]);
}
}
检查登录菜单/查看/日志以查看您的数据是否正确
为什么要使用2个电话,一个用于calendarApp,另一个用于日历(高级)?你可以写calendarApp.getCalendarById(sharedCalendar).getEventSeriesById(session).addGuest(email)
下面是我前一段时间写的脚本,它会自动邀请所有电子邮件都在说明中的人。它有一个实际添加客人的循环。而且效果很好。
A spreadsheet example(只读,复制使用)
// update the ID below to your copy ID and run the Callist() function to get the calendars ID on first sheet.
//set up an onOpen trigger for the myOnOpen function
var ss = SpreadsheetApp.openById('1xDOaoSl3HbkS95cj8Jl-82rdiui7G0sFz96PIO6iVF4');// this spreadsheet
var calNamesSheet = ss.getSheetByName('calNames');
var calList = calNamesSheet.getDataRange().getValues();
function MyOnOpen() {
var menuEntries = [ {name: "Lauch autoInvite on all listed calendars", functionName: "autoInviteAllCals"},
{name: "delete created sheets", functionName: "delsheets"}
];
ss.addMenu("Tracking utilities",menuEntries);//
}
function autoInviteAllCals(){
var today = new Date(); // now
var startDate = new Date(today.setHours(0,0,0,0));// today @ 0 AM
var endDate = new Date(new Date(startDate).setDate(startDate.getDate()+7)); // adjust time frame to read here = 7 days
for(var nn=0;nn<calList.length;nn++){
var logArray = new Array();
logArray.push(['Calendar + Title','Description','Start','End','Location','Creators','Date Created','Duration','Guests']);
var calName = calList[nn][0];
var calId = calList[nn][1];
var Calendar = CalendarApp.getCalendarById(calId);
var events = Calendar.getEvents(startDate , endDate);
if (events[0]) {
for (var i = 0; i < events.length; i++) {
var row = new Array();
row.push(calName +' : '+events[i].getTitle());
row.push(events[i].getDescription());
row.push(Utilities.formatDate(events[i].getStartTime(), Session.getScriptTimeZone(), "MMM-dd-yy")+' @ ' +Utilities.formatDate(events[i].getStartTime(), Session.getScriptTimeZone(), "HH:mm"));
row.push(Utilities.formatDate(events[i].getEndTime(), Session.getScriptTimeZone(), "MMM-dd-yy")+' @ ' +Utilities.formatDate(events[i].getEndTime(), Session.getScriptTimeZone(), "HH:mm"));
row.push(events[i].getLocation());
row.push(events[i].getCreators().join());
row.push('on '+Utilities.formatDate(events[i].getLastUpdated(), Session.getScriptTimeZone(), "MMM-dd-yyyy"));
row.push(((events[i].getEndTime() - events[i].getStartTime()) / 3600000)+' hours');//duration
var inviteList = checkInvites(events[i]);
if (inviteList.length==0){ // if guests were found in checkInvites() then don't read it from event since checkInvites() added them to the cal but this event is not yet updated
var list = events[i].getGuestList();
for(n=0;n<list.length;++n){inviteList.push(list[n].getEmail())};
}else{
for(var n in inviteList){
events[i].addGuest(inviteList[n]);
}
}
row.push(inviteList.join(', '));
logArray.push(row);
}
}
// Logger.log(logArray);
if(logArray.length==0){continue};
try{
var sheetToWrite = ss.insertSheet(calName,ss.getNumSheets());// create sheet if doesn't exist
}catch(err){
var sheetToWrite = ss.getSheetByName(calName);// else open it
}
sheetToWrite.getRange(1,1,logArray.length,logArray[0].length).setValues(logArray).setHorizontalAlignment('left'); // enhance formating
sheetToWrite.getRange(1,1,1,logArray[0].length).setBackground('#EEA').setBorder(true,true,true,true,true,true).setHorizontalAlignment('left').setFontSize(12);
for(var w in logArray[0]){
sheetToWrite.setColumnWidth(Number(w)+1,180);
}
}
}
function checkInvites(event){
var email = [];
// below are 2 different regex to get emails from the title string.They return an array of valid emails.
//both work very well, I don't really know why choose one or the other..., make your choice !
var title = event.getTitle();
if(title.indexOf('@')==-1){return email};
email = title.match(/([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})/g);
Logger.log('email var1 = '+JSON.stringify(email));
var regex = /(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/gm
email = title.match(regex);
Logger.log('email var2 = '+JSON.stringify(email));
return email;
}
function delsheets(){
var numSheets = ss.getNumSheets()-1;
for(var n = numSheets ; n>0 ; n--){
if(ss.getSheets()[n].getName() != 'calNames'){
ss.deleteSheet(ss.getSheets()[n]);
Utilities.sleep(400);
}
}
}
// This small function is to get the list of calendar names & Ids that you have access to, please edit the calNames sheet to keep only the ones you want to monitor (without empty rows).
function Callist(){
calNamesSheet.getDataRange().clearContent();
var list = new Array();
var store = new Array();
list = CalendarApp.getAllCalendars()
for (n=0;n<list.length;++n){
var name = list[n].getName() ;
var id = list[n].getId() ;
store.push( [name,id])
}
calNamesSheet.getRange(1,1,store.length,store[0].length).setValues(store);
}
// Serge insas - 08-2014