Google Script - 找不到方法getEventsforDay(字符串)错误

时间:2013-10-30 15:27:02

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

我正在尝试创建一个Google脚本,允许我将多个工作表中的事件拖到Google日历中,但是我遇到了重复问题。我想我必须在创建新事件之前删除事件(为了更新),但遇到了“无法找到方法getEventsForDay(string)”错误的问题。有什么建议吗?

function pushToPreKCalendar(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Pre-K");
  var lastRow = sheet.getLastRow(); 
  var range = sheet.getRange(3,1,lastRow,100);
  var values = range.getValues();   
  var data = range.getValues();
  var cal1 = CalendarApp.getCalendarsByName("WDW FB")[0];
  var cal2 = CalendarApp.getCalendarsByName("WDW Latino FB")[0];
  var cal3 = CalendarApp.getCalendarsByName("WDW Pinterest")[0];
  for (i in data){ 
  var row = data[i];
  var title = row[0];
  var date = row[2];
  var desc = row[3];
  var cal = row[4];
  var events1 = cal1.getEventsForDay(date);
     for (var i=0; i<events1.length;i++){
     var ev1 = events1[i];
     Logger.log(ev1.getTitle());
     ev1.deleteEvent();
  }
  var events2 = cal2.getEventsForDay(date);
  for (var i=0; i<events2.length;i++){
    var ev2 = events2[i];
    Logger.log(ev2.getTitle());
    ev2.deleteEvent();
  }
  var events3 = cal3.getEventsForDay(date);
  for (var i=0; i<events3.length;i++){
    var ev3 = events3[i];
    Logger.log(ev3.getTitle());
    ev3.deleteEvent();
  }
        if (cal=="WDW FB")
          var newEvent = cal1.createAllDayEvent(title, date, {description:desc});
        if (cal=="WDW Latino FB")
          var newEvent = cal2.createAllDayEvent(title, date, {description:desc});
        if (cal== "WDW Pinterest")
          var newEvent = cal3.createAllDayEvent(title, date, {description:desc});
  }
}

1 个答案:

答案 0 :(得分:0)

问题似乎出现在行上:var date = row[2];date是text类型,也许列有日期类型值,但var date = row[2];返回文本字符串,所以您需要将row[2]的值转换为日期类型,以作为参数传递给方法getEventsForDay(date)

如果你尝试这样的话:

...
var events1 = cal1.getEventsForDay(new Date());
...

未生成错误。