Situation
I have a calendar with a lot of events on it (staff appraisals).
I've made a load of modifications (changing the length of the event etc.) but the invitations are going to people who have a lotus notes calendar (poor people).
This means that unless I trigger what would be called "Send notification?" in the click-with-your-mouse version of things, they have no way of knowing that the event has been updated.
(Similar Q)
In this example, the event I'm trying to trigger is the same one as is triggered when the Send Update? modal is accepted with send.
Code
Here's some example code that gets all the events on the Appraisals calendar and changes their location to 'the moon'.
function fixInvitations(){
//get the callendar named "Appraisals"
var cApp = CalendarApp.getCalendarsByName("Appraisals")[0];
var events = cApp.getEvents(new Date(), new Date("Dec 30 2014"));
for (eIndex in events){
var event = events[eIndex];
event.setLocation("the moon");
}
}
Question
How do I trigger an update to all parties invited to an event so that the changes are reflected in their calendars?
Currently these events are now on the moon, but the update hasn't told people who are on non-Google calendars about the change.
Helpful, but not that helpful fact
The update email that manually triggering sends contains a .ics file (Gist of the contents). This contains a VCALENDAR and a VEVENT. From the Wikipedia page on VEVENTs
For sending an UPDATE for an event the UID should match the original UID. the other component property to be set is:
SEQUENCE:<Num of Update>
I.e., for the first update:
SEQUENCE:1
So if there was a way to manually build an email with a .ics attachment it would solve the problem, but that feels like massive overkill. This is mentioned here but not resolved.
The Google Calendar API supports the flag sendNotifications for event updates:
https://developers.google.com/google-apps/calendar/v3/reference/events/update
I would file a feature request for App Script to expose the flag and in the meantime use the Calendar API directly for updating events just like an external API:
https://developers.google.com/apps-script/guides/services/external
I was struggling with this one a bit and got some help from ekoleda+devrel#google.com: Ability for Calendar to send email invitation to users added via addGuest http://code.google.com/p/google-apps-script-issues/issues/detail?id=574
A couple of pointers to hopefully save people some time:
Note that CalendarApp and the Advanced Calendar service use different
event ID formats:
CalendarApp returns event IDs with "#google.com" at the end ex. b3gv...a5jrs#google.com
The Advanced Calendar service/Calendar API expects an event ID that does not have #google.com ex. b3gv...a5jrs
Here's some working code:
function sendInvite(calendarId, eventId, email) {
var event = Calendar.Events.get(calendarId, eventId);
if(event.attendees) {
event.attendees.push({
email: email
});
} else {
event.attendees = new Array({email: email});
}
event = Calendar.Events.patch(event, calendarId, eventId, {
sendNotifications: true
});
}
One final note for those following along - you won't receive an email notification if you're the event owner and you're trying to add your own email address. This makes sense in the calendar world since you wouldn't go into your own calendar event, add yourself as an attendee, and expect to be prompted as to whether or not you want to inform yourself. However, it's probably a common way for people to try and test things (I don't have a domain test account and I can't always reliably test things with my personal gmail account). So if you're testing with your own account and thinking that things aren't working please keep this in mind. Once you try things with a non-event owner account the invite will be sent via email.
I think one solution is to create a spreadsheet with the column headers: Event ID, Event Name, Description, Start Time, End Time, Location,Guest List, and then add this script to the spreadsheet. calendarImport() will import all of your calendar events and write them to the sheet. Then, you can modify the event in the spreadsheet line (change location to the Moon), and run calendarCreateEvent to create a new event based on the changes you made. It will automatically send out notifications to all old attendees to accept the new event, as it is a new invitation.
I am pretty close to that solution, but I have a hangup, now, on accessing the calendar to create a new event. I think it's because I'm calling it from an onEdit event function. It's getting late, but if you rewrite the third part of the script to get the events data range, then a for/if loop to check the added versus modified date, then you should be able to create a new event, and delete the old event. This would push a notification of the change in event details to everyone who had accepted the old event already.
I think, given the code example you put up in the beginning, you probably already know how to do that, but if you don't, I could probably throw something together tomorrow night to search for old events that have been recreated and delete them.
Here's my spreadsheet and code in action.
function calendarImport(){
//http://www.google.com/google-d-s/scripts/class_calendar.html#getEvents
// The code below will retrieve events between 2 dates for the user's default calendar and
// display the events the current spreadsheet
var cal = CalendarApp.getDefaultCalendar();
var calId = cal.getId();
var sheet = SpreadsheetApp.getActiveSheet();
var sheetName = SpreadsheetApp.getActiveSheet().setName(calId +" Calendar Data");
var events = cal.getEvents(new Date("March 9, 2014"), new Date("March 14, 2014"));
for (var i=0;i<events.length;i++) {
//http://www.google.com/google-d-s/scripts/class_calendarevent.html
Logger.log(events);
var details=[[events[i].getId(),events[i].getTitle(), events[i].getDescription(), events[i].getStartTime(), events[i].getEndTime(),events[i].getLocation()]];
var guestList = events[i].getGuestList();
var guestArray = [];
for (var n in guestList){
var guestEmail = (guestList[n].getEmail());
guestArray.push(guestEmail);
Logger.log(guestArray);
}
var row=i+1;
var range=sheet.getRange(row+1,1,1,6);
range.setValues(details);
var guestRange = sheet.getRange(row+1,7,1,1);
guestRange.setValues([guestArray]);
var dateAdded = Utilities.formatDate(new Date(), "GMT-6","MM/dd/yy HH:mm:ss");
var dateAddedRange = sheet.getRange(row+1,8,1,1);
dateAddedRange.setValue(dateAdded);
}
}
function onEdit(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var calId = CalendarApp.getDefaultCalendar().getId();
var sh = ss.getSheetByName(sheetName);
var actSht = event.source.getActiveSheet();
var actRng = event.source.getActiveRange();
var index = actRng.getRowIndex();
Logger.log(index);
var dateCol = actSht.getLastColumn();
var calId
var lastCell = actSht.getRange(index,dateCol);
var date = Utilities.formatDate(new Date(), "GMT-6", "MM/dd/yyyy HH:mm:ss");
lastCell.setValue(date);
var modifiedRow = sh.getRange(index,1,1,ss.getLastColumn()).getValues();
Logger.log(modifiedRow[0][7])
if (modifiedRow[0][7] < modifiedRow[0][8]){
var firstAdded = modifiedRow[0][7];
var dateModified = modifiedRow[0][8];
calendarCreateEvent(index,firstAdded,dateModified,calId);
}
}
function calendarCreateEvent(index,firstAdded,dateModified,calId){
var sheet = SpreadsheetApp.getActiveSheet();
var added = firstAdded;
var modified = dateModified;
var startRow = index; // First row of data to process
var calId = calId;
Logger.log(calId);
Logger.log(startRow);
Logger.log(added);
Logger.log(modified);
if (modified - added > "0"){
var numRows = 1; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 9);
var data = dataRange.getValues();
var cal = CalendarApp.getCalendarById(calId);
for (i in data) {
var row = data[i];
var eventId = row[0]
var title = row[1]; // First column
var desc = row[2]; // Second column
var tstart = row[3];
var tstop = row[4];
var loc = row[5];
var guests = row[6];
//cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
var newEvent = cal.createEvent(title, tstart, tstop, {description:desc,location:loc, guests:guests});//.addGuest(guests);
var newEventId = newEvent.getId();
Logger.log(newEventId);
}
}
}
I got an email from Gooogle today saying that they've added a feature to app script that makes this possible.
Updates:
Status: Fixed
Owner: ekoleda+devrel#google.com
Labels: -Priority-Medium
Comment #11 on issue 574 by ekoleda+devrel#google.com: Ability for Calendar to send email invitation to users added via addGuest
http://code.google.com/p/google-apps-script-issues/issues/detail?id=574
This is now possible using the Advanced Calendar service:
https://developers.google.com/apps-script/advanced/calendar
You can use Calendar.Events.patch() to add attendees, and if you set the optional parameter sendNotifications to "true" the attendees will get an invite.
https://developers.google.com/google-apps/calendar/v3/reference/events/patch
I'm going to try to solve this problem today and then edit this response to reflect that.
Related
I am trying to add Guests in "Options" for automatically add a schedule from Google Sheets into Calendar. I have watched videos (which don't discuss this and lead to no answers when others ask this question) and don't know enough to find the CalendarApp info helpful.
Can someone help? (FYI, I also want to stop duplicating events every time this is run) This is my Script:
function addEvents(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var cal = CalendarApp.getCalendarById("c_kdaqhj8lkd7u68s8thinbnjpik#group.calendar.google.com");
var events = cal.getEvents(new Date ("02/8/2019 12:00 AM"), new Date("02/28/2019 11:59 PM"));
for (var i=0;i<events.length;i++){
var ev = events[i];
ev.deleteEvent();
}
var data = ss.getRange("A2:F"+ lr).getValues();
for(var i = 0;i<data.length;i++){
cal.createEvent(data[i][0], data[i][1], data[i][2], guests:"data[i][3]", "data[i][4]", {description:data[i][5]});
}
}
try this:
cal.createEvent(data[i][0], data[i][1], data[i][2], {guests:`${data[i][3]},${data[i][4]}`, description:data[i][5]});
Reference
Your main problem was that you weren't including guests in the same JavaScript object as description and that guests must be an string including comma separated values. The following script has self explanatory comments and also checks whether an event already exists on the date and time your are trying to insert your event to avoid duplicate events:
function addEvents(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var cal = CalendarApp.getCalendarById("YOURCALENDARID");
var data = ss.getRange("A2:F"+ lr).getValues();
for(var i = 0;i<data.length;i++){
// According to the documentation if no event was found on those date times it will return null
// However if you delete an event this will return an empty array so we also have to check against that
if(cal.getEvents(data[i][1], data[i][2])==null || cal.getEvents(data[i][1], data[i][2]).length==0 ){
// Options must be a Javascript object and specifically the paramter guests is a string so your data must be integrated
// in such a string
cal.createEvent(data[i][0], data[i][1], data[i][2],{guests: ''+data[i][3]+','+data[i][4]+'', description:data[i][5]});
}
}
}
Reference
createEvent(title,title, startTime, endTime, options), please not the information regarding options and more specifically guests within options.
getEvent(startTime, endTime)
I am currently using app script to sync my google sheet to google calendar. The process is quite simple, my script only takes a date and title from the spreadsheet and creates an all day event on that date with that title.
The problem I am facing is that if I accidentally key in the wrong date or I have to update one of the dates inside the spreadsheet, on running the scheduleShifts function again, all the events are created again which results in many duplicate events that I did not intend to be there. I'm trying to find a solution that helps either updates the title of the event or deletes the event and create a new one in the case where the date that is in the spreadsheet is wrong.
It also isn't very efficient to update the data in the spreadsheet and then update the calendar because in the event where quite a few dates or titles have to be changed, it would take quite a bit of time to change them all in the calendar. It would also be very troublesome to delete the current calendar, create a new one, copy that id into the spreadsheet and then update everything again.
This is what my current code looks like:
function scheduleShifts()
{
/*Identify calendar*/
var spreadsheet = SpreadsheetApp.getActiveSheet();
var calendarId = spreadsheet.getRange("C1").getValue();
var eventCal = CalendarApp.getCalendarById(calendarId);
/*Import data from the spreadsheet*/
var signups = spreadsheet.getRange("C4:F73").getValues();
/*Create events*/
for (x=0; x<signups.length; x++)
{
var shift = signups[x];
var title = shift[0];
var date = shift[3];
eventCal.createAllDayEvent(title, date);
}
}
/*Make the script shareable for others to use*/
function onOpen()
{
var ui = SpreadsheetApp.getUi();
ui.createMenu('Sync to Calendar')
.addItem('Schedule shifts', 'scheduleShifts')
.addToUi();
}
I have tried to avoid duplicating events by retrieving all the events with Advanced Calendar Service, pushing their titles into an array and then verifying with IndexOf. However, I am unsure if this method will work if the title stays the same while there is an update in the date of that event.
The code that I referenced from to do this:
var existingEvents=Calendar.Events.list(calendarId);
var eventArray=[];
existingEvents.items.forEach(function(e){eventArray.push(e.summary)});
for (x=0; x<signups.length; x++) {
var shift = signups [x];
var startTime = shift[0];
var endTime = shift[1];
var inspector = shift[2];
if(eventArray.indexOf(inspector)==-1){
eventCal.createEvent(inspector, startTime, endTime);
}else{
Logger.log('event exists already');
}
}
If anyone needs more info feel free to ask in the comments, your help would be greatly appreciated.
You have developed a script to create events using data from a sheet.
You want to be able to update previously created events while avoiding creating duplicates.
Step 1. Avoid creating duplicates:
In order to avoid creating duplicates, you could make the script write the corresponding eventId when each event has been created. This way, next time the script runs, it can check whether the eventId is populated (in which case the event already exists), and only create the event if it doesn't exist. It could be something like this (in this sample, the eventIds are written to column G):
function scheduleShifts() {
const spreadsheet = SpreadsheetApp.getActiveSheet();
const calendarId = spreadsheet.getRange("C1").getValue();
const eventCal = CalendarApp.getCalendarById(calendarId);
const signups = spreadsheet.getRange("C4:G7").getValues();
for (let x = 0; x < signups.length; x++) {
const shift = signups[x];
const title = shift[0];
const date = shift[3];
const eventId = shift[4];
if (eventId == "") { // Check the event doesn't exist
const newEvent = eventCal.createAllDayEvent(title, date);
const newEventId = newEvent.getId();
spreadsheet.getRange(4 + x, 7).setValue(newEventId); // Write new eventId to col G
}
}
}
Step 2. Update events:
Regarding the update process, I'd suggest you to install an onEdit trigger, either manually or programmatically. This this action requires authorization, a simple onEdit trigger would not work here (see Restrictions).
This way, you can use the event object to only update the event corresponding to the row that was edited, thus avoiding having to update all events every time, which would make the process very inefficient. The update process itself would consist on calling setTitle and setAllDayDate.
The function fired by the onEdit trigger could be something like this:
function updateEvent(e) {
var editedRow = e.range.getRow();
var editedData = e.source.getActiveSheet().getRange(editedRow, 3, 1, 5).getValues()[0];
var eventId = editedData[4];
try {
var event = CalendarApp.getEventById(eventId);
event.setTitle(editedData[0]);
event.setAllDayDate(editedData[3]);
} catch(err) {
console.log("There was a problem updating the event. This event might not exist yet.");
}
}
Notes:
You may not want to manually set the range (C4:G73) if the number of events might vary. You can use methods like getLastRow() no make this range dynamic, based on the spreadsheet content.
The eventId that are used here, corresponding to Class CalendarEvent, are not identical to the API Event resource. Take this into account in case you use the Advanced Service.
I successfully integrated GCalendar to Gsheet for creating events. After a Google Form submission, the App Script sends an invitation to my calendar and my guest calendar.
I'm having problems with the automation and the duplicated entries. The script doesn't run when a new row appears - tried both on edit and on form submission -, and when I force it to run, it reschedules ALL the past events.
Here my code
function CreateEvent() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('functionSheet');
var eventCal = CalendarApp.getCalendarById('name#email.com');
var lr = spreadsheet.getLastRow();
var count = spreadsheet.getRange("A2:N"+lr+"").getValues();
for (x=0; x<count.length; x++) {
var shift = count[x];
var summary = shift[2];
var startTime = shift[7];
var endTime = shift[8];
var guests = shift[1];
var description = shift[3];
var location = shift[5];
var event = {
'location': location,
'description': description,
'guests':guests +',',
'sendInvites': 'True',
}
eventCal.createEvent(summary, startTime, endTime, event)
}
}
I'm looking for a solution to improve my script so that it runs every time a new submission has been done AND do not send invitation based on old entries (previous rows).
You are creating calendar events based on Form responses from a Google Form. When your code runs, it is creating events for all the responses and not limited to the most recent response.
The actions to resolve this are two-fold:
trigger the function by using the installable trigger `onFormSubmit.
use Event Objects to capture the form response values, and update the calendar based on those values.
Note: your function is now called CreateEvent(e). The e attribute will automatically give you access to the Event Objects.
The following code is untested, but it indicates the approach to be taken.
`
function CreateEvent(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Logger.log(JSON.stringify(e)); // DEBUG
// identify Calendar
var eventCal = CalendarApp.getCalendarById('<insert id >');
// get the response data
var summary = e.values[2];
var startTime = e.values[7];
var endTime = e.values[8];
var guests = e.values[1];
var description = e.values[3];
var location = e.values[5];
var event = {
'location': location,
'description': description,
'guests':guests +',',
'sendInvites': 'True',
}
// create the event.
eventCal.createEvent(summary, startTime, endTime, event)
}
onFormSubmit
It is important that you trigger your function with the installable trigger onFormSubmit. Refer to the documentation for Managing triggers manually for a set-by-step "how-to" explanation.
Your screen should look something like this when you have completed the setup.
Note that the function name (CreateEvent) does NOT indicate that you are using Event Objects - this is OK - the trigger is just picking up the basic name. BUT it is extremely important that your function is actually called CreateEvent(e) so that it can access the Event Objects.
I'm looking for a way (google app script, add-on, or otherwise) that will automate an appointment reminder e-mail using cell values in my google spreadsheet. Currently, my work flow is like this:
I fill out my client ID and appointment date google form.
Mail merge sends a confirmation e-mail to client that informs them of their appointment date and time.
I'd like for the 3rd step to be that an e-mail is automatically sent 1-2 days prior to the appointment date and maybe a 4th step to send the e-mail on the appointment date to remind them.
Here is my sheet with dummy data and without mail merge formatting:
Although I've looked on these forums and stack exchange, I cannot find a solution to my problem. I've found that people are trying to achieve similar, but mostly want e-mails routed to a static e-mail/e-mails. My needs are more dynamic. Please help.
Here's what what I'm working with:
function sendEmail() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = sheet.getLastRow()-1; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
// Fetch values for each row in the Range.
var data = dataRange.getValues();
//Logger.log(data)
for (i in data) {
var row = data[i];
var date = new Date();
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
//Logger.log(date);
var sheetDate = new Date(row[9]);
//Logger.log(sheetDate);
var Sdate = Utilities.formatDate(date,'GMT-0600','MM:dd:yyyy')
var SsheetDate = Utilities.formatDate(sheetDate,'GMT-0600', 'MM:dd:yyyy')
Logger.log(Sdate+' =? '+SsheetDate)
if (Sdate == SsheetDate){
var emailAddress = row[8]; // Patient Email
var message = row[6];row[7]; // Date and Time
var subject = "Your appointment is in 1 day!." +message;
MailApp.sendEmail(emailAddress, subject, message);
//Logger.log('SENT :'+emailAddress+' '+subject+' '+message)
}
}
}
What I've referred to and tried to make work for me:
Referencing by date/day- Google Forums:
Emails sent based on date- Google Forums:
Emails sent based on date - Stack Overflow:
Due Date Reminders - Stack Overflow
Create a calendar event with a default set of notifications
Here's a function that creates an event on your calendar with default notifications of 1 day, 2 days and 1 hour. You supply with an array that contains
Client Name, Appointment Date and Appointment Time as follows:
['Client Name','11/31/2017','1:30:00 PM']
function testCreateEventWithReminders()
{
createEventWithReminders(['Harry Dog','11/20/2017','1:30:00 PM'])
}
//eA=[clientName,date,time]
function createEventWithReminders(eA)
{
var minute=60*1000;
var hour=60*minute;
var day=24*hour;
var cal=CalendarApp.getCalendarById('CalendarID');//You need to supply your calendarID
var now=new Date().valueOf()
var start=new Date(eA[1] + ' ' + eA[2]);
var endValue=start.valueOf() + (hour);
var end=new Date(endValue);
var event=cal.createEvent(eA[0], start, end);
event.addEmailReminder(24*60);//reminders in minutes
event.addEmailReminder(2*24*60);
event.addEmailReminder(60);
}
Above you will need to insert your calendar ID. If you run displayCalendarInfo you should be able to determine what the calendar names and id's are.
function displayCalendarInfo()//Use this function to determine calendar names and ids.
{
var cals=CalendarApp.getAllCalendars();
var s='Calendar Information';
for(var i=0;i<cals.length;i++)
{
s+=Utilities.formatString('<br />Name: %s Id: %s', cals[i].getName(),cals[i].getId());
}
s+='<br /><input type="button" value="Close" onClick="google.script.host.close();" />';
var ui=HtmlService.createHtmlOutput(s).setWidth(1200).setHeight(450);
SpreadsheetApp.getUi().showModelessDialog(ui, 'Calendar Data');
}
If you want to create your own non default reminders you can add more elements to the input array
Additional Instructions:
The input to the function createEventWithReminders(eA) is an array in the form ['client name','mm/dd/yyyy','hh:mm:ss AM'] where the date and time are for the appointment.
I'm assuming that you can integrate this function into your spreadsheet some how. Personally, I would probably would have made it into a contained webapp because I like to use Siri to do a lot of my data entry thus eliminating the need to type on my mobile device and providing me the opportunity to enter reminders as I proceed through the day without my laptop.
I'm trying to create a sheet that creates events into multiple google calendars from a single google sheet. I am using a sheet modified from the fantastic solution on this post Create Google Calendar Events from Spreadsheet but prevent duplicates from Mogsdad. However I have been triplicating my work to go into 3 different calendars and would like to have my first go at programming. My idea is I would like to go one step further and add a drop down column (labeled status) containing either (Unconfirmed, Save the date, Confirmed) which would then create an even in one or all three calendars named the same as the conditional drop down.
My sheet is arranged as :-
Date | Title | Start Time | End Time | Location | Description | Even ID | Status | Confirmed details | Confirmed Start time | confirmed end time |
As you can see my idea is to have slightly different info in the confirmed calendar than the other two.
The existing code i'm using is
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the exportEvents() function.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Export Events",
functionName : "exportEvents"
}];
sheet.addMenu("Calendar Actions", entries);
};
/**
* Export events from spreadsheet to calendar
*/
function exportEvents() {
var sheet = SpreadsheetApp.getActiveSheet();
var headerRows = 1; // Number of rows of header info (to skip)
var range = sheet.getDataRange();
var data = range.getValues();
var calId = "30kpfnt5jlnooo688qte6ladnk#group.calendar.google.com";
var cal = CalendarApp.getCalendarById(calId);
for (i=0; i<data.length; i++) {
if (i < headerRows) continue; // Skip header row(s)
var row = data[i];
var date = new Date(row[0]); // First column
var title = row[1]; // Second column
var tstart = new Date(row[2]);
tstart.setDate(date.getDate());
tstart.setMonth(date.getMonth());
tstart.setYear(date.getYear());
var tstop = new Date(row[3]);
tstop.setDate(date.getDate());
tstop.setMonth(date.getMonth());
tstop.setYear(date.getYear());
var loc = row[4];
var desc = row[5];
var id = row[6]; // Sixth column == eventId
// Check if event already exists, delete it if it does
try {
var event = cal.getEventSeriesById(id);
event.deleteEventSeries();
row[6] = ''; // Remove event ID
}
catch (e) {
// do nothing - we just want to avoid the exception when event doesn't exist
}
//cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
var newEvent = cal.createEvent(title, tstart, tstop, {description:desc,location:loc}).getId();
row[6] = newEvent; // Update the data array with event ID
debugger;
}
// Record all event IDs to spreadsheet
range.setValues(data);
}
So I realize I need to define the new info to go into the "confirmed" calendar as well as the 2 additional calendars. My issue is I don't know how to fit in a series of if loops to direct events to the 3 calendars. I would also like the calendars to be additive e.g. all events appear in "unconfirmed calendar" events get added to save the date when uprated to that status and then finally appear in "confirmed" when set to that. So a confirmed event appears in all 3 calendars but an unconfirmed only appears there.
I'm virtually brand new to programming so please be nice and excuse my blatant plagarism of others work (thanks Mogsdad) and I appreciate any help!
Welcome to programming! Once you get the hang of it, you'll want to script every Google product you use. :)
If I understand your question correctly, you would like to be able to choose which calendar an event goes into when you run the function exportEvents(). There are several ways to do this, and you don't need any additional loops! You can make use of objects and refer to them by name.
What I would do first, where you currently define cal and calId, is create an object that defines the three calendars like this:
var cal1 = "30kpfnt5jlnooo688qte6ladnk#group.calendar.google.com";
var cal2 = "string url for second calendar";
var cal3 = "string url for third calendar";
var calendars = {
Unconfirmed: CalendarApp.getCalendarById(cal1),
SaveTheDate: CalendarApp.getCalendarById(cal2),
Confirmed: CalendarApp.getCalendarById(cal3)
}
The object calendars now contains the calendar objects for the three calendars such that the key is the status and the value is the object. Then, when you're grabbing the data for each row, add
var cal = row[7];
Now, cal contains the string indicating the status. You can make great use of chaining by making one change to your newEvent definition:
var newEvent = calendars[cal].createEvent(title, tstart, tstop, {description:desc,location:loc}).getId();
What's happening here is calendars[cal] gets the calendar object corresponding to the string in the table, to which you can then add the new event. This does require making a change to your sheet - change the label in your status column from 'Save the Date' to 'SaveTheDate' so it matches the variable name. That should do it!
EDIT
To add the event to multiple calendars, I would use if statements, but you don't need a loop. Something like the following would work:
calendars['Unconfirmed'].createEvent(title... // Add to unconfirmed no matter what
if (cal != 'Unconfirmed'){
calendars['SaveTheDate'].createEvent(title... // Add to SaveTheDate only if not Unconfirmed
}
if (cal == 'Confirmed'){
calendars['Confirmed'].createEvent(title... // Only add to Confirmed if Confirmed
}