Home > Enterprise >  Get events from multiple calendars in Google Apps Script
Get events from multiple calendars in Google Apps Script

Time:09-26

I'm posting after having read different posts here in SO, and still, I didn't manage to solve my problem. What I'm trying to do is basically to retrieve from Google Calendars the events from different calendars and have them listed in an excel file. The script below correctly gets the events from the first calendar, but not from the others. From the others, I basically get an empty array. Here is the code I'm working with:

function getEvents() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Sheet1");
  var sheetsoptions = ss.getSheetByName("Options");

  var start_time = sheet.getRange("A2").getValue();
  var end_time = sheet.getRange("B2").getValue();
  // var id_cal = sheet.getRange("C2").getValue();
  var id_cal = sheetsoptions.getRange("A5:A6").getValues();
  var arrayLength = id_cal.length;
  var cell_events = 5;

  for (var j = 0; j < arrayLength; j  ) {
    if (id_cal[j] == ""){
      break;
    }
    
    else{
    var cal = CalendarApp.getCalendarById(id_cal[j]);
      var events = cal.getEvents(new Date(start_time), new Date(end_time));

      for (var i = 0;i<events.length;i  ){
        
        var title =  events[i].getTitle();
        var start_time =  events[i].getStartTime();
        var end_time =  events[i].getEndTime();
        var loc = events[i].getLocation();
        var des =  events[i].getDescription();
        var vis = events[i].getVisibility();
        

        sheet.getRange(cell_events,1).setValue(title);
        sheet.getRange(cell_events,2).setValue(start_time);
        sheet.getRange(cell_events,3).setValue(end_time);
        sheet.getRange(cell_events,4).setValue(loc);
        sheet.getRange(cell_events,5).setValue(des);
        sheet.getRange(cell_events,6).setValue(vis);
        cell_events  ;
      }
      }
  }

  Logger.log("Events have been added to the Spreadsheet");
  
}

Thanks in advance for your help. Luca

CodePudding user response:

I think you just had a small issue with your calid being a 2d array. Try this, it's almost the same thing that you had, written a little differently.

function getEvents() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName("Sheet1");
  var sheetsoptions = ss.getSheetByName("Options");
  var start_time = sheet.getRange("A2").getValue();
  var end_time = sheet.getRange("B2").getValue();
  var calid = sheetsoptions.getRange("A5:A6").getValues().flat().filter(id => id);//test for truthiness
  var row = 5;
  calid.forEach(id => {
    var cal = CalendarApp.getCalendarById(id);
    var events = cal.getEvents(new Date(start_time), new Date(end_time));
    events.forEach(ev => {
      var title = ev.getTitle();
      var start_time = ev.getStartTime();
      var end_time = ev.getEndTime();
      var loc = ev.getLocation();
      var des = ev.getDescription();
      var vis = ev.getVisibility();
      sheet.getRange(row  , 1, 1, 6).setValues([[title, start_time, end_time, loc, des, vis]]);//this should save some time writing all five columns at the same time
    });
  });
  Logger.log("Events have been added to the Spreadsheet");
}

CodePudding user response:

Issue:

You are using the same variables for minimum and maximum dates when listing calendar events and for getting the start and end time of each event (start_time, end_time).

Because of this, after the first calendar iteration, start_time and end_time are not the times defined in cells A2 and B2, but the start and end times of the last event in the first calendar.

Solution:

Update one of the two sets of variables so there's no confusion. For example:

var event_start_time =  events[i].getStartTime();
var event_end_time =  events[i].getEndTime();
// ...
sheet.getRange(cell_events,2).setValue(event_start_time);
sheet.getRange(cell_events,3).setValue(event_end_time);

Note:

Consider using setValues to write all event values at once instead of setValue, in order to minimize calls to Sheets service and improve efficiency (see Use batch operations).

  • Related