简体   繁体   English

使用HTML输入中的日期在Google Apps脚本中创建事件

[英]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? 接下来的修改如何?

Modification points : 修改要点:

  • The value from 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

Modified script : 修改后的脚本:

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

Note : 注意 :

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.

相关问题 从Google Apps脚本创建HTML页面 - create html page from google apps script 使用html5时间和日期输入的Google Apps脚本创建日历事件? - Creating a calendar event with Google Apps Script from html5 time and date inputs? 使用日期选择器输入Google Apps脚本的电子表格日期 - Google Apps Script for Spreadsheet date input using date picker Google Apps脚本禁止从工作表创建日历事件吗? - Google Apps Script create calendar event from sheet forbidden? Google Apps 脚本 - 从电子表格创建日历事件 - Google Apps Script - Create Calendar Event from Spreadsheet 使用 Apps 脚本将 HTML 表(用户输入)更新到 Google 表格 - Update HTML table (user input) to Google Sheets using Apps Script 使用 html 对话框中输入字段的值使用 Google Apps 脚本在 code.gs 中进行 var - use value from input field in html dialog box to var in code.gs using Google Apps Script 使用Google表格中的输入来换行(Google Apps脚本) - Wrap text using input from Google Sheet (Google Apps Script) 使用 Google 表格中的数组在 html 中创建一个数据列表,该列表可提供给 Google Apps 脚本以自动完成文本输入 - use an array from Google Sheets to create a data list in html that can be served to a Google Apps Script for Autocomplete a Text Input 使用Google Apps脚本从HTML页面上的JSON数据创建下拉列表 - Create dropdown from JSON data on HTML page using Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM