列出所有FormResponses时,提交响应时FormResponse的id不同 [英] The id of a FormResponse when a response is submitted is different when you list all FormResponses

查看:94
本文介绍了列出所有FormResponses时,提交响应时FormResponse的id不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行由Google表单支持的错误跟踪,我认为我在Apps脚本中存在一个错误。



提交响应时,我的脚本 onFormSubmit 处理程序获取 FormResponse id(如下例所示):

 函数onFormSubmit(e){
log(ID(on submit):,e.response);


函数log(msg,response){
Logger.log(msg ++ response.getId()+; response is'+ response.getItemResponses ()[0] .getResponse()+');
}

另外,当我列出所有 FormResponse s,我得到了不同的ID,除了第一个,这是正确的。示例代码如下:

  function listResponsesId(){
FormApp.getActiveForm()。getResponses()。forEach (函数(v){
log(ID(on list):,v);
});


函数log(msg,response){
Logger.log(msg ++ response.getId()+; response is'+ response.getItemResponses ()[0] .getResponse()+');
}

我用这个示例应用程序运行了3个表单提交,结果如下: / p>

  ID(提交时):ChI2NzM1Mjg5OTY5NjY0MjA5MjEQzI768siy3sSOAQ;答案是'test 1'
ID(在列表中):ChI2NzM1Mjg5OTY5NjY0MjA5MjEQzI768siy3sSOAQ;答案是'test 1'

ID(提交时):ChMxOTczNzc5Nzk1MDI1MDkzMjMyEMyO-vLIst7EjgE;答案是'test 2'
ID(在列表中):ChMxOTczNzc5Nzk1MDI1MDkzMjMyEAA;回应是'测试2'

ID(提交时):ChMyNjk1ODgzNjgwMjk2NjM4NzAyEMyO-vLIst7EjgE;答案是'test 3'
ID(在列表中):ChMyNjk1ODgzNjgwMjk2NjM4NzAyEAA;答案是'test 3'

FormResponse.getId()应该不可靠?

解决方案

我现在面临同样的问题。 b
$ b

尽管我目前的实施方案在时间戳上有所回落 - 在不太可能(但仍然有可能)的事件中,两个响应在同一秒内提交,我的解决方案将失败。



我注意到的是,在上面的例子中,响应ID不同,它们仍然共享前29个字符。

这些ID的长度和复杂性,我愿意打赌,这些是所有提交给所有谷歌表格的回复中的全球性内容,并且找到两个回复共有这29个字符的可能性。相同的形式比两个受访者在同一时间提交表格的可能性要小得多。

因此,比较第一个p艺术的ID可能是成功的。

  if(idStoredOnFormSubmit.substr(0,29)=== idFetchedFromFormResponse.substr(0 ,29)){
runThisCode();
};

substr(0,29)从索引位置0开始的字符串的起始处需要29个字符。

I'm working in a bug tracking backed by a Google Form and I think I'm stuck with a bug in Apps Script.

When a response is submitted, my script onFormSubmit handler gets the FormResponse id (as in the sample below):

function onFormSubmit(e) {
  log("ID (on submit):", e.response);
}

function log(msg, response) {
  Logger.log(msg + " " + response.getId() + "; response is '" + response.getItemResponses()[0].getResponse() + "'");
}

Further, when I list all FormResponses, I get different IDs, except for the first one, that is right. A sample code is below:

function listResponsesId() {
  FormApp.getActiveForm().getResponses().forEach(function(v) {
    log("ID (on list):", v);
  });
} 

function log(msg, response) {
  Logger.log(msg + " " + response.getId() + "; response is '" + response.getItemResponses()[0].getResponse() + "'");
}

I ran 3 form submissions with this sample app, with the following results:

ID (on submit): ChI2NzM1Mjg5OTY5NjY0MjA5MjEQzI768siy3sSOAQ; response is 'test 1'
ID (on list): ChI2NzM1Mjg5OTY5NjY0MjA5MjEQzI768siy3sSOAQ; response is 'test 1'

ID (on submit): ChMxOTczNzc5Nzk1MDI1MDkzMjMyEMyO-vLIst7EjgE; response is 'test 2'
ID (on list): ChMxOTczNzc5Nzk1MDI1MDkzMjMyEAA; response is 'test 2'

ID (on submit): ChMyNjk1ODgzNjgwMjk2NjM4NzAyEMyO-vLIst7EjgE; response is 'test 3'
ID (on list): ChMyNjk1ODgzNjgwMjk2NjM4NzAyEAA; response is 'test 3'

The FormResponse.getId() shouldn't be reliable?

解决方案

I'm coming up against the same issue.

While my current implementation falls back on timestamps - in the unlikely (but still possible) event that two responses are submitted within the same second, my solution would fail.

Something I've noticed is that in your above examples, where the response IDs differ, they still share the first 29 characters.

Given the length and complexity of these IDs, I'd be willing to wager that these are global across all responses submitted to all google forms, and that the odds of finding two responses that share these 29 chars at the start of their string submitted to the same form are far less likely than having two respondents submitting forms at the same second.

Thus, a workaround comparing just the first part of the IDs could be successful.

if (idStoredOnFormSubmit.substr(0,29) === idFetchedFromFormResponse.substr(0,29)) {
   runThisCode();
};

substr(0,29) takes 29 characters from the beginning of a string starting at index position 0.

这篇关于列出所有FormResponses时,提交响应时FormResponse的id不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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