How to integrate Google Spreadsheet with Google Calendar? - google-apps-script

I've a Google spreadsheet in Google Apps. When I enter a date/time in spreadsheet, I want to create a corresponding entry in a particular Google Calendar? How can I do this?
This thread suggests some sort of scripting but complete solution is not given. And I'm using Google Apps instead of Google Docs as suggested by this thread.
Update : Found this thread helpful.

function caltest1() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 5);
var data = dataRange.getValues();
var cal = CalendarApp.getDefaultCalendar();
for (i in data) {
var row = data[i];
var title = row[0]; // First column
var desc = row[1]; // Second column
var tstart = row[2];
var tstop = row[3];
var loc = row[4];
//cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
cal.createEvent(title, tstart, tstop, {description:desc,location:loc});
}
}
Directly taken from here.

Related

How do I resolve this ReferenceError: "tStart" is not defined. (line 55, file "Code")?

I am trying to create calendar events from data in google sheets and each time I run the code I get ReferenceError: "tStart" is not defined. (line 55, file "Code"). Please see the code below. I'd be grateful for any assistance.
function listUpcomingEvents() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
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 cal = CalendarApp.getCalendarsByName(
"admin#someaddress.co.uk" )[0];
for (i in data) {
var row = data[i];
var title = row[8]; // First column
var tstart = row[9]; // Second column
var tstop = row[10];
Logger.log(tstop);
var desc = row[1];
var loc = row[4];
//var cmmail = row[6];
//var leamail = row[7];
cal.createEvent(title, new Date(tStart), new Date(tStop),{description:desc,location:loc});
}
}
I was expecting this to pick up the active spreadsheet and identify the dates in column J and add these to as new events to calendar.
JS is case sensitive, so "tstart" and "tStart" are two different variables.
You should either try changing tstart to tStart in your initial declaration, or vice-versa.

Updated Google Calendar events from Sheets

I am trying to sync a Google Calendar to a Sheet to allow me to easily mass update calendar events from within the sheet, then push all the changes back to the calendar.
I'm able to download all the events into my Sheet, specifically using this bit of code:
// Loop through all calendar events found and write them out starting on calulated ROW 2 (i+2)
for (var i=0;i<events.length;i++) {
var row=i+2;
var myformula_placeholder = '';
// Matching the "header=" entry above, this is the detailed row entry "details=", and must match the number of entries of the GetRange entry below
// NOTE: I've had problems with the getVisibility for some older events not having a value, so I've had do add in some NULL text to make sure it does not error
var details=[[events[i].getTitle(), events[i].getStartTime(), events[i].getEndTime(), events[i].getId()]];
var range=sheet.getRange(row,1,1,4);
range.setValues(details);
I'm able to create NEW events onto my calendar from the downloaded data using this code:
function caltest1() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 5);
var data = dataRange.getValues();
var cal = CalendarApp.getDefaultCalendar();
for (i in data) {
var row = data[i];
var title = row[0]; // First column
var desc = row[1]; // Second column
var tstart = row[2];
var tstop = row[3];
var loc = row[4];
//cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
cal.createEvent(title, tstart, tstop, {description:desc,location:loc});
}
}
However because it's creating a new event and not simply updating the original event, it creates duplicate events every time I push the script.
I want to find code that instead uses the event ID gathered in the first function and simply updates the existing event with the new information.
I'd prefer not to use code that deletes all calendar events then creates all the new ones.
The getEventSeriesById() method can be used. Even though the name indicates that this method is for getting a calendar event that is a series, it works for a single event.
The documentation states:
Gets the event series with the given ID. If the ID given is for a single CalendarEvent, then a CalendarEventSeries will be returned with a single event in the series.
Apps Script documentation - getEventSeriesById
function caltest1() {
var cal,desc,i,iCalId,loc,row,sheet,startRow,thisEvent,title,tstart,
tstop,;
sheet = SpreadsheetApp.getActiveSheet();
startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 5);
var data = dataRange.getValues();
cal = CalendarApp.getDefaultCalendar();
for (i in data) {
row = data[i];
title = row[0]; // First column
desc = row[1]; // Second column
tstart = row[2];
tstop = row[3];
loc = row[4];
iCalId = row[5];//Column 6 or F
//cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
//cal.createEvent(title, tstart, tstop, {description:desc,location:loc});
thisEvent = cal.getEventSeriesById(iCalId);//Get a calendar event by it's ID
if (thisEvent) {
thisEvent.setDescription('Test it to determine if it works');//Edit the description
}
}
}
Note that the above code assumes that the calendar event ID is in column F, and that the function that downloaded the calendar data put the ID in column F.

How can I import the last row ONLY of a spreadsheet to my calendar using Google Apps Script?

I have been able to create a script to import a spreadsheet (delivery schedule) to a calendar. It works great, but I am constantly adding new deliveries to the schedule. When I import it to the calendar, it imports everything every time. Is there a way to have ONLY the last row imported and not the whole spreadsheet?
This is the script that I am using...
var DELIVERY_IMPORTED = "DELIVERY_IMPORTED";
var ss = SpreadsheetApp.getActiveSpreadsheet();
function onOpen() {
var menuEntries = [{name: "Import Events in Calendar", functionName: "importCalendar"}];
ss.addMenu("Calendar", menuEntries);
}
function importCalendar() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 5; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 5)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var title = row[1]; // Name
var location = row[2];
var startDate = row[3]; // Start Date
var description = row[4];
var cal = CalendarApp.getCalendarsByName('Calendar Name')[0];
var advancedArgs = {description: description, location: location};
cal.createAllDayEvent(title, new Date(startDate), advancedArgs);
SpreadsheetApp.flush();
}
}
Any help would be greatly appreciated!
If you only have one new lime each time, you can use Sheet.getLastRow() to get the index of the last row with content.
Else you might use Properties service to store the last imported row, and start from there.

Update calendar from google spreadsheet

I would like to update my calendar from a spreadsheet:
I tried the following:
function caltest1() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 1; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 5);
var data = dataRange.getValues();
var calId = "TestCalendar";
var cal = CalendarApp.getCalendarById(calId);
for (i in data) {
var row = data[i];
var title = row[0]; // First column
var desc = row[1]; // Second column
var tstart = row[2];
var tstop = row[3];
var loc = row[4];
//cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
cal.createEvent(title, tstart, tstop, {description:desc,location:loc});
}
}
However, I get the following error:
TypeError: Cannot call method "createEvent" of null
The following spreadsheet is used:
Link to spreadsheet
Any suggestions, what I am doing wrong?
I appreciate your replies!
Short answer: You are passing the calendar's name to getCalendarById() so it is returning null. Change "TestCalendar" to the ID of the calendar or use getCalendarsByName().
I will walk you through this the way I read it so you can see one way to debug this.
This error is telling you that your cal variable is null on the last line where you call cal.createEvent( ... ). This means that your earlier statement var cal = CalendarApp.getCalendarById(calId); is returning null. Googling the Google Calendar API for this function I see that you are using the correct syntax for getCalendarId() but the string argument you are passing appears to be the calendar's name and not the ID.

Google script google sheet to calendar TypeError: Cannot find function createEvent in object Calendar. (line 18, file "Code")

I have attempted to create a google script that will create calendar events based on information within cells in google sheets. I have gotten to the point where everything is labeled and marked, but an error is occuring that states:
TypeError: Cannot find function createEvent in object Calendar. (line 18, file "Code")
Here is the code I have written:
function calInsert() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 25; // Number of rows to process
var dataRange = sheet.getRange(startRow, 2, numRows, 9);
var data = dataRange.getValues();
var cal = CalendarApp.getCalendarsByName( "EC Calendar" );
for (i in data) {
var row = data[i];
var title = row[1]; // First column
var tstart = row[2]; // Second column
var tstop = row[3];
var desc = row[4];
var loc = row[5];
var cmmail = row[6];
var leamail = row[7];
var event = cal.createEvent(title, new Date(tstart), new Date(tstop), {description:desc,location:loc,guests:cmmail,guests:leamail});
}
}
Any help you can provide would be appreciated.
var cal = CalendarApp.getCalendarsByName("EC Calendar");
This gives you an array of calendars with that name. Change this to:
var cal = CalendarApp.getCalendarsByName("EC Calendar")[0]; //Gets the correct calendar
This gives you the first Calendar with the name. I tried the below code:
function calInsert() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 5);
var data = dataRange.getValues();
var cal = CalendarApp.getCalendarsByName( "calendar ID" )[0];
for (i in data) {
var row = data[i];
var title = row[0]; // First column
var tstart = row[2]; // Second column
var tstop = row[3];
Logger.log(tstop);
var desc = row[1];
var loc = row[4];
//var cmmail = row[6];
//var leamail = row[7];
cal.createEvent(title, new Date(tstart), new Date(tstop), {description:desc,location:loc});
}
}
I am able to create event in my calendar. If needed, you can send cmmail and leamail to createEvent.