Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I modify a google calendar event using google apps script?

I figured out how to add an event to a calendar, but then spent a good 8 hours trying to figure out how to edit an existing event (or delete it, either way would get the job done). Here's what I've got:

function UpdateEventTime() {
  var cal = CalendarApp.getCalendarById("[email protected]");
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 1;  // First row of data to process
  var numRows = 1;   // Number of rows to process
  var dataRange = sheet.getRange(startRow, 5, numRows, 5);
  var data = dataRange.getValues();


//  var oldtstart = SpreadsheetApp.getActiveSheet().getRange('G2');
//  var oldtstop = SpreadsheetApp.getActiveSheet().getRange('H2');
// ??????????????????????????????????????????????????
// ??  How do I call up and delete the old event?  ??  
// ??????????????????????????????????????????????????  



  for (i in data) {
    var row = data[i];
    var title = row[0];  // First column
    var desc = row[1];       // Second column
    var tstart = row[2];
    var tstop = row[3];
    var loc = row[4];
    cal.createEvent(title, tstart, tstop, {description:desc,location:loc});
    SpreadsheetApp.getActiveSheet().getRange('G2').setValue(tstart);
    SpreadsheetApp.getActiveSheet().getRange('H2').setValue(tstop);
 }
}

From what I can tell in the online documentation, you can't pull up an event, you can only pull up all the events in a date range. So at the end of the code I try to store the start time and stop time in a spreadsheet, and then refer back to it next time the script is executed. The commented out section in the middle is where I'm lost. That's where I'm trying to call up the event that I added last time I ran the script. I need to either edit it, or delete it.

Please help.

like image 232
user1764447 Avatar asked Oct 18 '25 18:10

user1764447


1 Answers

It would be better if you manage to save the event Ids in spreadsheet along with those details. If you need to modify or delete those event, just fetch the event by id and do the things.

Modified code For saving

var event = cal.createEvent(title, tstart, tstop, {description:desc,location:loc});
var eventid = event.getId();
SpreadsheetApp.getActiveSheet().getRange('I2').setValue(eventid);

To fetch the event back at later time

var id = SpreadsheetApp.getActiveSheet().getRange('I2');
var cal = CalendarApp.getCalendarById("[email protected]");
var event = cal.getEventSeriesById(id);
//Now modify or delete the event
event.addEmailReminder(minutesBefore);
event.addGuest(email);
event.deleteEvent();
.
.
.

Hope this will help you

like image 177
Waqar Ahmad Avatar answered Oct 21 '25 22:10

Waqar Ahmad