Search code examples
javascriptgoogle-apps-script

Exception: (String,(class),String) don't match the method signature for CalendarApp.Calendar.createEvent


I'm writing a function to create events on Google Calendar with a spreadsheet. The goal is to use a date-formatted cell to set up the parameters of the event to create it. I set up my code, and the problem is that it returns this error:

Exception: The parameters (String,(class),String) don't match the method signature for CalendarApp.Calendar.createEvent.

I think the problem is the "eventos[1]" that is not supposed to be a date, but I can't find what I'm doing wrong. What could be the problem?

Thanks for the help! Below is my script code:

function SheetsToCalendar() {

  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var hojaCitas = spreadsheet.getSheetByName("BBDD_Citas");
  var configCalendar = spreadsheet.getSheetByName("Config");
  var calendarID = configCalendar.getRange("C2").getValue();
  var eventCal = CalendarApp.getCalendarById(calendarID);

  var fechaHora = hojaCitas.getRange("A2:B1000").getValues();
  var titulos = hojaCitas.getRange("F2:F1000").getValues();

  for(x=0; x<fechaHora.length;x++){
    var eventos = fechaHora[x];
    var nombres = titulos[x];

    var startTime = eventos[0];
    var endTime = eventos[1];
    var titulo = nombres[0];
    

    eventCal.createEvent(startTime,endTime,titulo);
  }
  
}

Solution

  • It seems like you are not passing expected date formate here, eventCal.createEvent(startTime,endTime,titulo);

    startTime and endTime should be Date objects

    You need to convert the date values into JavaScript Date objects before passing them to you'r createEvent().

    
        var startTime = new Date(eventos[0]); // Js Date object
        var endTime = new Date(eventos[1]);   // Js Date object
        var titulo = nombres[0];
    
    

    And one more thing title should come first

    eventCal.createEvent(titulo, startTime, endTime)
    

    Ref - https://developers.google.com/apps-script/reference/calendar/calendar