I've been reading a few questions and help on managing a google sheet then transferring info to google calendar via email.
I'm using the below code which i pulled from various places and its working really well however a couple of things i'm struggling with:
every time i run the script it duplicates the events, how do i stop this from happening every time the script is run - but still allow for say, addition of new email addresses down the line or update the calendar entry?
cant quite get the starttime and endtime to work nicely - the script is run off a tab in a google sheet that pulls its data from another tab (data entry)to make life easier for my users; is there a trick to the format for yyyy-mm-dd-hh:mm:ss?
possible to setup a line of code as part of this to send out automatic reminders say 7 days out from the event?
Appreciate any and all help on this!
Code thats working right now:
function CreateEvent() {
var spreadsheet = SpreadsheetApp.getActiveSheet();
var calendarId = spreadsheet.getRange('***<LINKTOCELLTHATHASCALENDARID>***').getValue();
var eventCal = CalendarApp.getCalendarById('***CALENDARID<DELETED>***');
var lr = spreadsheet.getLastRow();
Logger.log(lr);
var count = spreadsheet.getRange("A2:F"+lr+"").getValues();
for (x=0; x<count.length; x++) {
var shift = count[x];
var title = shift[0];
var startTime = shift[1];
var endTime = shift[2];
var guests = shift[3];
var description = shift[4];
var location = shift[5];
var event = {
'location': location,
'description': description,
'guests':guests +',',
'sendInvites': 'True',
}
eventCal.createEvent(title, startTime, endTime, event)
}
}
function CreateEvent() {
var sh=SpreadsheetApp.getActiveSheet();
var calendarId=sh.getRange('***<LINKTOCELLTHATHASCALENDARID>***').getValue();
var eventCal=CalendarApp.getCalendarById('***CALENDARID<DELETED>***');
var drg=sh.getRange(2,1,sh.getLastRow()-1,7);
var dA=drg.getValues();
var crg=sh.getRange(2,7,sh.getLastRow()-1,1);//save that event was created in column G
var cA=crg.getValues();
for (i=0;i<dA.length;i++) {
var shift=dA[i];
var title=shift[0];
var startTime=shift[1];
var endTime=shift[2];
var guests=shift[3];
var description=shift[4];
var location=shift[5];
if(!shift[6]) {
var event={'location': location,'description': description,'guests':guests +',','sendInvites': 'True'}
eventCal.createEvent(title, startTime, endTime, event)
cA[i][0]="CREATED";//keeps this event from being created again
}
}
crg.setValues(cA);
}
Related
I'm by no means a programmer but have picked up some of the basics from tutorials to create an App Script that allows Google Sheets data to automatically update with a Google Calendar.
The sync works great, but I'm having an issue finding the right code to prevent duplicate entries.
I'd want the calendar to update an event if the name or time changes, and obviously add any new events, but not create a duplicate of the unchanged events everytime.
Would anyone be able to help with what's needed to make this happen? Thanks!
function scheduleShifts() {
var spreadsheet = SpreadsheetApp.getActiveSheet();
var calendarId = spreadsheet.getRange("A1").getValue();
var eventCal = CalendarApp.getCalendarById(calendarId);
var signups = spreadsheet.getRange('M3:0300').getValues();
for (x=0; x<signups.length; x++) {
var shift = signups[x];
var startTime = shift[0];
var endTime = shift[1];
var volunteer = shift[2];
eventCal.createEvent(volunteer, startTime, endTime);
}
}
I have the below code to take a list of events in a Google Sheet and schedule them into a Google Calendar.
function addEvents(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var calId = ss.getRange("C4").getValue();
var cal = CalendarApp.getCalendarById(calId);
var lr = ss.getLastRow();
var data = ss.getRange("B9:F" + lr).getValues();
for(var i = 0;i<data.length;i++){
cal.createEvent(data[i][0], data[i][1], data[i][2], {location: data[i][3], description: data[i][4]});
}
}
I've used this script before with no issues, but now I'm getting the following error message and can't for the life of me figure out what is going on:
TypeError: Cannot read property 'createEvent' of null
I ultimately would like to add an if statement to this code that grabs the newly created Event ID and puts it in the last column of the data on the sheet, and I would like to be asking for help with that, but at this point I just need step one to work.
Any thoughts?
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 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 fairly new to AppScript, so I apologise if this is crazy easy and I'm just hopeless. I've searched a bunch, but solutions were all in slightly different formats that I wasn't sure how to utilise.
The script works to import my sheets information into the calendar, but I want my first column to be a formula column and I can't figure out how to ignore it while using LastRow.
I got the code from a tutorial online, but if you all have suggestions on what to do better/differently, let me know. Thank you!
I've tried a few different iterations in this line:
var count = spreadsheet.getRange("B2:H"+lr+"").getValues();
But I it's been trial and error, with only errors being the result.
function CreateEvent() {
var spreadsheet = SpreadsheetApp.getActiveSheet();
var calendarId = spreadsheet.getRange('K1').getValue();
var eventCal = CalendarApp.getCalendarById(calendarId);
var lr = spreadsheet.getLastRow();
Logger.log(lr);
var count = spreadsheet.getRange("B2:H"+lr+"").getValues();
for (x=0; x<count.length; x++) {
var shift = count[x];
var summary = shift[0];
var guests = shift[2];
var description = shift[3];
var location = shift[4];
var startTime = shift[5];
var endTime = shift[6];
var event = {
'location': location,
'description': description,
'guests':guests +',',
}
eventCal.createEvent(summary, startTime, endTime, event)
}
}
Your approach is right, there is only one step that you need to take in order to create the event. The startTime and endTime parameters have to be Date type, no String. To achieve that you need to add the following changes:
var startTime = new Date(shift[5]);
var endTime = new Date(shift[6]);
With this code the strings will be converted into dates and the createEvent method will work. Please, ask for more clarification if I haven't explained myself adequately.
As a sidenote I want to commet one little thing, in the following line:
var count = spreadsheet.getRange("B2:H"+lr+"").getValues();
The last "" are not needed at all and may cause confusion, but it does certainly work. For clarity, I recommend just using:
var count = spreadsheet.getRange("B2:H"+lr).getValues();