3
votes

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?

5
Please read the description. I am referring to Entry ID, not Item ID.igorpavlov

5 Answers

4
votes

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

2
votes

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
votes

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;
1
votes

This one worked for me.

1.Get the Pre-filled link(you can see this in the more option in the form edit window).

2.In the new form appeared, fill in all the tiles and then click "get link".

3.Paste the link in the new tab, the filled form will appear.

  1. Check the URL of this form you can see the entry ID of each tile there with the responses you filled.

or

5.Right click on the first tile with the response and then inspect.

6.Cmd+f for Mac ,or Ctrl+f for windows to search, search a response you typed you can see the element Id in the code.

0
votes

Great answer @contributorpw ... works great.

Youd only added a couple of the form types so i added most of the rest. At the time of writing this, I dont see an option for .asFileItem() in the docs, so I havent added that one... as well as a few of the other less common ones (e.g. like PAGE_BREAK, SECTION_HEADER, etc.)

Someone's welcome to add them ;-)

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;
    case FormApp.ItemType.CHECKBOX:
      return item.asCheckboxItem()
        .createResponse(item.asCheckboxItem().getChoices()[0].getValue());
      break;
    case FormApp.ItemType.DATETIME:
      return item.asDateTimeItem()
        .createResponse(new Date());
      break;
    case FormApp.ItemType.DATE:
      return item.asDateItem()
        .createResponse(new Date());
      break;
    case FormApp.ItemType.LIST:
      return item.asListItem()
        .createResponse(item.asListItem().getChoices()[0].getValue());
      break;
    case FormApp.ItemType.PARAGRAPH_TEXT:
      return item.asParagraphTextItem()
        .createResponse(item.asParagraphTextItem().createResponse("some paragraph"));
      break;
    case FormApp.ItemType.CHECKBOX_GRID:
      return item.asCheckboxGridItem()
        .createResponse(item.asCheckboxGridItem().createResponse([item.asGridItem().getColumns[0], item.asGridItem().getRows[0]]));
      break;
    case FormApp.ItemType.DURATION:
      return item.asDurationItem()
        .createResponse(item.asDurationItem().createResponse(2, 20, 20));
      break;
    case FormApp.ItemType.GRID:
      return item.asGridItem()
        .createResponse(item.asGridItem().createResponse([item.asGridItem().getColumns[0], item.asGridItem().getRows[0]]));
      break;
   case FormApp.ItemType.SCALE:
      return item.asScaleItem()
        .createResponse(item.asScaleItem().createResponse(1));
      break;
   case FormApp.ItemType.TIME:
      return item.asTimeItem()
        .createResponse(item.asTimeItem().createResponse(1, 1));
      break;
    default:
      return undefined; 
  } 
}