Jump to content

Recommended Posts

Posted

Just to share my little error that started on Friday. We use Google resource calendars and appointment slots for Room bookings and a google form linked via script to a Google calendar for booking out a certain number of iPads from our bank.

Anyway I accidentally deleted all of the resource calendars on Friday, went on chat with Google support but no - resource calendars aren't restorable (I presume user calendars are if the whole user is deleted), so I spent Friday recreating the calendars and the appointment slots for our resources and today working out how to back up the calendar automatically in case I'm ever stupid again.

 

I have cobbled together and stole from a few scripts to come up a Google script. You'll need to enable the Sheets and Calendar API and change the details for calendarID, spreadsheetID and worksheetname. Any other problems let me know. You can also copy the script inbetween the first and last lines multiple times for different resource calendars in a different worksheet. I've then set a time based trigger to run this every hour.

 

function listUpcomingEvents() {
 var calendarId = '[email protected]';
 var optionalArgs = {
   timeMin: (new Date()).toISOString(),
   showDeleted: false,
   singleEvents: true,
   maxResults: 200,
   orderBy: 'startTime'
 };
 var response = Calendar.Events.list(calendarId, optionalArgs);
 var events = response.items;
 if (events.length > 0) {
             var ss = SpreadsheetApp.openById('SPREADSHEETIDGOESHERE');
             var sheet = ss.getSheetByName('NAMEOFWORKSHEET');
             
             var lastrow = sheet.getLastRow() - 1
             if (lastrow > 1){
   sheet.deleteRows(2, lastrow)
   }
   for (i = 0; i < events.length; i++) {
     var event = events[i];
     var when = event.start.dateTime;
     var end = event.end.dateTime
     if (!when) {
       when = event.start.date;
         end = event.end.date;
     }
     
       Logger.log(event.summary+' '+when+'   '+end);
   sheet.getRange(sheet.getLastRow()+1, 1, 1, 4).setValues([[new Date(), event.summary, when, end]]);
   }
 } else {
   Logger.log('No upcoming events found.');
 }
}

  • Thanks 1
Posted
Just to make it clear this script logs a row to a Google sheet for each event. It isn't a full backup of the calendar but enough for me if I needed to recreate the calendar if I ever muck up my GAM script again.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...