In the previous chapter, you learned how to create Forms programmatically using FormApp
, ContentService
, and HtmlService
. Also, you learned how to use the doGet
and doPost
functions.
In this chapter, you will learn to:
The CalendarApp
class provides direct access to Calendar's basic service. This service allows you to read and update your default as well as subscribed Calendars. Using GAS, you can create Calendar events, and invite your friends programmatically. You can even grab event details and populate them in Sheets.
You can create an event by just passing a description as an argument to the createEventFromDescription
method of the CalendarApp
class:
function createCalendarEventFromDescription(){ CalendarApp.getDefaultCalendar() .createEventFromDescription('Team Meeting, Monday from 3 PM to 4 PM'); }
You can also create events by specifying the title, start time, and end time:
function createCalendarEvents() { var title = "Title of the event"; var startTime = new Date("October 21, 2015 21:00:00"); var endTime = new Date("October 21, 2015 21:30:00"); CalendarApp.getDefaultCalendar() .createEvent(title, startTime, endTime); }
The following code shows how to create an event with the specified options, such as the description and location. Uncomment the sendInvites
line only if you insert the guest's e-mail ID(s). Use a comma to separate them if there is more than one e-mail ID:
function createCalendarEventsWithOptions() { var options = { description : 'Description of the event', location : 'Event Location', //sendInvites : true, //guests : 'Comma-separated list of guest email IDs.' }; var title = "Title of the event"; var startTime = new Date("October 21, 2015 21:00:00"); var endTime = new Date("October 21, 2015 21:30:00"); CalendarApp.getDefaultCalendar() .createEvent(title, startTime, endTime, options); }
To create events from prepopulated Sheets data, create a Sheet named Events
and create column headers as shown here:
Create the function createCalendarEventsFromSheetData
as shown here:
function createCalendarEventsFromSheetData() { /* * 'Events' sheet column numbers, * use 0 for column 'A', * 1 for column 'B' and so on. * This makes life easy to use in '0' indexed JS arrays. * */ const TITLE = 0; const START_TIME = 1; const END_TIME = 2; const DESCRIPTION = 3; const LOCATION = 4; const SEND_INVITES = 5; const GUESTS = 6; var sheet = SpreadsheetApp.getActiveSpreadsheet() .getSheetByName("Events"); var data = sheet.getDataRange().getValues(); // Remove header var header = data.shift(); var options = { description : '', location : '', sendInvites : false, guests : '' }; for(var i in data){ /* * 'data' is a 2-dim array. * First index for row numbers and * second index for column numbers. * */ options.description = data[i][DESCRIPTION]; options.location = data[i][LOCATION]; options.sendInvites = data[i][SEND_INVITES]; options.guests = data[i][GUESTS]; var title = data[i][TITLE]; var startTime = data[i][START_TIME]; var endTime = data[i][END_TIME]; CalendarApp.getDefaultCalendar() .createEvent(title, startTime, endTime, options); } }
Instead of creating events from Sheet data, you can create them from an external CSV file uploaded to the Drive. Upload a CSV file with the same headers as in the previous task.
Get the key/ID of the uploaded file and replace it with the following code:
function createEventsFromCsvData(){ // CSV columns, 0 based. const TITLE = 0; const START_TIME = 1; const END_TIME = 2; const DESCRIPTION = 3; const LOCATION = 4; const SEND_INVITES = 5; const GUESTS = 6; // Put the key/ID of the CSV file placed in Drive. var blob = DriveApp.getFileById("[[ CSV file id ]]").getBlob(); var str = blob.getDataAsString(); var data = Utilities.parseCsv(str); // Now the data is a two-dimensional array // Remove header data.shift(); var options = { description : '', location : '', sendInvites : false, guests : '' }; for(var i in data){ // Skip if no title if(!data[i][0]) continue; // Populate the options object options.description = data[i][DESCRIPTION]; options.location = data[i][LOCATION]; options.sendInvites = data[i][SEND_INVITES]; options.guests = data[i][GUESTS]; var title = data[i][TITLE]; var startTime = data[i][START_TIME]; var endTime = data[i][END_TIME]; CalendarApp.getDefaultCalendar() .createEvent(title, startTime, endTime, options); } }
18.221.161.201