【问题标题】:How can I import URL from google calendars to google sheets using Appscript?如何使用 Apps 脚本将 URL 从谷歌日历导入谷歌表格?
【发布时间】: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


【解决方案1】:

如果您检查Apps Script DocumentGoogle Apps Script Quickstart (for Calendar)

高级日历服务允许您在 Apps 脚本中使用公共 Google Calendar API。与 Apps 脚本的 built-in Calendar service 非常相似,此 API 允许脚本访问和修改用户的 Google 日历,包括用户订阅的其他日历。在大多数情况下,内置服务更易于使用,但此高级服务提供了一些额外功能,包括为各个事件设置背景颜色。

检查变量event 内容的一种方法是使用Logger.log。你可以试试这个示例代码:

function listUpcomingEvents() {
  var calendarId = 'primary';
  var optionalArgs = {
    timeMin: (new Date()).toISOString(),
    showDeleted: false,
    singleEvents: true,
    maxResults: 10,
    orderBy: 'startTime'
  };
  var response = Calendar.Events.list(calendarId, optionalArgs);
  var events = response.items;
  if (events.length > 0) {
    for (i = 0; i < events.length; i++) {
      var event = events[i];
      Logger.log(event)
      var when = event.start.dateTime;
      if (!when) {
        when = event.start.date;
      }
      Logger.log('%s (%s)', event.summary, when);
    }
  } else {
    Logger.log('No upcoming events found.');
  }
}

如果您要查看Events 资源:

html链接

  • 在 Google 日历 Web UI 中指向此事件的绝对链接。只读。

这里还有一个教程:Export from Calendar to Sheet using Apps Script

希望这会有所帮助!

【讨论】:

  • 那么,您是说为了获得每个活动的 html 链接,我必须启用高级日历服务?
  • 是的,您可能想查看此文件enhancement for Google Apps Script
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多