Google脚本:无法调用null方法

时间:2013-10-27 20:03:07

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

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

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

任何人都可以帮我一个忙吗?

1 个答案:

答案 0 :(得分:0)

您收到该错误的原因是因为getAllOwnedCalendars()没有返回任何日历,而您认为它是。所以你在开头使用[0]从列表中取出第一个,但它无效。如果您是通过网络运行此功能,则可能需要更具体地查找所需的日历。

相关问题