1

I am new here, and somehow new for js for Google Scripts.

I have created a form, which fills a sheet, and want that a Calendar would be update with the data from the sheet. This form and calendar will be shared with a large group, so we all can create Events in a particular format, and to avoid deleting or modifying Events that should not be modified.

When I run the script from the workspace it runs properly, but after defining the trigger to run it, the date format changes (I believe that this is the reason, but I am not sure) and all Events are created for 1969. Below I present part of the script.

So, again, the script runs fine when I start it from the working space, but it changes the date format when running by trigger (onsubmit).
I have already removed and recreated the trigger, but it didn't work.
The date format is set as dd/MM/yyyy, and the time as HH:mm:ss.

Does anybody know how to fix this issue?
Thanks in advance
Best regards

function create_event_PRH(){\
  var calId = ID from Calendar;\
  var calendar = CalendarApp.getCalendarById(calId);\
  var timezone=calendar.getTimeZone();


  // Getting data from the spreadsheet\
  var ss = SpreadsheetApp.getActive();\
  var ss_name = ss.getName();\
  var sheet = ss.getActiveSheet();\
  var range = sheet.getDataRange();\
  var values = range.getValues();\
  var lastRow = sheet.getLastRow()-1;

  // Definition of the starting and finishing time in the spreadsheet (I am using this part of the script to sum date and time of the day)\
  var range = sheet.getRange('P'+String(lastRow+1)).setFormula('H'+String(lastRow+1)+'+I'+String(lastRow+1));\
  var range = sheet.getRange('Q'+String(lastRow+1)).setFormula('H'+String(lastRow+1)+'+J'+String(lastRow+1));\
  Logger.log(Utilities.formatDate(new Date(), timezone, 'dd/MM/yyyy HH:mm:ss'));

  // Start and end time
  var event_values = values[lastRow];\
  var event_start_time=new Date(event_values[15]);\
  var event_end_time=new Date(event_values[16]);


  // Event description and creation\
  var lecture_title=event_values[5];\
  var program_number=event_values[2];\
  var event_title = 'Program'+program_number+' - '+lecture_title;\
  var description='Presenter: '+event_values[10]+'\nInstitution:'+event_values[11];\
  var event = calendar.createEvent(event_title,event_start_time,event_end_time);\
  event = event.setDescription(description);\
}
Fabio TK
  • 11
  • 1
  • In `new Date(event_values[15])`, what is the value of *event_values[15]*? See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Feb 09 '21 at 23:32
  • Howdy! To better analyze the script, could you please share the value logs of the event? That would include the values of the `event_title`, `event_start_time` and `event_end_time` variables just before the event creation takes place. Are you able to replicate this issue by using [Try this API!](https://developers.google.com/calendar/v3/reference/events/insert?apix=true)? If so please share the call and response. – Jacques-Guzel Heron Feb 10 '21 at 08:54
  • Dear RobG, the event_values[15] and [16] are the starting and end times in the format dd/MM/yyyy HH:mm:ss, which is passed as string from the cell. – Fabio TK Feb 10 '21 at 14:53
  • Dear @Jacques-GuzelHeron\ The log data for the event_title is okay. However, the log related to start and end time are weird when running by the trigger, as follows:\ Event Start time from event.getStartime: Wed Dec 31 1969 19:00:00 GMT-0500 (Eastern Standard Time)\ Event End time from event: Wed Dec 31 1969 19:00:00 GMT-0500 (Eastern Standard Time)\ Event Start time from the variable: Invalid Date\ Event End time from the variable: Invalid Date\ The cell values were 05/02/2021 12:00:00 and 05/02/2021 13:00:00\ – Fabio TK Feb 10 '21 at 15:24
  • By running from the workspace, the log is as follows: Event Start time from event.getStartime: Fri Feb 05 2021 10:00:00 GMT-0500 (Eastern Standard Time)\ Event End time from event: Fri Feb 05 2021 11:00:00 GMT-0500 (Eastern Standard Time)\ Event Start time from the variable: Fri Feb 05 2021 10:00:00 GMT-0500 (Eastern Standard Time)\ Event End time from the variable: Fri Feb 05 2021 11:00:00 GMT-0500 (Eastern Standard Time) – Fabio TK Feb 10 '21 at 15:26
  • Strangely, running either by the workspace or trigger onmodify, the date are okay. Just by running by trigger onsubmit it is wrong... – Fabio TK Feb 10 '21 at 15:42
  • I just solved it. The problem was in the sum of date + time by using the spreadsheet, but it was not updating when using the trigger onsubmit. Hence, I had to change the method to combine the date and time... Thank you all. – Fabio TK Feb 10 '21 at 17:44
  • Howdy! Congratulations on solving this issue. Could you please write an answer detailing your approach to the solution? – Jacques-Guzel Heron Feb 12 '21 at 09:52

1 Answers1

0

specially @Jacques-GuzelHeron and @RobG,
The problem was related to the fact that I was using the spreadsheet to combine (add) date (dd/MM/yyyy) and time (HH:mm:ss) from the form, and retrieving it to get the date object. But I was not aware that the spreadsheet is not updated on the run, and the script uses only the cache data...
So, I had to combine both parameters manually. I had not found a better way to do this, but be code below is working fine.

  Logger.log(Utilities.formatDate(new Date(), 'America/Sao_Paulo', 'dd/MM/yyyy HH:mm:ss'));

  // Start and end time (the procedure below was needed to combine date and time)
  var event_values = values[lastRow];
  // The commands in the next lines were required to combine date and time
  // event_values[7] corresponds to the date of the event provided by the form to the spreadsheet as dd/MM/yyyy
  var day = new Date(event_values[7].toLocaleString()).getDate().valueOf();
  var month=new Date(event_values[7].toLocaleString()).getMonth().valueOf();
  var year=new Date(event_values[7].toLocaleString()).getFullYear().valueOf();
  // event_values[8] and [9] correspond to the start and end time of the event as HH:mm:ss also provided by the form to the spreadsheet
  var start_time=event_values[8].toString().split(":");
  // since the day was not provided for the start_time and end_time variables, in my case the script assumed as 1899. So I split the string first with ":" and then with "1899 ".
  var start_hour=start_time[0].split("1899 ")[1].valueOf();
  var start_min=start_time[1].valueOf();
  var end_time=event_values[9].toString().split(":")
  var end_hour=end_time[0].split("1899 ")[1].valueOf();
  var end_min=end_time[1].valueOf();

  var event_start_time = new Date(year,month,day,start_hour,start_min,"00");
  var event_end_time=new Date(year,month,day,end_hour,end_min,"00");
Fabio TK
  • 11
  • 1