2

I have a Google Form With a Google spreadsheet to store responses. In my spread sheet I have 4 columns: name, email, revenue, and a fourth column, Id, which is used to identify the particular recipient.

What I am trying to accomplish is to generate a unique URL for each respondent so that they can respond to the form and use the same URL to edit the form at a later time.

I've looked at the getEditUrl() (Google Apps Script) method which creates a unique URL for the respondent after submitting the response-- code below:

function myFunction() {
  assignEditUrls();  
}

function assignEditUrls() {
  var form = FormApp.openById('1vsqvwomoqSXwF6TlNkmmktAAk2av2r-2LRrBYJdv3VQ');
    //enter form ID here

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

    //Change the sheet name as appropriate
  var data = sheet.getDataRange().getValues();
  var urlCol = 5; // 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++) { 
    var resp = responses[i];

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

    resultUrls.push([data[j][0]?urls[timestamps.indexOf(data[j][0].setMilliseconds(0))]: '']);
  }
  sheet.getRange(2, urlCol, resultUrls.length).setValues(resultUrls);  
}


function shortenUrl(longUrl) {
  // google url shortener api key
  var key = "AIzaSyBVG4Q5i1mNI0YAO0XVGZ3suZU8etTvK34";

  var serviceUrl="https://www.googleapis.com/urlshortener/v1/url?key="+key;

  var options={
    muteHttpExceptions:true,
    method:"post",
    contentType: "application/json",
    payload : JSON.stringify({'longUrl': longUrl })
  };

  var response=UrlFetchApp.fetch(serviceUrl, options);

  if(response.getResponseCode() == 200) {
    var content = JSON.parse(response.getContentText());
    if ( (content != null) && (content["id"] != null) )
      return content["id"];
  }

  return longUrl;
}

However I want to do it the other way which is to first generate the unique URL to be then sent to respondents so they can submit and edit their responses without the need of sending them another URL (e.g. the editurlresponse).

Is this possible to do?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
rararake
  • 43
  • 1
  • 5
  • Do you want to know who submitted the form? Have you looked at how the pre-filled url is constructed? You can construct a "pre-filled" url (a url that fills in some or all of the fields). For example, you could construct a pre-filled url, that filled in an item that was the name of the person submitting the form. – Alan Wells Nov 07 '15 at 22:35
  • not who responded. I want to build a unique url for each recipient so that they can use that url to submit then re-edit their response. – rararake Nov 07 '15 at 22:43
  • This question was cross-posted by another user to the Web Apps SE site: http://webapps.stackexchange.com/q/86396/88163 – Rubén Nov 09 '15 at 13:22
  • There is an error in the third paragraph. It says "getEditUrl" but this method return the URL to edit the form, not the one to edit a response. See [getEditUrl()](https://developers.google.com/apps-script/reference/forms/form#getediturl). – Rubén Nov 09 '15 at 19:41
  • I'm having this problem... But this code did'nt helped. – Raul Chiarella May 14 '22 at 21:45

1 Answers1

0

Originally posted to https://webapps.stackexchange.com/a/86399/88163


Yes, it's possible, but with slight different approach:

Submit one answer for each respondent in order to get one edit URL by each of them, then send the corresponding URL to each respondent.

Below is a code snippet that programmatically submits a response and log some response attributes including the edit response url by through two code lines, the first one has an issue, the second one is a workaround. Please note that these lines use getEditResponseUrl() not the toPrefilledUrl().

It will work as a stand alone or as a bounded script.

/*
This code shows how to get the edit response url of a 
programmatically submitted response to a Google Form
*/ 

// Replace the form ID by your own form
var formID = '1234567890abcdefghijklmnopqrstuvwxyz';

function myFunction() {
  var form = FormApp.openById(formID);
  var response = form.createResponse();
  var items = form.getItems();
  var item = items[0];
  if (item.getType() == 'TEXT') {
    var textItem = item.asTextItem();
    var itemResponse = textItem.createResponse('my text');
    response.withItemResponse(itemResponse);
  } 
  // Submit response
  var submittedResponse = response.submit();
  // Get submitted response attributes
  var values = {
    ID : submittedResponse.getId(),
    TS : submittedResponse.getTimestamp(),
    /* 
    Issue 4476: FormApp: getEditResponseUrl() produces invalid URL 
    https://code.google.com/p/google-apps-script-issues/issues/detail?id=4476
    */
    ER1 : submittedResponse.getEditResponseUrl(),
    /* Workaround from 
    https://code.google.com/p/google-apps-script-issues/issues/detail?id=4476#c2
    */
    ER2 : submittedResponse.getEditResponseUrl().replace(
      /\?edit2=.*/,"?edit2=" + submittedResponse.getId()
    ) 
  };
  Logger.log(values);
}

References

Community
  • 1
  • 1
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • This does not do what the OP requested; while there will be a unique URL for them, it would need to be regenerated any time they submitted the form. – Mogsdad Nov 09 '15 at 16:22
  • @Mogsdad : I think that you confused the getEditResponseUrl() with toPrefilledUrl(). The code use the first one. – Rubén Nov 09 '15 at 19:45
  • You're almost right, I did a different wrong-headed thing - I misremembered how getEditResponseUrl() worked, thinking it tracked the specific values of the response. – Mogsdad Nov 09 '15 at 21:15
  • @Mogsdad Thanks for coming back. Any tip/suggestions to on how to improve this answer? – Rubén Nov 09 '15 at 21:46
  • It's a pretty good answer, really. The only things I can suggest is to change the tone from "no, it's not possible" to "yes, but indirectly, here's how", and add a brief summary of what getEditResponseUrl() is _supposed_ to do. The details of having to work around a long-time bug are important for implementing working code, but not for understanding the basic thought process in the solution, so presenting the workaround as a secondary issue could help readers "get it". – Mogsdad Nov 10 '15 at 01:49
  • 1
    Thanks for your advice. I very rarely start a conversation with a "No", but somewhere I read that it's encouraged to answer with a "No..." followed by an "instead do..." in similar cases :) – Rubén Nov 10 '15 at 02:00
  • Hello. What does ER1 and ER2 means? – Raul Chiarella May 14 '22 at 21:09
  • @RaulChiarella They are property names. They hold two values derived from the edit response URL. – Rubén May 14 '22 at 21:10