How to generate a pre-filled form URL for Google Form

11,514

Solution 1

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);  
}

Solution 2

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.

Share:
11,514
Mogsdad
Author by

Mogsdad

By day, I'm a Senior Engineering Manager for GetFeedback at Momentive.ai, focused on Customer Experience Management products. I'm a full-stack engineer working mainly with ruby-on-rails backend and reactjs/typescript frontend development. On SO, I'm most active in curation. google-apps-script and other Google Apps related tags, but also contribute to javascript, html, and the like.

Updated on July 01, 2022

Comments

  • Mogsdad
    Mogsdad almost 2 years

    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&[email protected]&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.)
  • Mogsdad
    Mogsdad over 11 years
    Thanks, I've added my vte, and linked back here.
  • mhawksey
    mhawksey almost 11 years
    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?
  • Arun Nagarajan
    Arun Nagarajan almost 11 years
    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 - developers.google.com/apps-script/reference/forms/…
  • Mogsdad
    Mogsdad over 10 years
    Thanks for the example, and for pointing out Adam's code on the Google Docs forum. That will help others, certainly.
  • Kartik Domadiya
    Kartik Domadiya almost 9 years
    @Arun Is it possible to make an HTTP call and get values from there instead of spreadsheet and fill up the form ?