Adrienne
Adrienne

Reputation: 27

How do I get my google calendar event's ID into the right format for the Calendar API to read in my script?

I'm trying to write a script that will do both: 1) update an existing event's dates and title on my Google calendar, based on the dates provided in my sheet AND 2) send the updated dates by email to the event's guests (so the event on their calendars is updated).

I was able to successfully update my event's dates and title using CalendarApp.getEventById(eventId). However, I'd really like the event notifications to resend to each guest when the dates of the event have updated. This doesn't seem possible to do using CalendarApp (if it's possible, please tell me how!). After struggling with this in this question, I'm now trying to use Calendar.Events.patch to both update the event and send the update to all the event's guests.

The problem I keep encountering is that the eventId, as logged in my sheet by my createEvent script, is in a format ending in "@google.com." This format is read just fine by CalendarApp, but cannot be read by Calendar.Events.patch. I've tried encoding it to base64, since that seeems to be the format looked for by Calendar.Events.patch, but I think I'm still missing something.

When I run the code below, the error returns "API call to calendar.events.get failed with error: Not Found."

function updateEvent(calendarId, eventId, email) {
  var vSS = SpreadsheetApp.getActiveSpreadsheet();
  var vS = vSS.getActiveSheet();
  var eventId = vS.getRange("R2").getValue(); //R2 is the cell where my eventId is logged

//My attempt to encode my eventId to base64
  var base64data = eventId;
  var encoded = Utilities.base64Encode(base64data,Utilities.Charset.UTF_8);

  var vEventName = vS.getRange("M3").getValue();
  var newstartTime = vS.getRange("M5").getValue();
  var newendTime = vS.getRange("M6").getValue();
  var event = Calendar.Events.get('[email protected]', encoded);
  if(event.attendees) {
    event.attendees.push({
      email: email
    });
  } else {
    event.attendees = new Array({email: email});
  }
  event = Calendar.Events.patch(vEventName, encoded, {
    start: newstartTime,
    end: newendTime,
    sendUpdates: "all"});
}

I suspect the encoded eventId is off a few characters or something, since that seems to be the case when I run the @google.com eventId through this encoding tool. I just don't know enough to fix it.

Please understand that I'm extremely new to writing scripts and I've already been struggling with this issue for well over a week. I think I learned a lot after the last question I posted, since now I have a better understanding of my exact problem, but I still don't know the fix. I'm trying to teach myself how to do this by reading the questions on this website, reading Google's information (which I unfortunately largely don't understand), and watching YouTube videos. If I'm asking too much of the script, please let me know.

Upvotes: 2

Views: 2778

Answers (1)

Tanaike
Tanaike

Reputation: 201378

  • You want to retrieve an event using event ID.
  • You want to update an event and send an email to users.
    • You want to modify the event name, start and end time of the event.
  • eventId is [email protected] which is a text value.
  • vEventName is Ethiopia Limu which is a text value.
  • newstartTime is 6/10/2019 which is a date object.
  • newendTime is 7/30/2019 which is a date object.

If my understanding is correct, how about this modification?

Modification points:

  • In your script, it seems that the scripts for retrieving the event and updating the event are different. So in this modification, there were separated.
    • You can see the event at the log, when the script is run.
    • The event is updated.
  • About the event ID, you can use 1c0tqtn56c1tdo1i0fus66f53g of [email protected] as the event ID.

When these are reflected to your script, it becomes as follows.

Modified script:

function updateEvent(calendarId, eventId, email) {
  var vSS = SpreadsheetApp.getActiveSpreadsheet();
  var vS = vSS.getActiveSheet();
  var eventId = vS.getRange("R2").getValue();
  var vEventName = vS.getRange("M3").getValue();
  var newstartTime = vS.getRange("M5").getValue();
  var newendTime = vS.getRange("M6").getValue();

  var eid = eventId.split("@")[0]; // Added

  // Retrieve event
  var event = Calendar.Events.get(calendarId, eid);
  if(event.attendees) {
    event.attendees.push({
      email: email
    });
  } else {
    event.attendees = new Array({email: email});
  }
  Logger.log(event)

  // Update event
  var resource = {
    start: {dateTime: newstartTime.toISOString()},
    end: {dateTime: newendTime.toISOString()},
    summary: vEventName,
  };
  Calendar.Events.patch(resource, calendarId, eid, {sendUpdates: "all"})
}

Note:

  • If an error related to date and time occurs when the event is updated, please confirm whether newstartTime and newendTime are the date object, respectively.

Reference:

Upvotes: 1

Related Questions