Reputation: 17
I am trying to create an Add-on that will create a series of events based on the date entered into the sidebar by the user. Essentially, the script will create a number of different events on the day that the user wants.
I just cannot seem to get the date to process correctly and it is creating events on Wed. Dec. 31 1969. I have been searching and understand why but cannot figure out how to fix it.
Here is the HTML for the sidebar input.
<input type="date" id="date" width="50px"/>
<input type="button" value="Run Script"
onclick="google.script.run.sidebarOutputB(
document.getElementById('date').value
)"; /><br>
<br>
Here is the function. This is the simple form. I intend on using different start and end times for all the events, but want to use the date from selected by the user.
function betaCreateB(dateP) {
var date = dateP;
var startTime = "09:00:00";
var endTime = "09:30:00";
var title = "titleP";
var getCal = CalendarApp.getDefaultCalendar();
var event = getCal.createEvent(title,
new Date(date + ' ' + startTime),
new Date(date + ' ' + endTime));
Logger.log('Event ID: ' + date + ' ' + event.getId());
}
The log tells me that is getting the date that was selected in the following format: 2017-10-21. The Log also shows that it is creating an event and provides an event ID.
The issue is in the Execution Transcript. This is what I am getting for the createEvent function:
[17-10-21 18:48:10:447 EDT] Starting execution
[17-10-21 18:48:10:454 EDT] Logger.log([2017-10-21, []]) [0 seconds]
[17-10-21 18:48:10:532 EDT] CalendarApp.getDefaultCalendar() [0.076 seconds]
[17-10-21 18:48:11:039 EDT] Calendar.createEvent([titleP, Wed Dec 31 16:00:00 PST 1969, Wed Dec 31 16:00:00 PST 1969]) [0.506 seconds]
[17-10-21 18:48:11:039 EDT] CalendarEvent.getId() [0 seconds]
[17-10-21 18:48:11:040 EDT] Logger.log([Event ID: 2017-10-21 [email protected], []]) [0.001 seconds]
[17-10-21 18:48:11:042 EDT] Execution succeeded [0.589 seconds total runtime]
Any help on a fix for this is appreciated. I admit to being new to Calendar and have done a lot of searching and reading but have managed to spin myself in circles at this point.
Thanks.
Upvotes: 0
Views: 660
Reputation: 201438
How about a following modification?
google.script.run.sidebarOutputB(document.getElementById('date').value)
is 2017-10-21
. When a string date is converted to a date object using javascript, if that is only date, 2017-10-21
can be done. But if that is 2017-10-21 09:00:00
, the format of string date is required to be 2017/10/21 09:00:00
.function betaCreateB(dateP) {
var date = dateP.replace(/-/g, "/"); // Modified
var startTime = "09:00:00";
var endTime = "09:30:00";
var title = "titleP";
var getCal = CalendarApp.getDefaultCalendar();
var event = getCal.createEvent(title,
new Date(date + ' ' + startTime),
new Date(date + ' ' + endTime));
Logger.log('Event ID: ' + date + ' ' + event.getId());
}
About the timezone, the document of createEvent()
is here. It says as follows.
If no time zone is specified, the time values are interpreted in the context of the script's time zone, which may be different than the calendar's time zone.
But if the time is not what you want, please compensat it.
If I misunderstand your question, I'm sorry. At that time, please tell me.
Upvotes: 1