发生错误:TypeError:无法通过Google Script调用未定义的方法“createEvent”

时间:2012-06-29 23:54:00

标签: google-apps-script

在我的脚本和函数的后半部分,我一直得到这个不能调用方法错误,我不知道为什么。我完全模仿了模板脚本,但我不确定它为什么不调用该方法。

非常感谢任何见解。

感谢。

function createEvents(e){

//Get the active application
var app = UiApp.getActiveApplication();

try{
   //get the entries;
   var eventDate = e.parameter.eventDate;
var eventPeople = e.parameter.eventPeople;
var eventCompany = e.parameter.eventCompany;
var eventName = e.parameter.eventName;
var eventTime = e.parameter.eventTime;  
var eventPhone = e.parameter.eventPhone;  
var eventEmail = e.parameter.eventEmail;  
 var eventTaken = e.parameter.eventTaken;  


//Get the calendar
var cal = CalendarApp.getCalendarsByName('Phoenix Reservations')[0];//Change the calendar name
var eventStartTime = eventDate;
//End time is calculated by adding an hour in the event start time 
var eventEndTime = new Date(eventDate.valueOf()+60*60*1000);
//Create the events
cal.createEvent(eventPeople,eventCompany,eventName,eventTime,eventPhone,eventEmail,eventTaken);

//Log the entries in a spreadsheet
var ss = SpreadsheetApp.openById('KEY_TAKEN_OUT');//Change the spreadhseet key to yours
var sheet = ss.getSheets()[0];
sheet.getRange(sheet.getLastRow()+1, 1, 1, 5).setValues([[new Date(), eventDate,eventPeople,eventCompany,eventName,eventTime,eventPhone,eventEmail,eventTaken, 'Event created']]);

//Show the confirmation message
app.add(app.createLabel('Event created Successfully'));
//make the form panel invisible
app.getElementById('panel').setVisible(false);
return app;
}

  //If an error occurs, show it on the panel
  catch(e){
   app.add(app.createLabel('Error occured: '+e));
   return app;
 }
}

1 个答案:

答案 0 :(得分:1)

这一行可能什么都不返回:

var cal = CalendarApp.getCalendarsByName('Phoenix Reservations')[0];//Change the calendar name

您可以将其记录为确认,如Logger.log(cal)

'Phoenix Reservations'是您拥有的日历名称还是您具有写入权限?

编辑:您可以测试这个简单的功能,看看这个日历是否一切正常吗? 它将刚刚创建一个事件。

function testcal(){
  var cal = CalendarApp.openByName('Phoenix Reservations');// or you can replace this with your var definition : same result normally ;-)
  if (cal) {
  var title = 'Test Event';
  var start = new Date();
  var end = new Date(start.valueOf()+60*60*1000);
  Logger.log(cal.getName()+' '+start+'   '+end)
  var desc = 'Created using Google Script';
  var loc = 'there';

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

编辑2:我修改了记录器,使其显示日历名称。