Get eventID for Google Calendar Event

2019-08-21 23:58发布

问题:

I would like to get the eventID from a specific event with the help of google script. I'm using currently this variable:

function export_gcal_to_gsheet(){

// Reference Websites:
// https://developers.google.com/apps-script/reference/calendar/calendar
// https://developers.google.com/apps-script/reference/calendar/calendar-event
//

var mycal = "info@neugier.schule";
var cal = CalendarApp.getCalendarById(mycal);
var today = new Date()
var thisweek = new Date (today.getTime())
var oneWeekfromNow = new Date(today.getTime() + (7*3600000*24));

//
var events = cal.getEvents(thisweek, oneWeekfromNow, {search: 'stammtisch'});

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Termine");
// Uncomment this next line if you want to always clear the spreadsheet content before running - Note people could have added extra columns on the data though that would be lost
//sheet.clearContents();  

// Create a header record on the current spreadsheet in cells A1:N1 - Match the number of entries in the "header=" to the last parameter
// of the getRange entry below
var header = [["Event Title", "Event Start", "Thema", "LinkZoom", "EventID"]]
var range = sheet.getRange(1,1,1,5);
range.setValues(header);


// Loop through all calendar events found and write them out starting on calulated ROW 2 (i+2)
for (var i=0;i<events.length;i++) {
var row=i+2;
var myformula_placeholder = '';
// Matching the "header=" entry above, this is the detailed row entry "details=", and must match the number of entries of the GetRange entry below
// NOTE: I've had problems with the getVisibility for some older events not having a value, so I've had do add in some NULL text to make sure it does not error
  var details=[[events[i].getTitle(), Utilities.formatDate(events[i].getStartTime(),'Europe/Berlin', 'dd.MM.yyyy,\' um\' HH:mm\'Uhr\''), events[i].getDescription(), events[i].getLocation(), events[i].getId()]];

var range=sheet.getRange(row,1,1,5);
range.setValues(details);

}
}

But it seems that getId is a different than the event ID, because when I insert the ID into this link: https://www.google.com/calendar/event?eid={event-id}&ctz={timezone}

I get an error. Also when I share my event manually I see a different ID, than the ID I get from my script.

Hope that someone can help me out.

回答1:

I understand that you want event-id for using https://www.google.com/calendar/event?eid={event-id}&ctz={timezone}. Is my understanding correct? If my understanding is correct, how about this modification?

Modification point :

  • In order to retrieve event-id of https://www.google.com/calendar/event?eid={event-id}&ctz={timezone}, you can retrieve it by using Calendar.Events.list() of Calendar API.

Preparation to use this modified sample script :

When you use this modified script, please enable Calendar API at Advanced Google Services and API console.

Enable Calendar API v3 at Advanced Google Services

  • On script editor
    • Resources -> Advanced Google Services
    • Turn on Calendar API v3

Enable Calendar API at API console

  • On script editor
    • Resources -> Cloud Platform project
    • View API console
    • At Getting started, click Enable APIs and get credentials like keys.
    • At left side, click Library.
    • At Search for APIs & services, input "Calendar". And click Calendar API.
    • Click Enable button.
    • If API has already been enabled, please don't turn off.
    • When you run this script, if an error occurs, you might be required to wait few minutes until the API is enabled.

Modified script :

function export_gcal_to_gsheet(){
  var mycal = "info@neugier.schule";
  var cal = CalendarApp.getCalendarById(mycal);
  var today = new Date()
  var thisweek = new Date (today.getTime())
  var oneWeekfromNow = new Date(today.getTime() + (7*3600000*24));
  var events = cal.getEvents(thisweek, oneWeekfromNow, {search: 'stammtisch'});
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Termine");
  var header = [["Event Title", "Event Start", "Thema", "LinkZoom", "EventID"]]
  var range = sheet.getRange(1,1,1,5);
  range.setValues(header);

  // Added
  var items = Calendar.Events.list(mycal, {
    timeMin: thisweek.toISOString(),
    timeMax: oneWeekfromNow.toISOString(),
    q: 'stammtisch'
  }).items;

  for (var i=0;i<events.length;i++) {
    var row=i+2;
    var myformula_placeholder = '';
    var details=[[events[i].getTitle(), Utilities.formatDate(events[i].getStartTime(),'Europe/Berlin', 'dd.MM.yyyy,\' um\' HH:mm\'Uhr\''), events[i].getDescription(), events[i].getLocation(), items[i].htmlLink.split("=")[1]]]; // Modified
    var range=sheet.getRange(row,1,1,5);
    range.setValues(details);
  }
}

Note :

  • This modified script retrieves htmlLink from Calendar.Events.list(). The link is https://www.google.com/calendar/event?eid={event-id}. Now I retrieved event-id from the link for matching your script using items[i].htmlLink.split("=")[1]. If you want the link, please modify it to items[i].htmlLink.

References :

  • Advanced Google Services
  • Calendar.Events.list()
  • toISOString()

If I misunderstand your question, I'm sorry.