0

我正在为日历使用谷歌脚本,并尝试使用他们的示例。但是当我运行下面的代码时,它一直说不能调用null的方法“createEvent”。其实我不是很了解这里的回调函数。所以它要么是一般的 js 问题,要么是 google 脚本问题。

  var event = cal.createEvent(title, start, end, {
      description : desc,
      location : loc
  });
};

这是整个代码:

function createEvent() {
  /*var cal = CalendarApp.getCalendarById(calendarId);*/
  var cal = CalendarApp.getAllOwnedCalendars()[0];
  var title = 'Script Demo Event';
  var start = new Date("October 26, 2013 08:00:00 EST");
  var end = new Date("October 26, 2013 10:00:00 EST");
  var desc = 'Created using Google Apps Script';
  var loc = 'Script Center';


  var event = cal.createEvent(title, start, end, {
      description : desc,
      location : loc
  });
};



function doGet() { // A script with a user interface that is published as a web app
  // must contain a doGet(e) function.

  // Create the UiInstance object myapp and set the title text
  var myapp = UiApp.createApplication().setTitle('Here is the title bar');

  // Create a button called mybutton and set the button text
  var mybutton = myapp.createButton('Here is a button');
  var handler = myapp.createServerHandler('createEventInvitePeople');
  mybutton.addClickHandler(handler);

  // Create a vertical panel called mypanel and add it to myapp
  var mypanel = myapp.createVerticalPanel();

  // Add mybutton to mypanel
  mypanel.add(mybutton);

  // Add my panel to myapp
  myapp.add(mypanel);

  // return myapp to display the UiInstance object and all elements associated with it.
  return myapp;

}


/**
 * Creates an event, invite guests, book rooms, and sends invitation emails.
 * For more information on using Calendar events, see
 * https://developers.google.com/apps-script/class_calendarevent.
 */
function createEventInvitePeople() {
  var calId = CalendarApp.getAllOwnedCalendars()[0];
  var room1CalId = 'a_room_cal_id';
  var room2CalId = 'another_room_cal_id';
  var guest1Email = 'XXX@gmail.com';
  var guest2Email = 'XXX@gmail.com';
  var invitees = room1CalId + ',' + room2CalId + ',' + guest1Email + ',' +
      guest2Email;

  var cal = CalendarApp.getCalendarById(calId);
  var title = 'Script Center Demo Event';
  var start = new Date("October 30, 2012 08:00:00 PDT");
  var end = new Date("October 30, 2012 10:00:00 PDT");
  var desc = 'Created using Apps Script';
  var loc = 'Script Center';
  var send = 'true';

  var event = cal.createEvent(title, start, end, {
      description : desc,
      location : loc,
      guests : invitees,
      sendInvites : send
  });
};

谁能帮我一个忙?

4

1 回答 1

0

您收到该错误的原因是 getAllOwnedCalendars() 没有返回任何日历,并且您假设它是。因此,您在开始时使用 [0] 将第一个从列表中删除,但它无效。如果您从网络上运行它,您可能需要更具体地查找所需的日历。

于 2014-01-03T17:39:55.767 回答