Skip to content

Instantly share code, notes, and snippets.

@wkrsz
Created December 27, 2010 12:01
Show Gist options
  • Save wkrsz/756086 to your computer and use it in GitHub Desktop.
Save wkrsz/756086 to your computer and use it in GitHub Desktop.
Google App Script - import calendar events into a spreadsheet
function import_calendar( start_date, end_date, cal_name, spreadsheet, sheet_name) {
var sheet = spreadsheet.getSheetByName(sheet_name);
var cal = CalendarApp.getCalendarsByName(cal_name)[0];
sheet.clear();
events = cal.getEvents( start_date, end_date );
irow = 2;
for( i in events ) {
evt = events[i];
var date = evt.getStartTime();
var formatted_date = (date.getMonth()+1) + '/' + date.getDate() + '/' + date.getYear();
sheet.getRange("A"+irow).setValue( formatted_date );
sheet.getRange("B"+irow).setValue( evt.getTitle() );
irow ++;
}
}
function import_calendars() {
var start_date = new Date("Jan 1, 2010");
var end_date = new Date("Jan 1, 2011");
var ss = SpreadsheetApp.getActiveSpreadsheet();
import_calendar( start_date, end_date, "holidays", ss, "holidays" );
import_calendar( start_date, end_date, "days off - Daniel", ss, "days off - Daniel" );
}​
@hardymathieu
Copy link

Hey,

I've dnoe something similar that imports the coming 200 days worth of events into the spreadsheet

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [ {name: "Get Calendar Report", functionName: "SaveToSheet"},
                     ];
  ss.addMenu("Get Calendar Report", menuEntries);
}



  function SaveToSheet() 
{
  var Settings = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Settings");
  var CalSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Calendar");
  var CalRow = CalSheet.getLastRow()+1;
  var CALfrom = Settings.getRange("A2").getValue();
  var dayrange = new Date;
  dayrange.setDate(dayrange.getDate()+200);
  var events = CalendarApp.getCalendarById(CALfrom).getEvents(new Date(), dayrange);
  var customeventID = new Array();
  var EventsCopyInSheet = CalSheet.getDataRange().getValues();
  var EventsCopyIDInSheet = new Array();
  
  
  //var eventCopy = CalendarApp.getCalendarById(CALto).getEvents(new Date(), new Date("01/01/2013"));
  //var eventCopyID = new Array();
  var TABLEAUDESEVENT = new Array();
  
  for (k=0; k feuille du calendrier  
  //  CalRow=> où on écrit les données var CalSheet.getRange(row,1,1,4);
  //  range.setValues(details);
      
   
   TABLEAUDESEVENT.push (new Array(title, stime, etime, elocation , edescription, status, creator, datecreated, eventID, lastupdate, visibility));
   
      
    }
    
       
    
    //otherwise, do nothing.
  }
  
      
 CalSheet.getRange(CalRow,1,TABLEAUDESEVENT.length,TABLEAUDESEVENT[0].length).setValues(TABLEAUDESEVENT);
 //CALtoCOPY.createEvent(title, stime, etime, { location:elocation , description:description});
      
      
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment