获取Google日历活动的eventID

时间:2018-01-02 20:32:39

标签: google-apps-script google-sheets google-calendar-api

我想在google脚本的帮助下从特定事件中获取eventID。我目前正在使用这个变量:

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);

}
}

但似乎getId与事件ID不同,因为当我将ID插入此链接时:https://www.google.com/calendar/event?eid= {event-id}&amp; ctz = {timezone}

我收到错误。此外,当我手动共享我的事件时,我看到的ID与我从脚本中获取的ID不同。

希望有人可以帮助我。

1 个答案:

答案 0 :(得分:1)

我们了解您希望https://www.google.com/calendar/event?eid={event-id}&ctz={timezone}使用event-id。我的理解是否正确?如果我的理解是正确的,那么这个修改怎么样?

修改点:

  • 要检索https://www.google.com/calendar/event?eid={event-id}&ctz={timezone} Calendar.Events.list(),您可以使用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); } } Calendar API检索它。

准备使用此修改后的示例脚本:

使用此修改过的脚本时,请在Advanced Google Services和API控制台上启用Calendar API。

在高级Google服务中启用Calendar API v3

  • 在脚本编辑器上
    • 资源 - &gt;高级Google服务
    • 启用Calendar API v3

Enable Calendar API at API console

  • 在脚本编辑器上
    • 资源 - &gt;云平台项目
    • 查看API控制台
    • 在“入门”中,单击“启用API”并获取密钥等凭据。
    • 在左侧,单击“库”。
    • 在搜索API&amp;服务,输入“日历”。然后点击日历API。
    • 单击“启用”按钮。
    • 如果已启用API,请不要关闭。
    • 运行此脚本时,如果发生错误,可能需要等待几分钟才能启用API。

修改后的脚本:

htmlLink

注意:

  • 此修改后的脚本从Calendar.Events.list()检索https://www.google.com/calendar/event?eid={event-id}。链接为event-id。现在,我使用items[i].htmlLink.split("=")[1]从链接中检索items[i].htmlLink以匹配您的脚本。如果您需要该链接,请将其修改为sma<-sma(data = forewing_R ,formula = y~x*groups, method = "SMA", type = "shift", log = "xy") plot(sma, col = c("Black", "Red", "Blue", "Dark Green"), pch= c(5:10), log = "xy") legend(0.765, 0.84, c("Control", "Low", "Medium", "High"), pch = c(5,7,8,6), col=c("Black", "Blue", "Dark Green", "Red"))

参考文献:

如果我误解了你的问题,我很抱歉。