0

My code works fine when exporting to google calendar, but it started showing the error message in my title. It never did that before. It still works and everything gets exported just fine, except it annoys me to have an error message I don't understand.

function export1() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lr = ss.getLastRow();
  var cal = CalendarApp.getCalendarById("some ID");

  var data = ss.getRange("E2:H" + lr).getValues();

  for(var i = 0;i<data.length;i++){

    cal.createAllDayEvent(data[i][0], data[i][1], {location: data[i][2], 
guests: data[i][3], sendInvites: true});

    } 
}

function exportMail() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lr = ss.getLastRow();
  var cal = CalendarApp.getCalendarById("some ID");

  var data = ss.getRange("A2:D" + lr).getValues();

  for(var i = 0;i<data.length;i++){

    cal.createAllDayEvent(data[i][0], data[i][1], {location: data[i][2], 
guests: data[i][3], sendInvites: true});

    } 
}

//

Like I said, it exports just fine, but only recently it started showing that error.

Pabz
  • 11
  • 6

1 Answers1

0

Cannot find method createAllDayEvent(string,string,object)

This error message is telling you that there is no matching method for the arguments you have provided to createAllDayEvent(). Checking the documentation shows that the date provided to createAllDayEvent() must be in the form of a Date() object.

If data[i][1] is an acceptably formatted date, you can simply wrap it in new Date(data[i][1]) and pass that as an argument instead.

sinaraheneba
  • 781
  • 4
  • 18
  • Could you help me out with this? I guess you mean I need to create a variable called new Date? I'm only a beginner with this and was quite proud I got it to work in the first place haha. Thanks – Pabz May 22 '19 at 09:30
  • @pabz typo, or missed something? – sinaraheneba May 22 '19 at 09:31
  • ye sorry, was struggling with the comments haha – Pabz May 22 '19 at 09:33
  • 1
    You need to pass an [instance of](https://stackoverflow.com/questions/6395754/difference-between-reference-and-instance-in-javascript) the [Date() object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) to the function as the second argument. If `data[i][1]` is a format that Date() understands, you can drop `new Date(data[i][1])` in that place to create an object that the function can use. – sinaraheneba May 22 '19 at 09:35
  • Thanks, I'll look into that and wil get back to you! – Pabz May 22 '19 at 09:51
  • I guess I did something wrong. `var data = ss.getRange("E2:H" + lr).getValues(); var date1 = new Date(data[i][0]) var date2 = new Date(data[i][1]) for(var i = 0;i – Pabz May 22 '19 at 11:38
  • Again, check the documentation--The *second* argument must be a date object. The *first* object must be a string. And check your code carefully; what do you expect the value of `i` to be *before* you define it in your for-loop? – sinaraheneba May 22 '19 at 11:41
  • Ah yes, sorry got confused. `data[i][0]` is the title, so makes no sense to pass it as a date() object haha. I changed `data[i][1]` to `new Date(data[i][1])` and now it works fine. Thanks! – Pabz May 22 '19 at 12:27