Google Form on Submit会获取值并设置时间格式 [英] Google Form on Submit get values and format the time

查看:193
本文介绍了Google Form on Submit会获取值并设置时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google表单使用Google Apps脚本。当用户提交Google表单时,我会从问题中获得一个值。然后,我将这个值作为日期对象,从我在这篇文章中看到的内容夏令时我使用它来确定时区。我通过Utilities.formatDate运行日期对象,并希望得到格式正确的日期。



例如:9:00 AM
$ b $但是我却得到了与预期完全不同的时间。



我的问题是:有人可以帮我理解为什么下面的代码输出的时间是3小时不同?



 函数onSubmit(e){

var values = e.values;
Logger.log(values);

尝试{
var start1 = new Date(values [3]);
var startN = new Date(start1).toString()。substr(25,6)+00;
var startT = Utilities.formatDate(start1,startN,h:mm a);
Logger.log(startT);

} catch(error){
Logger.log(error);
}
}


解决方案

假设实用程序formatDate不支持GMT ...参数是不正确的。



您在引用中提到的帖子用于获取日历事件,并且是获取日历事件的有用方法当你从另一个夏令时获得事件时获得正确的值(从日历事件本身获取TZ信息),例如下个月的事件将在夏令时,而我们仍处于冬令时...



您的问题可能来自不同的来源,具体取决于脚本的时区设置与来源的时区。你能描述一下你使用这个脚本的确切配置吗?

同时,这里有一个小代码演示代码如何工作+记录器结果:

  function testOnSubmit(){
var eventInfo = {};
var values = {};
values ['3'] = new Date();
eventInfo ['values'] =值;
Logger.log('eventInfo ='+ JSON.stringify(eventInfo)+'\\\
\\\
');
onSubmit(eventInfo);
}

函数onSubmit(e){
var values = e.values;
尝试{
var start1 = new Date(values [3]);
Logger.log('onSubmit log results:\\\
');
Logger.log('start1 ='+ start1)
var startN = new Date(start1).toString()。substr(25,6)+00;
Logger.log('startN ='+ startN);
var startT = Utilities.formatDate(start1,startN,h:mm a);
Logger.log('result in timeZone ='+ startT);
} catch(error){
Logger.log(error);
}
}



编辑:另外,大约30和45 'offset,这可以很容易地通过像这样改变子串长度来解决:

  var startN = new Date(start1).toString ().substr(25,8); 

