The id of a FormResponse when a response is submit

2019-01-27 01:00发布

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?

2条回答
Bombasti
2楼-- · 2019-01-27 01:28

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.

查看更多
beautiful°
3楼-- · 2019-01-27 01:33

In case others run into this, I wanted to post how I got around this behavior.

I discovered this same behavior while trying to use my FormResponse ID in an onFormSubmit trigger. I wanted to map the FormResponse ID to a value returned by another API when users were allowed to edit responses. So, in order to get passed the changing FormResponse ID I used the FormResponse ID from onFormSubmit response to get the saved FormResponse and mapped the ID of the saved FormResponse, as seen in the code below.

if (form.canEditResponse()) {
  var savedResponse = form.getResponse(response.getId());
}
查看更多
登录 后发表回答