get Entry ID which is used to pre-populate fields (Items) in a Google Form URL

送分小仙女□ 提交于 2020-06-23 03:07:35

问题


NOTE: This question is not about finding ID of an Item (form element), but an ID of Entry. These are different things. Entry ID is a number which is used to pre-populate fields (Items) in a form URL.

As described here https://developers.google.com/apps-script/reference/forms/text-item it is possible to get a TextItem ID (actually, any Item ID) via getID() method.

If you open any public Google Form HTML source code you will see something like this in the var FB_PUBLIC_LOAD_DATA:

[232495719,"Question 1",null,0,[[1492883199]]
 ^                               ^
 Item ID                         Entry ID

I don't see a method to get Entry IDs. Is it actually possible to do via Google Apps Script API?


回答1:


There are two ways to get the entry IDs

  1. From the pre-filled URL.
    • For this, you have two options:
      1. get the prefilled URL from the Form editor or
      2. get the prefilled URL by using toPrefilledUrl() Google Apps Script method.
  2. From the form response view.
    • For this, you should go to the source code of the form. If your form use sections, this could be tricky because it's possible that will not be any page showing all the entries IDs.



回答2:


I'd like to extend the @devonuto 's idea

The code below should return an array of Form fields with their properties including entry

function getPreFillEntriesMap_(id){
  var form = FormApp.openById(id);
  var items = form.getItems();
  var newFormResponse = form.createResponse();
  var itms = [];
  for(var i = 0; i < items.length; i++){
    var response = getDefaultItemResponse_(items[i]);
    if(response){
      newFormResponse.withItemResponse(response);
      itms.push({
        id: items[i].getId(),
        entry: null,
        titile: items[i].getTitle(),
        type: "" + items[i].getType()
      });
    }
  }

  var ens = newFormResponse.toPrefilledUrl().split("&entry.").map(function(s){
    return s.split("=")[0];
  });
  ens.shift();

  return itms.map(function(r, i){
    r.entry = this[i];
    return r;
  }, ens);
}

function getDefaultItemResponse_(item){
  switch(item.getType()){
    case FormApp.ItemType.TEXT:
      return item.asTextItem().createResponse("1");
      break;
    case FormApp.ItemType.MULTIPLE_CHOICE:
      return item.asMultipleChoiceItem()
        .createResponse(item.asMultipleChoiceItem().getChoices()[0].getValue());
      break;
    default:
      return undefined; 
  } 
}

You have to extend getDefaultItemResponse_() yourself to support more types of items.

This works fine for me

function run(){
  Logger.log(JSON.stringify(
    getPreFillEntriesMap_("1X9MBJDZYwrij8m_c7V4HbrT1cOyBu2OLRvDsnqS4Vdw"),
    null,
    "  "
  ));
}
[18-07-07 17:22:46:288 MSK] [
  {
    "id": 1144844846,
    "entry": "1854759972",
    "titile": "Q1",
    "type": "TEXT"
  },
  {
    "id": 1458564606,
    "entry": "1109661125",
    "titile": "Q2",
    "type": "TEXT"
  },
  {
    "id": 216942465,
    "entry": "1829112829",
    "titile": "Q3",
    "type": "MULTIPLE_CHOICE"
  }
]

The snippet get_pre_fill_entries_map




回答3:


Realise this is an old thread, but I have been doing something similar, where I needed the IDs to create pre-fill Hyperlinks in a C# application.

You can create a response to do this which doesn't need to be submitted to get the information.

var form = FormApp.create("TEST FORM");
var fieldCount = 0;
var fields = [];
var fieldIds = [];
var field1 = form.addTextItem();
var title = "Field 1";
fields[fieldCount++] = title;
field1.setTitle(title);
var field2 = form.addTextItem();
title = "Field 2";
fields[fieldCount++] = title;
field2.setTitle(title);
var response = form.createResponse();
response.withItemResponse(field1.createResponse("TEST FIELD 1"));
response.withItemResponse(field2.createResponse("TEST FIELD 2"));
var url = response.toPrefilledUrl();
var split = url.split("entry.");
for (var x = 1; x < split.length; x++) {
    var split2 = split[x].split("=");
    fieldIds[x - 1] = split2[0];
}
for (var y = 0; y < fieldIds.length; y++) {
    Logger.Log("Field Name: " + fields[y] + "Field ID: " + fieldIds[y]);
}
delete response;


来源:https://stackoverflow.com/questions/46017170/get-entry-id-which-is-used-to-pre-populate-fields-items-in-a-google-form-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!