【发布时间】:2016-09-26 21:19:50
【问题描述】:
我正在编写一个自动将谷歌日历事件导入谷歌表格的 Appscript。然而,我似乎在导入 URL 以链接到日历事件时遇到了问题。
是否有一个功能可以让我轻松地做到这一点,或者,有没有人知道不涉及高级服务的方法。
提前致谢
PS。这是我到目前为止所做的工作,是我在网上看到的一些内容。如果代码不好,请道歉。
我正在努力获取日历邀请的 URL。
function calendar_importer(){
//
// Import google calendar events into google sheets.
//
// This code retrieves events between the current date and any date in the future.
// It logs the results in the current spreadsheet starting at cell A2 listing the events,
// dates/times, etc and even calculates event duration (via creating formulas in the spreadsheet) and formats the values.
//
// I do re-write the spreadsheet header in Row 1 with every run, as I found it faster to delete then entire sheet content,
// change the parameters, and re-run my exports versus trying to save the header row manually...so be sure if you change
// any code, you keep the header in agreement for readability!
//
// 1. Please modify the value for mycal to be YOUR calendar email address or one visible on your MY Calendars section of your Google Calendar
// 2. Please modify the values for events to be the date/time range you want and any search parameters to find or omit calendar entires
// Note: Events can be easily filtered out/deleted once exported from the calendar
//
// This script is triggered to run once every 5 minutes, doing this will wipe the spreadsheet in its entirity and download a new set of data.
// This way only the most up to date information is displayed from the calendar.
//
// Enter the address of the calendar you wish to download onto the google spreadsheet below.
var mycal = "edmsmedical@gmail.com";
// Request calendar from google calendars
var cal = CalendarApp.getCalendarById(mycal);
// Calculate today + 365 days for rolling yearly end date.
var EndDate = new Date();
EndDate.setDate(EndDate.getDate()+365);
// Set the desired dates for the spreadsheet to show below. () denotes todays date and will only download events from and including todays date until the set end date.
// Should you wish to select specific dates enter a date and time value in the () for start and end date.
var events = cal.getEvents(new Date(), new Date(EndDate));
// Activates sheet so that it can be cleared as per next instruction.
var sheet = SpreadsheetApp.getActiveSheet();
// 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 = [["Date", "Event Details", "Staffing", "Location", "Start", "End", "Duration", "Calendar Link"]]
var range = sheet.getRange(1,1,1,8);
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.
//*var splitEventId = events[i].getId().split('@');
//*var eventURL = "https://www.google.com/calendar/event?eid=" + Utilities.base64Encode(splitEventId[0] + "&ctz=Ect/GMT" );
//*some work required to complete the URL framework - what format does google use to link URLs
//*var EventId = events[i].getId();
var details=[[events[i].getStartTime(), events[i].getTitle(), events[i].getDescription(), events[i].getLocation(), events[i].getStartTime(), events[i].getEndTime(), events[i].getCreators(), "eventURL"]]
var range=sheet.getRange(row,1,1,8);
range.setValues(details);
var cell=sheet.getRange(row,7);
// Calculation for event duration, displayed in HH:MM
cell.setFormula('=HOUR(F' +row+ '-E' +row+ ')+(MINUTE(F' +row+ '-E' + row +')/60)');
}
}
【问题讨论】:
-
如果您有代码,请将其包含在您的问题中。您将获得更高质量的答案,并且比其他方式更快地获得答案。
标签: javascript google-apps-script calendar google-sheets google-calendar-api