google apps script - Adding guests to calendar event -
google apps script - Adding guests to calendar event -
i have script adds guests event spreadsheet, adding first invitee loop. logger shows list of people, yet 1 gets added in calendar. here code:
for (var = 0; < 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 together = calendarapp.getcalendarbyid(sharedcalendar).geteventseriesbyid(session).addguest(email);
i create link new calendar advanced services goes out in email, yet message event deleted users calendar. because aren't beingness added invitee first?
var link = calendar.events.get(userscalendar, eventid).htmllink;
thanks can help!
edit: new working loop serge's fabulous help
(var = 0; < 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 i='+i+' '+guestemail); }
edit : next edit :
try :
for (var = 0; < registrationdata.length; i++) { var session = registrationdata[i][0]; var eventid = session.tostring().replace("@google.com","");// event id ? var event = sharedcalendar.geteventseriesbyid(session); var invitelist = registrationdata[i][2].split(',');//assuming guestlist comma separated if (invitelist.length>0){ for(var n in invitelist){ event.addguest(email); } } }
edit 2 adding logs , adapting code try add together few logs check data, modified code below :
for (var = 0; < 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 guestlist comma separated logger.log('email='+email+' id='+session); if (invitelist.length>0){ for(var n in invitelist){ var link = event.htmllink;// don't know method ... documented ? logger.log('event title='+event.gettitle()+' - guest='+invitelist[n]); event.addguest(invitelist[n]); } }
check log in menu/view/logs see if info right (or not)
why using 2 calls, 1 calendarapp , calendar (advanced) ? write calendarapp.getcalendarbyid(sharedcalendar).geteventseriesbyid(session).addguest(email)
below script wrote time ago invites people who's emails in description automatically. has loop adds guests. , works nicely.
a spreadsheet example (read only, create re-create use)
// update id below re-create id , run callist() function calendars id on first sheet. //set onopen trigger myonopen function var ss = spreadsheetapp.openbyid('1xdoaosl3hbks95cj8jl-82rdiui7g0sfz96pio6ivf4');// spreadsheet var calnamessheet = ss.getsheetbyname('calnames'); var callist = calnamessheet.getdatarange().getvalues(); function myonopen() { var menuentries = [ {name: "lauch autoinvite on listed calendars", functionname: "autoinviteallcals"}, {name: "delete created sheets", functionname: "delsheets"} ]; ss.addmenu("tracking utilities",menuentries);// } function autoinviteallcals(){ var today = new date(); // var startdate = new date(today.sethours(0,0,0,0));// today @ 0 var enddate = new date(new date(startdate).setdate(startdate.getdate()+7)); // adjust time frame 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]) { (var = 0; < 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 found in checkinvites() don't read event since checkinvites() added them cal event 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 } 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 2 different regex emails title string.they homecoming array of valid emails. //both work well, don't know why take 1 or other..., create selection ! 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)); homecoming 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); } } } // little function list of calendar names & ids have access to, please edit calnames sheet maintain ones want monitor (without empty rows). function callist(){ calnamessheet.getdatarange().clearcontent(); var list = new array(); var store = new array(); list = calendarapp.getallcalendars() (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
google-apps-script google-calendar
Comments
Post a Comment