I'm attempting to set up yearly recurring events from a Google spreadsheet, and although I've followed this answer, I still can't get the ID to write to each row and thus allow me to write and check entries.
The column layout is
RenDate | Domain | Client | Type | Registrar | ID
Code:
/**
* 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 = "[id removed]#group.calendar.google.com";
var cal = CalendarApp.getCalendarById(calId);
for (i in data) {
if (i < headerRows) continue; // Skip header row(s)
var row = data[i];
var date = new Date(row[0]); // First column - Renewal Date
var domain = row[1]; // Second column - Domain
var client = row[2]; // Third column - Client
var type = row[3]; // Fourth column - Type
var source = row[4]; // Fifth column - Registrar
var id = row[5]; // Sixth column - ID
if(row[1]=="") continue; // Skip rows if no date exists
var title = type + " renewal for " + domain;
var desc = "Renew with " + source + " for " + client;
// Check if event already exists, update it if it does
try {
var event = cal.getEventSeriesById(id);
event.setTitle('got you'); // this is to "force error" if the event does not exist
}catch (e) {
var newEvent = cal.createEvent(title, date, {description:desc});
row[5] = newEvent.getId(); // Update the data array with event ID
Logger.log('event created'); // For debugging
var event = cal.getEventSeriesById(row[5]);
}
event.setTitle(title);
event.setDescription(desc);
var recurrence = CalendarApp.newRecurrence().addYearlyRule();
event.setRecurrence(recurrence, date);
debugger;
data[i] = row ;
}
// Record all event IDs to spreadsheet
range.setValues(data);
}
Debugging seems to be fine, but no events are created. I was able to create events using the CreateAllDayEventSeries function, but that seems to be impossible to work out with the ID approach, so that's why I'm trying this method.
Edit: Made change to continue function as caught by Serge insas.
Now the script is not finding the method for createEvent on the catch command.
I found that you were making a few errors (minimal) that prevented it to work.
One detail : you tried to create events with a single date, this works only for allDay Events so I guess that's what you wanted to do.
below is the full working code :
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 range = sheet.getDataRange();
var data = range.getValues();
var headers = data.shift();
Logger.log(data);
var calId = "h22nevo15tm0nojb6ul4hu7ft8#group.calendar.google.com";
var cal = CalendarApp.getCalendarById(calId);
for (i in data) {
var row = data[i];
Logger.log(row);
var date = new Date(row[0]); // First column - Renewal Date
var domain = row[1]; // Second column - Domain
var client = row[2]; // Third column - Client
var type = row[3]; // Fourth column - Type
var source = row[4]; // Fifth column - Registrar
var id = row[5]; // Sixth column - ID
if(row[0]=="") continue; // Skip rows if no date exists
var title = type + " renewal for " + domain;
var desc = "Renew with " + source + " for " + client;
// Check if event already exists, update it if it does
try {
var event = cal.getEventSeriesById(id);
event.setTitle('got you'); // this is to "force error" if the event does not exist
}catch (e) {
var newEvent = cal.createAllDayEvent(title, date, {description:desc});
row[5] = newEvent.getId(); // Update the data array with event ID
Logger.log('event created'); // For debugging
var event = cal.getEventSeriesById(row[5]);
}
event.setTitle(title);
event.setDescription(desc);
var recurrence = CalendarApp.newRecurrence().addYearlyRule();
event.setRecurrence(recurrence, date);
debugger;
data[i] = row ;
}
// Record all event IDs to spreadsheet
sheet.getRange(2,1,data.length,data[0].length).setValues(data);
}
I was able to resolve the issue on my own (although thanks again to Serge for some excellent assistance initially and a solid answer, even though I didn't see it before I fixed the issue). Here's the code I eventually settled with:
/**
* 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 = "[id withheld]#group.calendar.google.com";
var cal = CalendarApp.getCalendarById(calId);
for (i in data) {
if (i < headerRows) continue; // Skip header row(s)
var row = data[i];
var date = new Date(row[0]); // First column - Renewal Date
var domain = row[1]; // Second column - Domain
var client = row[2]; // Third column - Client
var type = row[3]; // Fourth column - Type
var source = row[4]; // Fifth column - Registrar
var id = row[5]; // Sixth column - ID
if(row[0]=="") continue; // Check to see if date exists
var title = type + " renewal for " + domain;
var desc = "Renew with " + source + " for " + client;
// Check if event already exists, update it if it does
try {
var event = cal.getEventSeriesById(id);
}catch(e){
//Do nothing
}
if(!event){
var newEvent = cal.createAllDayEventSeries(title, date, CalendarApp.newRecurrence().addYearlyRule(), {description:desc});
row[5] = newEvent.getId(); // Update the data array with event ID
Logger.log('Event ID ' + row[5] + ' created'); // For debugging
}
else{
var event = cal.getEventSeriesById(row[5]);
event.setTitle(title);
event.setDescription(desc);
Logger.log('Event ID ' + row[5] + ' already exists'); //For debugging
}
debugger;
data[i] = row ;
}
// Record all event IDs to spreadsheet
range.setValues(data);
}
Related
Absolute noob here !
Background:
am trying to create a Google Sheet which I can update for a series of events and
create Google Calendar events based on those entries
so far, am successful in creating calendar events and also updating back the last column of the sheet with the EventID (iCalUID) - thanks to other stackoverflow posts
am also successful in not creating Duplicates by checking if the EventID (iCalUID) is already present in the last column - thanks again to other stackoverflow posts
But... have another requirement, where am failing:
need to mark an existing event as 'Cancelled' in one of the columns in the sheet and
if this is 'true' then look-up the EventID (iCalUID) from the corresponding last cell (of that row which has a 'Cancelled' entry) and
delete that particular event from the calendar
also, calendar events should NOT be created again as long as that cell remains/retains the word 'Cancelled'.
the "var check1 = row[23]; //Booked/Blocked/Cancelled" in below script was just added to bring in this logic that I wanted, but am kind of unable to proceed
Relevant screen-shot of the sheet
Code that I used so far as below:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Sync to Calendar')
.addItem('Sync Now', 'sync')
.addToUi();
}
function sync() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var calendar = CalendarApp.getCalendarById('myemailid#gmail.com');
var startRow = 2; // First row from which data should process > 2 exempts my header row
var numRows = sheet.getLastRow(); // Number of rows to process
var numColumns = sheet.getLastColumn();
var dataRange = sheet.getRange(startRow, 1, numRows-1, numColumns);
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var name = row[1]; //Name of Guest
var place = row[4]; //Add2
var room = row[9]; //Room Number
var inDate = new Date(row[10]); //Check-In Date
var outDate = new Date(row[11]); //Check-Out Date
var check1 = row[23]; //Booked/Blocked/Cancelled
var check2 = row[24]; //Event created and EventID (iCalUID) populated
if (check2 == "") {
var currentCell = sheet.getRange(startRow + i, numColumns);
var event = calendar.createEvent(room, inDate, outDate, {
description: 'Booked by: ' + name + ' / ' + place + '\nFrom: ' + inDate + '\nTo: ' + outDate
});
var eventId = event.getId();
currentCell.setValue(eventId);
}
}
}
I believe your goal is as follows.
You want to check the columns "X" and "Y".
When the column "X" is not Cancelled and the column "Y" is empty, you want to create a new event.
When the column "X" is Cancelled and the column "Y" is not empty, you want to delete the existing event.
When the column "X" is Cancelled, you don't want to create a new event.
In this case, how about the following modification?
Modified script:
In this script, in order to check whether the event has already been deleted, Calendar API is used. So please enable Calendar API at Advanced Google services.
function sync() {
var calendarId = 'myemailid#gmail.com'; // Please set your calendar ID.
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var calendar = CalendarApp.getCalendarById(calendarId);
var startRow = 2; // First row from which data should process > 2 exempts my header row
var numRows = sheet.getLastRow(); // Number of rows to process
var numColumns = sheet.getLastColumn();
var dataRange = sheet.getRange(startRow, 1, numRows - 1, numColumns);
var data = dataRange.getValues();
var done = "Done"; // It seems that this is not used.
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var name = row[1]; //Name of Guest
var place = row[4]; //Add2
var room = row[9]; //Room Number
var inDate = new Date(row[10]); //Check-In Date
var outDate = new Date(row[11]); //Check-Out Date
var check1 = row[23]; //Booked/Blocked/Cancelled
var check2 = row[24]; //Event created and EventID (iCalUID) populated
// I modified below script.
if (check1 != "Cancelled" && check2 == "") {
var currentCell = sheet.getRange(startRow + i, numColumns);
var event = calendar.createEvent(room, inDate, outDate, {
description: 'Booked by: ' + name + ' / ' + place + '\nFrom: ' + inDate + '\nTo: ' + outDate
});
var eventId = event.getId();
currentCell.setValue(eventId);
} else if (check1 == "Cancelled" && check2 != "") {
var status = Calendar.Events.get(calendarId, check2.split("#")[0]).status;
if (status != "cancelled") {
calendar.getEventById(check2).deleteEvent();
}
}
}
}
Reference:
Events: get
I'm working on a spreadsheet for my department to create, edit, and delete work assignments to a google calendar. It's been quite a while since I did any actual coding, so I've been doing a lot of trial and error and referring to the Google App Script and Calendar references. I've finally gotten to the point where I can create the events; however, when I am testing my function to delete events, I've discovered that the event on row two, deletes the event on row three, and when I try to delete the event on row three, I get a "cannot call method deleteEvent" error. I've tried looking through various sites for similar situations and have not been successful. Any help walking me through the error in my code issue would be greatly appreciated. I'm also attempting to attach a link for a video of the spreadsheet as the script is running here
// Adds the custom menu to the active spreadsheet.
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [
{
name: "Create New Events",
functionName: "createCalEvent"
}, {
name: "Update Existing Events",
functionName: "updateCalEvent"
}, {
name: "Delete Existing Events",
functionName: "deleteCalEvent"
}
];
spreadsheet.addMenu('Calendar Options', menuEntries);
}
// Event Status --> Assist in Triggering Update/Delete Functions
var delConfirmed = 'Event Created';
var updateConfirmed = 'Event Updated';
var eventCreate = 'Event Deleted';
function createCalEvent(){
var calSheet = SpreadsheetApp.getActiveSheet();
var dataRange = calSheet.getRange('$A2:$J');
var data = dataRange.getValues();
for (var i = 0; i < data.length; i++) {
var row = data[i];
var calDate = row[1]; // COL A
var calTitle = row[5]; // COL F
var calGuests = row[6]; // COL G
var calEType = row[7]; // COL H
var calID = row[8]; // COL I
var calStatus = row[9]; // COL J
// If there is a date and both the Event Type (Adjust/Delete) and Status are blank then Create the Calendar Event
if(calDate !=='' && calEType == '' && calStatus == ''){
var calEvent = CalendarApp
.getCalendarById('envysion.com_kmfeb8mqmlv4j9k34l37q7fv3k#group.calendar.google.com')
.createAllDayEvent(calTitle, new Date(calDate),{guests:calGuests});
// Update the Status Column
SpreadsheetApp.getActiveSheet().getRange(i+2, 10).setValue('Event Created');
var newEvent = calEvent.getId();
// Add the Event ID to the Event ID Column
SpreadsheetApp.getActiveSheet().getRange(i+2,9).setValue(newEvent);
Logger.log('Event ID: ' + newEvent + ' Title: ' + calEvent.getTitle() + 'Guests: ' + calEvent.getGuestList());
}
}
}
function deleteCalEvent()
{
// Get range of dates to delete
var fromDate = new Date(2019,3,1);
var toDate = new Date(2019,4,1);
// Get Calendar Events
var calendar = CalendarApp.getCalendarById('envysion.com_kmfeb8mqmlv4j9k34l37q7fv3k#group.calendar.google.com')
var events = calendar.getEvents(fromDate, toDate);
// Get Spreadsheet Details
var calSheet = SpreadsheetApp.getActiveSheet();
var dataRange = calSheet.getRange('$A2:$J');
var data = dataRange.getValues();
for (var i = 0; i < data.length; i++) {
var row = data[i];
var calDate = row[1];
var calTitle = row[5];
var calGuests = row[6];
var calEType = row[7];
var calID = row[8];
var calStatus = row[9];
// Check if the Type column is set to Delete and if the event hasn't already been deleted
if(calEType == 'Delete' && calStatus !== 'Event Deleted'){
events[i].deleteEvent();
SpreadsheetApp.getActiveSheet().getRange(i+2, 10).setValue('Event Deleted');
SpreadsheetApp.getActiveSheet().getRange(i+2, 9).setValue('');
}
}
}
It seems you need do something like this
if(calEType == 'Delete' && calStatus !== 'Event Deleted'){
var _event_ = calendar.getEventById(calStatus);
if(_event_){
_event_.deleteEvent();
SpreadsheetApp.getActiveSheet().getRange(i+2, 10).setValue('Event Deleted');
SpreadsheetApp.getActiveSheet().getRange(i+2, 9).setValue('');
}
}
This is because you may have a different number of events in the calendar and in the sheet.
Using this great answer, I've managed to alter it to create a script to export events from a Google Spreadsheet to Google Calendar.
Create Google Calendar Events from Spreadsheet but prevent duplicates
I then got some great advice, and worked out that it wasn't populating the eventID column due to the error I was getting - "Exceeded maximum execution time" - due to the large number of rows (up to 1000).
Create Google Calendar events from a Google Spreadsheet - script is creating duplicates
I've been looking through answers to try and work out a way to get around this, but can't seem to work out an answer! Apologies - I'm quite new to all this.
Can anyone point me in the right direction, as to how I can either force the script to process beyond the 5 minutes, or anything else?
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 = 2; // Number of rows of header info (to skip)
var range = sheet.getDataRange();
var data = range.getValues();
var calId = "pma5g2rd5cft4lird345j7pke8#group.calendar.google.com";// use default claendar for tests
var cal = CalendarApp.getCalendarById(calId);
for (i in data) {
if (i < headerRows) continue; // Skip header row(s)
var row = data[i];
var date = new Date(row[12]); // WHC
var title = row[18]; // WHC Title
var tstart = setTimeToDate(date,row[15]);// start time
var tstop = setTimeToDate(date,row[16]);// end time
Logger.log('date = '+date+'tstart = '+tstart+' tstop = '+tstop);
var id = row[17]; //EventID WHC
// Check if event already exists, update it if it does
try {
var event = cal.getEventSeriesById(id);
event.setTitle('got you');// this is to "force error" if the event does not exist, il will never show for real ;-)
}catch(e){
var newEvent = cal.createEvent(title, tstart, tstop); // create a "normal" event
row[17] = newEvent.getId(); // Update the data array with event ID
Logger.log('event created');// while debugging
var event = cal.getEventSeriesById(row[17]);// make it an event Serie
}
event.setTitle(title);
}
// Record all event IDs to spreadsheet
range.setValues(data);
}
function setTimeToDate(date,time){
var t = new Date(time);
var hour = t.getHours();
var min = t.getMinutes();
var sec = t.getSeconds();
var dateMod = new Date(date.setHours(hour,min,sec,0))
return dateMod;
}
The idea is to count the time the script is taking inside the main function and to interrupt it when we reach the limit.
We have to store the row number where we interrupt the script and continue from there on the next run.
Since we don't want to do that manually (how lazy we are :-) we'll set up a trigger to run it every 5 minutes.
Below is a full script.
It will send you an email on each run to tell you the progress... you'll have to remove this line after your test of course (unless you like receiving emails every 5 minutes from yourself !)
You will have to change the calendar ID, the row distribution (I tested it on a sheet with less column than yours) but that will be fairly easy.
function createEventsWithBatch() {
// check if the script runs for the first time or not,
// if so, create the trigger and PropertiesService.getScriptProperties() the script will use
// a start index and a total counter for processed items
// else continue the task
if(PropertiesService.getScriptProperties().getKeys().length==0){
PropertiesService.getScriptProperties().setProperties({'itemsprocessed':0});
ScriptApp.newTrigger('createEventsWithBatch').timeBased().everyMinutes(5).create();
}
// initialize all variables when we start a new task, "notFinished" is the main loop condition
var itemsProcessed = Number(PropertiesService.getScriptProperties().getProperty('itemsprocessed'));
var startTime = new Date().getTime();
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 = "h22nevo15tm0nojb6ul4hu7ft8#group.calendar.google.com";
var cal = CalendarApp.getCalendarById(calId);
for (var i = itemsProcessed ; 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 = setTimeToDate(date,row[2]);
var tstop = setTimeToDate(date,row[3]);
// Logger.log('date = '+date+'tstart = '+tstart+' tstop = '+tstop);
var loc = row[4];
var desc = row[5];
var type = row[6];
var times = row[7]
var id = row[8];
// Check if event already exists, update it if it does
try {
var event = cal.getEventSeriesById(id);
event.setTitle('got you');
}catch(e){
var newEvent = cal.createEvent(title, tstart, tstop, {description:desc,location:loc});
row[8] = newEvent.getId(); // Update the data array with event ID
// Logger.log('event created');
var event = cal.getEventSeriesById(row[8]);
}
event.setTitle(title);
event.setDescription(desc);
event.setLocation(loc);
if(type=='PM'){
var recurrence = CalendarApp.newRecurrence().addMonthlyRule().times(times);
event.setRecurrence(recurrence, tstart, tstop);
}else if(type=='PW'){
var recurrence = CalendarApp.newRecurrence().addWeeklyRule().times(times)
event.setRecurrence(recurrence, tstart, tstop);
}
data[i] = row ;
Logger.log(i+' '+new Date().getTime()-startTime)
if(new Date().getTime()-startTime > 240000){ // if > 4 minutes
var processed = i+1;// save usefull variable
PropertiesService.getScriptProperties().setProperties({'itemsprocessed':processed});
range.setValues(data);
MailApp.sendEmail(Session.getEffectiveUser().getEmail(),'progress sheet to cal','item processed : '+processed);
return;
}
}
range.setValues(data);// this time we are done !
killTrigger();// delete the trigger
PropertiesService.getScriptProperties().deleteAllProperties(); // clean up properties
}
function setTimeToDate(date,time){
var t = new Date(time);
var hour = t.getHours();
var min = t.getMinutes();
var sec = t.getSeconds();
var dateMod = new Date(date.setHours(hour,min,sec,0))
return dateMod;
}
function killTrigger(){
var trigger = ScriptApp.getProjectTriggers()[0];
ScriptApp.deleteTrigger(trigger);
}
There are various GAS libraries that help you get past the 5 minute execution timeout, using the same idea as #Serge suggested. I personally use the Continuous Batch Library.
At the bottom is the code from a previous blog, which works great!
This code is set up with the following Google sheet header:
Date | Title | Start Time | End Time | Location | Description | EventID
However, I need to have the ability to create recurring events.
The new Google sheet header is as follow:
Date | Title | Start Time | End Time | Location | Description | Type | Recurring | EventID
I need to create recurring events if Type = "PM" (new column) on a monthly basis for "Recurring" (also a new column) amount of months.
How is this possible while still not having duplicates every time the script is ran?
/**
* 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 = "YOUR_CALENDAR_ID";
var cal = CalendarApp.getCalendarById(calId);
for (i in data) {
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, update it if it does
try {
var event = cal.getEventSeriesById(id);
}
catch (e) {
// do nothing - we just want to avoid the exception when event doesn't exist
}
if (!event) {
//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
}
else {
event.setTitle(title);
event.setDescription(desc);
event.setLocation(loc);
// event.setTime(tstart, tstop); // cannot setTime on eventSeries.
// ... but we CAN set recurrence!
var recurrence = CalendarApp.newRecurrence().addDailyRule().times(1);
event.setRecurrence(recurrence, tstart, tstop);
}
debugger;
}
// Record all event IDs to spreadsheet
range.setValues(data);
Ok, this was again something interesting... The code above needed a few modification to do what you wanted :
Since newly created events are not series (or else they must be created as eventSeries but this would make the conditions more complicated...) when we create a new event we dont use that object but get it using getEventSeriesById() which implicitly changes its nature without needing to define a recurrence.
This trick works just fine and makes the code simpler.
The other issue was about setting time and dates : your code took the hour/minutes value from a date object without year (that's normal when reading a SS) but it means that the Javascript Date has a date value in January (month 0) and January is in winter (as you know XD) so we had a problem with daylight savings and all time values were 1 hour later because setting month and date afterwards didn't change hour value (this is unclear I'm afraid...but you could check it using your code these days)
I had to invert the process and set time value to the date object instead, this gives the right result.
Since it's a bit more code to write I created a small function to do the job : it helps to keep the main code "cleaner".
Below it the full code, I added also a 'PER WEEK' recurrence to test the idea... keep it or leave it if you don't need it .
// Date | Title | Start Time | End Time | Location | Description | Type | Recurring | EventID
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 = CalendarApp.getDefaultCalendar().getId();// use default claendar for tests
var cal = CalendarApp.getCalendarById(calId);
for (i in data) {
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 = setTimeToDate(date,row[2]);
var tstop = setTimeToDate(date,row[3]);
Logger.log('date = '+date+'tstart = '+tstart+' tstop = '+tstop);
var loc = row[4];
var desc = row[5];
var type = row[6];
var times = row[7]
var id = row[8];
// Check if event already exists, update it if it does
try {
var event = cal.getEventSeriesById(id);
event.setTitle('got you');// this is to "force error" if the event does not exist, il will never show for real ;-)
}catch(e){
var newEvent = cal.createEvent(title, tstart, tstop, {description:desc,location:loc}); // create a "normal" event
row[8] = newEvent.getId(); // Update the data array with event ID
Logger.log('event created');// while debugging
var event = cal.getEventSeriesById(row[8]);// make it an event Serie
}
event.setTitle(title);
event.setDescription(desc);
event.setLocation(loc);
if(type=='PM'){
var recurrence = CalendarApp.newRecurrence().addMonthlyRule().times(times)
event.setRecurrence(recurrence, tstart, tstop);// we need to keep start and stop otherwise it becomes an AllDayEvent if only start is used
}else if(type=='PW'){
var recurrence = CalendarApp.newRecurrence().addWeeklyRule().times(times)
event.setRecurrence(recurrence, tstart, tstop);
}
data[i] = row ;
}
range.setValues(data);
}
function setTimeToDate(date,time){
var t = new Date(time);
var hour = t.getHours();
var min = t.getMinutes();
var sec = t.getSeconds();
var dateMod = new Date(date.setHours(hour,min,sec,0))
return dateMod;
}
test sheet here in view only
If you want to display a recurring event on specific intervals, the .interval function allows you to do that under the recurrence method.
In the above example, changing the appropriate code to
var recurrence = CalendarApp.newRecurrence().addMonthlyRule().interval(times)
does that.
This means if for one event, times = 3, this event will appear on your calendar every 3 months.
I'm trying to write a script that will take data from a Google spreadsheet and create events in my Google calendar.
I managed that fine but it produced duplicates every time I ran it. So now I'm trying to prevent that by creating a column 17 in the spreadsheet with an automatically produced unique event ID for each row and then each time the script is run it will look at the event ID for each row and delete the corresponding event in the calendar before recreating it with the original data or updated data if I've changed the row.
I'm new to scripting of any kind and cobbled this together but am hitting a wall now. Can anyone help sort this out?
function CalInsert() {
var cal = CalendarApp.getDefaultCalendar();
var id = SpreadsheetApp.getActiveSheet().getRange(2,17).getValue();
if (id != 0) {
var event = cal.getEventSeriesById(id);
event.deleteEventSeries();
}
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = sheet.getLastRow(); // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var title = row[0]; // First column
var desc = row[13]; // Second column
var tstart = row[14];
var tstop = row[15];
var event = cal.createEvent(title, tstart, tstop, {description:desc});
var eventid = event.getId();
SpreadsheetApp.getActiveSheet().getRange(2,17).setValue(eventid);
}
}
This is very similar to a question asked just two days ago, which was about synchronizing a spreadsheet of events with a calendar. It sounds like you want to consider the spreadsheet to be the master of events that it originates, which would simplify the problem considerably. The basics of what you need to do are covered in this answer. If you'd rather just modify existing code, I've got an implementation below.
I have a modified version of the code from this blog, that will modify pre-existing calendar entries to match the info in the spreadsheet. I have arranged my spreadsheet differently, and this is reflected in the code.
Date | Title | Start Time | End Time | Location | Description |
EventID
The event ID column gets filled in by the script when new events are created, and is then used in later invocations to retrieve events from the calendar, thereby avoiding duplication.
Script
/**
* 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 = "YOUR_CALENDAR_ID";
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, update it if it does
try {
var event = cal.getEventSeriesById(id);
}
catch (e) {
// do nothing - we just want to avoid the exception when event doesn't exist
}
if (!event) {
//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
}
else {
event.setTitle(title);
event.setDescription(desc);
event.setLocation(loc);
// event.setTime(tstart, tstop); // cannot setTime on eventSeries.
// ... but we CAN set recurrence!
var recurrence = CalendarApp.newRecurrence().addDailyRule().times(1);
event.setRecurrence(recurrence, tstart, tstop);
}
debugger;
}
// Record all event IDs to spreadsheet
range.setValues(data);
}
Delete / Recreate
In this alternative, the eventID is used to find and delete the previously existing event. After that, a new event is created with the data in the spreadsheet. This has the benefit that all values of the event can be updated, including start and stop times (see Notes below). On the other hand, any changes that were made to the original event will be lost - for instance, if other people had been invited to the event, or custom reminders were added.
To use this alternative, simply replace the matching code with this:
// 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;
Notes
The Documentation for getEventSeriesById wrongly states it returns null when no matching event is found, when instead it throws an exception. (nasty!) So I've enclosed it in a try / catch block just to keep on swimming.
Unfortunately, while getEventSeriesById works to retrieve an event, it returns an EventSeries object, which does not support the setTime() method. If you don't expect to change the time of events, this OK. Otherwise, you can change the Event into an EventSeries by setting the recurrence rules & times, or delete the old event and create a new one, as shown in Delete / Recreate. Issue 1154.
The spreadsheet always wins. Any event changes (in relevant fields) recorded via the Google Calendar will be overwritten by the script.
Id like to Post this for anyone who would like to use it, I have modified the script to work within a sheet I was already using. Date Format and event duplication were a couple of issues that needed to be fixed but after some testing im pretty happy with how this is working.I Use it to Book jobs and share them with my employees who are mobile and do construction type work across the city.
Next step is to pull calendar events to the spreadsheet so it can work both ways and I can use the calendar app on my phone to book jobs on the fly so if anyone has any advice im all ears, also i still need a script to insert form response data into the same sheet and add complete rows where the job numbers match keeping the existing Data intact.
`function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Export Events",
functionName : "exportEvents"
}]; sheet.addMenu("Calendar Actions", entries);
};
function parseDate(s) {
var months = {jan:0,feb:1,mar:2,apr:3,may:4,jun:5,
jul:6,aug:7,sep:8,oct:9,nov:10,dec:11};
var p = s.replace(".", "").split('-');
return new Date(p[2], months[p[1].toLowerCase()], p[0]);
}
/**
* Export events from spreadsheet to calendar
*/
function exportEvents() {
var sheet = SpreadsheetApp.getActiveSheet();
var headerRows = 6; // Number of rows of header info (to skip)
var range = sheet.getDataRange();
var data = range.getDisplayValues();
//var calId = "Your calendar Id"; // PRODUCTION
var calId = "Your_calendar Id to test"; // TEST
var cal = CalendarApp.getCalendarById(calId);
//Logger.log(cal);
//Logger.log(data.length);
for (i=0; i<data.length; i++) {
if (i < headerRows) continue; // Skip header row(s)
if (data[i][0].length < 1) continue; // Skip if no content.
var row = data[i];
Logger.log(row);
var date = parseDate(row[0]); // First column
//Logger.log(date);
var title = row[1]; // Second column
var tstart = new Date();
var s = row[2].split(":");
tstart.setHours(s[0]);
tstart.setMinutes(s[1]);
tstart.setSeconds(s[2]);
tstart.setDate(date.getDate());
tstart.setMonth(date.getMonth());
tstart.setYear(date.getYear());
var tstop = new Date();
var e = row[3].split(":");
tstop.setHours(e[0]);
tstop.setMinutes(e[1]);
tstop.setSeconds(e[2]);
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, update it if it does
var event = null;
if (id.length > 0) {
try {
event = cal.getEventSeriesById(id);
}
catch (e) {
// do nothing - we just want to avoid the exception when event doesn't exist
}
}
if (!event) {
//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();
var r = i + 1;
var cell = sheet.getRange("G" + r);
cell.setValue(newEvent);
}
else {
Logger.log(event);
event.setTitle(title);
event.setDescription(desc);
event.setLocation(loc);
// event.setTime(tstart, tstop); // cannot setTime on eventSeries.
// ... but we CAN set recurrence!
var recurrence = CalendarApp.newRecurrence().addDailyRule().times(1);
event.setRecurrence(recurrence, tstart, tstop);
}
debugger;
}
}
`