Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...