最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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);
  }
  
}

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);
  }
  
}
Share Improve this question asked Feb 7 at 11:24 Cristina GarcíaCristina García 131 silver badge3 bronze badges New contributor Cristina García is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1
  • According to developers.google.com/apps-script/reference/calendar/…, the method signature is createEvent(title, startTime, endTime) – C3roe Commented Feb 7 at 11:34
Add a comment  | 

2 Answers 2

Reset to default 2

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

You are passing incorrect types to the CalendarApp.Calendar.createEvent() method. The correct signature for createEvent is:

createEvent(title, startTime, endTime)

The function expects the title first, followed by the start and end dates. Here's a fixed 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 (var 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];

    // Ensure valid date objects and non-empty titles
    if (startTime instanceof Date && endTime instanceof Date && titulo) {
      eventCal.createEvent(titulo, startTime, endTime);
    }
  }
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论