简体   繁体   English

表单输入类型日期时间返回字符串而不是日期

[英]Form input type date-time returns a string instead of a date

In this sheet ,这张表中

From the script editor we have an HTML form that submits dates and times into the sheet.在脚本编辑器中,我们有一个 HTML 表单,用于将日期和时间提交到工作表中。

The purpose is to check with the calendar app if there is any events among the submitted time range.目的是检查日历应用程序是否在提交的时间范围内有任何事件。

The problem is that the calendar app is reading the submitted dates and times as a string/text, instead of dates and times and returns an error问题是日历应用程序将提交的日期和时间读取为字符串/文本,而不是日期和时间并返回错误

Cannot find method getEvents(string,string)找不到方法 getEvents(string,string)

I manually modified the inputs from 2020-02-21T18:00 into 2020-02-21 18:00:00 , tested the code and it worked.我手动将输入从2020-02-21T18:00修改为2020-02-21 18:00:00 ,测试了代码并且它起作用了。 So, I guess that the problem is with the format of the date-time values submitted by the form所以,我猜问题在于表单提交的日期时间值的格式

The shared file is an editable version, feel free to edit and fix.共享文件是可编辑版本,您可以随意编辑和修复。 Thanks in advance :)提前致谢 :)

Google Apps Script Google Apps 脚本

function doPost (e) {
  Logger.log(JSON.stringify(e))

  if (!e || !e.parameter) {
    return;
  }

  var lock = LockService.getScriptLock();
  lock.tryLock(10 * 1000);

  try {

    // Form response sheet
    var doc = SpreadsheetApp.getActive();
    var sheet = doc.getSheetByName(sheetName);
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
    var nextRow = sheet.getLastRow() + 1;
    var newRow = headers.map(function(header) {
      return header === 'Timestamp' ? new Date() : e.parameter[header]
    });
    sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow]);

    // Get all events for the time range
    var startTime = newRow[1];
    var endTime = newRow[2];
    var cal = CalendarApp.getCalendarById("###");
    var allEvents = cal.getEvents(startTime, endTime);
    if (allEvents.length > 0) {
    return ContentService
    .createTextOutput(JSON.stringify('Not Available. Thanks'))
      .setMimeType(ContentService.MimeType.JSON)
    }else {
    return ContentService
    .createTextOutput(JSON.stringify('Available. Thanks'))
      .setMimeType(ContentService.MimeType.JSON)
    };

  }

  catch (e) {
    return ContentService
      .createTextOutput(JSON.stringify({ 'result': 'error', 'error': e }))
      .setMimeType(ContentService.MimeType.JSON)
  }

  finally {
    lock.releaseLock()
  }
}

HTML: HTML:

<!DOCTYPE html>
<html lang="en">
<body>

<form name="Class1" id="Class1" action="https://script.google.com/macros/s/###/exec" target="_blank" method="POST">

Start Date & Time
<input type="datetime-local" required name="Start Date & Time">

End Date & Time
<input type="datetime-local" required name="End Date & Time">

<button type="submit">Check</button>

</form>

</body>
</html>

How about this modification?这个改装怎么样? I think that in your script, the values of startTime and endTime are the string values of ISO8601 .我认为在您的脚本中, startTimeendTime的值是ISO8601的字符串值。 And at getEvents(startTime, endTime) , startTime and endTime are required to be the date object.getEvents(startTime, endTime)startTimeendTime必须是日期对象。 So I think that new Date() can be used.所以我认为可以使用new Date()

Modified script:修改后的脚本:

When your script is modified, please modify as follows.当您的脚本被修改时,请进行如下修改。

From: 从:
 var allEvents = cal.getEvents(startTime, endTime);
To: 到:
 var allEvents = cal.getEvents(new Date(startTime), new Date(endTime));

Note:笔记:

  • When you modified the script of Web Apps, please redeploy the Web Apps as new version.当您修改 Web Apps 的脚本时,请将 Web Apps 重新部署为新版本。 By this, the latest script is reflected.由此,反映了最新的脚本。 Please be of careful this.请注意这一点。

References:参考:

If I misunderstood your question, and this was not the direction you want, I apologize.如果我误解了您的问题,并且这不是您想要的方向,我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM