9

I'm looking for a programmatic way to automate the generation of pre-filled URLs for google forms.

In a recent update, Google introduced a new Forms product to Google Docs. If you open the tools menu in a spreadsheet, you'll see some new options.

Tools menu with Legacy Form

In the new Form Responses menu, one option is "Get pre-filled URL". This opens up a dialog containing a version of your form that you can fill out, and when you submit it, you receive a URL that can be used to open a Live Form with the data you pre-filled ready and waiting for you. The URL looks something like this...

https://docs.google.com/forms/d/--Form-ID--/viewform?entry.1094330118=Something&entry.1471717973=stack@example.com&entry.540962741&entry.787941281&entry.1873343651

The questions from the form have a fixed identity, e.g. entry.1094330118. They may be pre-filled with a value (entry.1094330118=Something) or blank (entry.7879412).

In apps-script, I'd like to generate these pre-filled URLs for users of my form, so I can provide them for updates. My users are not members of an Apps Domain, so I don't have the option of embedding an Edit your response link.

If I can get the information about the form, I will be able to piece together the URL. While I can go through the UI to create one URL, and dissect it to get the info for that form, I want a solution that will work with arbitrary forms.

  1. How can I programmatically determine the question IDs?
  2. With the new Forms product, is the form available to me through any apps-script APIs? (I know about getFormURL() - that's not what I mean.)
  3. Armed with a question ID, can I get more information about the question? (Question text, type, etc.)
Rubén
  • 34,714
  • 9
  • 70
  • 166
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • 1
    See [this answer](http://stackoverflow.com/questions/20108511/is-it-possible-to-prefill-a-google-form-using-data-from-a-google-spreadsheet/20110656#20110656) for a couple of ways to do this. – Mogsdad Nov 22 '13 at 18:57

4 Answers4

3

I required something similar for users to go and edit their response, take a look here: http://productforums.google.com/forum/#!topic/docs/LSKKCR3VHC8

Copy / paste code below:

function assignEditUrls() {
var form = FormApp.openById('1MonO-uooYhARHsr0xxxxxxxxxxxxxxxxxxxxx');
//enter form ID here

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses');

    //Change the sheet name as appropriate
  var data = sheet.getDataRange().getValues();
  var urlCol = 4; // column number where URL's should be populated; A = 1, B = 2 etc
  var responses = form.getResponses();
  var timestamps = [], urls = [], resultUrls = [];

  for (var i = 0; i < responses.length; i++) {
    timestamps.push(responses[i].getTimestamp().setMilliseconds(0));
    urls.push(responses[i].getEditResponseUrl());
  }
  for (var j = 1; j < data.length; j++) {

    resultUrls.push([data[j][0]?urls[timestamps.indexOf(data[j][0].setMilliseconds(0))]:'']);
  }
  sheet.getRange(2, urlCol, resultUrls.length).setValues(resultUrls);  
}
MPiets
  • 66
  • 1
  • 5
  • Thanks for the example, and for pointing out Adam's code on the Google Docs forum. That will help others, certainly. – Mogsdad Aug 27 '13 at 01:16
1

This is not possible right now but this request is being tracked on the Issue Tracker here. Please add your use cases there and watch it for updates.

Arun Nagarajan
  • 5,547
  • 1
  • 22
  • 19
  • Thanks, I've added my vte, and linked back here. – Mogsdad Feb 07 '13 at 00:06
  • given that the issue ticket is for general Forms Api access/documentation which is marked fixed would it not be better for this use case to be entered as a new issue ticket? – mhawksey Jun 02 '13 at 21:16
  • 4
    mhawksey - with the new API it is possible to get prefilled URL. You create a "FormResponse" object and get its prefilled URL. See the documentation here - https://developers.google.com/apps-script/reference/forms/form-response#toPrefilledUrl() – Arun Nagarajan Jun 03 '13 at 02:17
  • @Arun Is it possible to make an HTTP call and get values from there instead of spreadsheet and fill up the form ? – Kartik Domadiya Jul 13 '15 at 07:17
0

I found no way to create a pre-filled URL from the form id itself. It is possible if the form has already an answer:

  var form = FormApp.openById('1nGvvEzQHN1n-----_your_for_id_----zemoiYQA');
  var responses = form.getResponses();
  Logger.log(responses[0].toPrefilledUrl());
Max Makhrov
  • 17,309
  • 5
  • 55
  • 81
0

referring to this answer, it can be used to create prefilled urls, by replacing the last line like this: instead of FormResponse.submit(); it will be FormResponse.toPrefilledUrl();

icarus
  • 85
  • 2
  • 13