TypeError:无法调用null的方法“ createEvent”。 (第38行,文件“代码”)

时间:2019-12-29 14:30:26

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

我正在尝试编写一个程序,将数据从Google表格发布到Google日历,并且我收到此错误:

TypeError: Cannot call method "createEvent" of null. (line 38, file "Code")

以下是我的代码:

function calandarsync() {
  var spreadsheet = SpreadsheetApp.getActiveSheet();
  var eventCal = CalendarApp.getCalendarById('bHVhbmFtYXJhbno3MjBAZ21haWwuY29t');

  var lr = spreadsheet.getLastRow();
  Logger.log(lr);

  var data = spreadsheet.getRange("B2:I"+lr+"").getValues();

  for (x=0; x<data.length; x++) {
    var event = data[x];

    var price = data[0];
    var wordcount = data[2];
    var duedate = data[3];
    var duetime = data[4];
    var order = data[5];
    var buyer = data[6]; 
    var acx = data [7];
    var files = data[8];
    var summary = buyer + " " + wordcount;
    var eventtime = duedate + " " + duetime;
    var final = new Date(eventtime);
    var ONE_MINUTE_IN_MILLIS = 60000;
    var afterAddingTenMins = new Date(final + (10 * ONE_MINUTE_IN_MILLIS));

    var event = {
      'price': price,
      'wordcount' : wordcount,
      'duedate' : duedate,
      'duetime' : duetime,
      'order' : order,
      'buyer' : buyer,
      'acx' : acx,
      'files' : files,
    }
   eventCal.createEvent(summary, final, afterAddingTenMins, event)

  }
}

谢谢大家的帮助!

编辑

这是日志:

[19-12-29 06:27:54:459 PST] 2.0

这是执行时间表:

[19-12-29 06:27:54:304 PST] Starting execution
[19-12-29 06:27:54:310 PST] SpreadsheetApp.getActiveSheet() [0 seconds]
[19-12-29 06:27:54:319 PST] CalendarApp.getCalendarById([bHVhbmFtYXJhbno3MjBAZ21haWwuY29t]) [0.008 seconds]
[19-12-29 06:27:54:458 PST] SpreadsheetApp.Sheet.getLastRow() [0.138 seconds]
[19-12-29 06:27:54:460 PST] Logger.log([2.0, []]) [0 seconds]
[19-12-29 06:27:54:460 PST] SpreadsheetApp.Sheet.getRange([B2:I2]) [0 seconds]
[19-12-29 06:27:54:555 PST] SpreadsheetApp.Range.getValues() [0.094 seconds]
[19-12-29 06:27:54:699 PST] Execution failed: TypeError: Cannot call method "createEvent" of null. (line 38, file "Code") [0.247 seconds total runtime]

1 个答案:

答案 0 :(得分:0)

所以我发现我的数据和事件变量都被弄乱了,不能正常工作。一旦我解决了问题,我就回去了,只是将电子邮件作为id即可使用。这是代码:

function calandarsync() {
  var spreadsheet = SpreadsheetApp.getActiveSheet();
  var eventCal = CalendarApp.getCalendarById('luanamaranz720@gmail.com');

  var lr = spreadsheet.getLastRow();
  Logger.log(lr);

  var count = spreadsheet.getRange("B2:I"+lr+"").getValues();

  for (x=0; x<count.length; x++) {
    var data = count[x];

    var price = data[0];
    var wordcount = data[1];
    var duedate = data[2];
    var duetime = data[3];
    var order = data[4];
    var buyer = data[5]; 
    var acx = data [6];
    var files = data[7];
    var summary = buyer + " " + wordcount;
    var startdate = duedate + " " + duetime;

    var event = {
      'price': price,
      'wordcount' : wordcount,
      'duedate' : duedate,
      'duetime' : duetime,
      'order' : order,
      'buyer' : buyer,
      'acx' : acx,
      'files' : files,
      'description' : 'Price: ' + price + '\n'+ 'Words: ' + wordcount + '\n' + 'Due Date: ' + duedate + '\n' + 'Due  Time: ' + duetime + '\n' + 'Order Number: ' + order + '\n' + 'Buyer Name: ' + buyer + '\n' + 'ACX Format: ' + acx + '\n' + 'Files: ' + files,
    }
   Logger.log(event);
   eventCal.createEvent(summary, duedate, duedate, event)

  }
}