[英]Using a Date from HTML Input to Create Event in Google Apps Script
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. 1969年12月31日。我一直在搜寻并了解原因,但无法找出解决方法。
Here is the HTML for the sidebar input. 这是侧边栏输入的HTML。
<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. 日志告诉我正在获取以以下格式选择的日期:2017-10-21。 The Log also shows that it is creating an event and provides an event ID. 该日志还显示它正在创建事件并提供事件ID。
The issue is in the Execution Transcript. 问题在执行记录中。 This is what I am getting for the createEvent function: 这是我为createEvent函数得到的:
[17-10-21 18:48:10:447 EDT] Starting execution [17-10-21 18:48:10:447 EDT]开始执行
[17-10-21 18:48:10:454 EDT] Logger.log([2017-10-21, []]) [0 seconds] [17-10-21 18:48:10:454 EDT] Logger.log([2017-10-21,[]])[0秒]
[17-10-21 18:48:10:532 EDT] CalendarApp.getDefaultCalendar() [0.076 seconds] [17-10-21 18:48:10:532 EDT] CalendarApp.getDefaultCalendar()[0.076秒]
[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] Calendar.createEvent([titleP,1969年12月31日星期三16:00:00 PST 1969,12月31日星期三16:00:00 PST 1969])[0.506秒]
[17-10-21 18:48:11:039 EDT] CalendarEvent.getId() [0 seconds] [17-10-21 18:48:11:039 EDT] CalendarEvent.getId()[0秒]
[17-10-21 18:48:11:040 EDT] Logger.log([Event ID: 2017-10-21 odj9j3feheksjnji5h1qf89hns@google.com, []]) [0.001 seconds] [17-10-21 18:48:11:040 EDT] Logger.log([事件ID:2017-10-21 odj9j3feheksjnji5h1qf89hns@google.com,[]])[0.001秒]
[17-10-21 18:48:11:042 EDT] Execution succeeded [0.589 seconds total runtime] [17-10-21 18:48:11:042 EDT]执行成功[总运行时间0.589秒]
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. 我承认自己是Calendar的新手,已经做了大量的搜索和阅读工作,但现在已经设法使自己陷入圈子。
Thanks. 谢谢。
How about a following modification? 接下来的修改如何?
google.script.run.sidebarOutputB(document.getElementById('date').value)
is 2017-10-21
. google.script.run.sidebarOutputB(document.getElementById('date').value)
是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. 当使用javascript将字符串日期转换为日期对象时,如果仅是日期, 2017-10-21
可以执行2017-10-21
。 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
. 但是,如果那是2017-10-21 09:00:00
,则字符串日期的格式必须为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 . 关于时区, createEvent()
的文档在此处 。 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. 那时,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.