通过Google应用脚本在日历上创建带附件的活动

iJa*_*Jay 2 google-apps-script

我找不到添加附件到日历活动的方法.我希望应该有一个简单的方法,如下面的代码片段,

function createNewEvent()
{
 var file = DriveApp.getFileById('1eqaThzYmTbZzP-my file id-rXrBrWDW8DwMNeU');   //get file to be attached
 var title  = 'Apollo 11 Landing';
 var startTime = new Date('January 20, 2016 20:00:00 UTC');
 var endTime = new Date('January 20, 2016 21:00:00 UTC');
 var options = {description:'Sample description', location: 'The Moon', attachments:file}; //can we add attachments like this?

 var event = CalendarApp.getDefaultCalendar().createEvent(title, startTime, endTime, options);
}
Run Code Online (Sandbox Code Playgroud)

这可能吗?

BMc*_*McV 7

是的,这是可能的.首先,您必须启用先进的日历服务.然后你可以做这样的事情:

function createNewEvent() {
  var calendarId = ''; //Calendar Id String
  var fileId = ''; // File Id String
  var start = new Date('January 20, 2016 20:00:00 UTC');
  var end = new Date('January 20, 2016 21:00:00 UTC');
  var eventObj = {
    summary: 'Apollo 11 Landing',
    location: 'The Moon',
    description: 'Sample description',
    start: {dateTime: start.toISOString()},
    end: {dateTime: end.toISOString()},
    attachments: [{
        'fileUrl': 'https://drive.google.com/open?id=' + fileId,
        'title': 'Moon Docs'
    }]
  };
  var resp = Calendar.Events.insert(eventObj, calendarId, {'supportsAttachments': true});
  Logger.log(resp); // Check out the response in the logs!
}
Run Code Online (Sandbox Code Playgroud)

有关更多选项,请查看" 事件"文档.