结果是一样的,几年前我不得不使用另一个版本,因为Google改变了在某个时刻的Utilities.formatDate方法(问题编辑2:在同一主题上,两种方法实际上都会返回相同的结果, GMT字符串的优点是您不必知道确切的时区名称......还有 Session.getScriptTimeZone()方法。下面是一个演示脚本,显示1月和7月2日的resulst以及日志结果:

 函数testOnSubmit() {
var eventInfo = {};
var values = {};
values ['3'] =新日期(2014,0,1,8,0,0,0);
eventInfo ['values'] =值;
Logger.log('eventInfo ='+ JSON.stringify(eventInfo)+'\\\
\\\
');
onSubmit(eventInfo);
values ['3'] =新日期(2014,6,1,8,0,0,0);
eventInfo ['values'] =值;
Logger.log('eventInfo ='+ JSON.stringify(eventInfo)+'\\\
');
onSubmit(eventInfo);
}

函数onSubmit(e){
var values = e.values;
var start1 = new Date(values [3]);
Logger.log('onSubmit log results:');
Logger.log('start1 ='+ start1)
var startN = new Date(start1).toString()。substr(25,8);
Logger.log('startN ='+ startN);
Logger.log('timezone using GMT string result ='+ Utilities.formatDate(start1,startN,MMM,d h:mm a));
Logger.log('使用Joda.org得到timeZone字符串='+ Utilities.formatDate(start1,'Europe / Brussels',MMM,d h:mm a));
Logger.log('result in timeZone using Session.getScriptTimeZone()='+ Utilities.formatDate(start1,Session.getScriptTimeZone(),MMM,d h:mm a)+'\\\
');
}


请注意记录器有自己的方式来显示日期对象价值!它使用ISO 8601时间格式,即UTC值。

I am using Google Apps Script with a Google form. When the user submits the Google Form I get a value from a question. I then take that value and make it a date object, from what I saw on this post about daylight savings I use that to determine the timezone. I run the date object through Utilities.formatDate and want to get the correctly formatted date.

example: 9:00 AM

But instead I am getting a completely different time than expected.

My question is: Can someone help me understand why the below code is outputting a time that is 3 hours different?

function onSubmit(e) {

  var values = e.values;  
  Logger.log(values);

  try {    
    var start1 = new Date(values[3]);
    var startN = new Date(start1).toString().substr(25,6)+"00";
    var startT = Utilities.formatDate(start1, startN, "h:mm a");
    Logger.log(startT);

  } catch(error) {
    Logger.log(error);
  }
}

解决方案

The assumption that Utilities formatDate does not support GMT... parameter is not true.

The post you mentioned in reference is used to get calendar events and is a useful way to get the right value when you get events from another daylight saving period (getting the TZ info from the calendar event itself), for example events for next month will be in "summer time" while we are still in "winter time"...

Your issue might come from different sources depending on time zone settings of your script vs timezone of the source. Could you describe the exact configuration in which you use this script ?

In the mean time, here is a small code that demonstrates how the code is working + the logger results :

function testOnSubmit() {
  var eventInfo = {};
  var values = {};  
  values['3'] = new Date();
  eventInfo['values'] = values;
  Logger.log('eventInfo = '+JSON.stringify(eventInfo)+'\n\n');
  onSubmit(eventInfo);
}

function onSubmit(e) {
  var values = e.values;  
  try {    
    var start1 = new Date(values[3]);
    Logger.log('onSubmit log results : \n');
    Logger.log('start1 = '+start1)
    var startN = new Date(start1).toString().substr(25,6)+"00";
    Logger.log('startN = '+startN);
    var startT = Utilities.formatDate(start1, startN, "h:mm a");
    Logger.log('result in timeZone = '+startT);
  } catch(error) {
    Logger.log(error);
  }
}

EDIT : additionally, about the 30 and 45' offset, this can easily be solved by changing the substring length like this :

var startN = new Date(start1).toString().substr(25,8);

the result is the same, I had to use the other version a couple of years ago because Google changed the Utilities.formatDate method at some moment (issue 2204) but this has been fixed.

EDIT 2 : on the same subject, both methods actually return the same result, the GMT string has only the advantage that you don't have to know the exact timezone name... there is also the Session.getScriptTimeZone() method. Below is a demo script that shows the resulst for 2 dates in January and July along with the log results :

function testOnSubmit() {
  var eventInfo = {};
  var values = {};  
  values['3'] = new Date(2014,0,1,8,0,0,0);
  eventInfo['values'] = values;
  Logger.log('eventInfo = '+JSON.stringify(eventInfo)+'\n\n');
  onSubmit(eventInfo);
  values['3'] = new Date(2014,6,1,8,0,0,0);
  eventInfo['values'] = values;
  Logger.log('eventInfo = '+JSON.stringify(eventInfo)+'\n');
  onSubmit(eventInfo);
}

function onSubmit(e) {
  var values = e.values;  
  var start1 = new Date(values[3]);
  Logger.log('onSubmit log results : ');
  Logger.log('start1 = '+start1)
  var startN = new Date(start1).toString().substr(25,8);
  Logger.log('startN = '+startN);
  Logger.log('result in timeZone using GMT string = '+Utilities.formatDate(start1, startN, "MMM,d  h:mm a"));
  Logger.log('result in timeZone using Joda.org string = '+Utilities.formatDate(start1, 'Europe/Brussels', "MMM,d  h:mm a"));
  Logger.log('result in timeZone using Session.getScriptTimeZone() = '+Utilities.formatDate(start1, Session.getScriptTimeZone(), "MMM,d  h:mm a")+'\n');
}

Note also that the Logger has its own way to show the date object value ! it uses ISO 8601 time format which is UTC value.

这篇关于Google Form on Submit会获取值并设置时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