I'd like to extract events from Google Calendar for a dynamic range: from (today) to (today+28), giving me a 29 day range of data.

How can I specify that using Google Apps Script?

1 Answer 1


Here is a script that

  1. Calculates the startPeriod time, defined as the beginning of today, and the endPeriod, 28 days later.
  2. Grabs the events using getEvents method.
  3. Gets some properties of each event to list in the sheet; as an example, I pick StartTime, EndTime, and Title for each event.
  4. Puts the array of selected properties in the currently active sheet.
function listEvents() {
  var id = "...";   // enter Calendar Id, found on its Settings page
  var cal = CalendarApp.getCalendarById(id);
  var startPeriod = new Date();
  startPeriod.setHours(0, 0, 0, 0);
  var endPeriod = new Date(startPeriod);
  endPeriod.setDate(endPeriod.getDate() + 28);
  var events = cal.getEvents(startPeriod, endPeriod);

  var output = [];
  for (var i = 0; i < events.length; i++) { 
    output.push([events[i].getStartTime(), events[i].getEndTime(), events[i].getTitle()]);    

  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1, 1, output.length, output[0].length).setValues(output);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.