10

I'm trying to make the edit URL for a Google Form automatically populate in the response Google Sheet that it's attached to. I've seen this already and want to use it, but I'm having issues trying to figure out where exactly to put the script.

I've tried putting it in the script editor in the Google Spreadsheet that I would like the URL to appear in, but I'm not sure where to go from there. In the script editor I've tried to test it as an add-on but that didn't end up working.

I have little experience with scripts and the script editor in Google Sheets. As a final note I am using one other add-on called AutoCrat in the same Google Spreadsheet.

  • Besides the link to the source of the code that you tried it good to include the code and the steps followed to do your tests. – Rubén Feb 5 '16 at 10:40
15

Short answer

The link points to a Q&A that has two answers but non-of them looks to me to be appropiate. I.E. the code of one of answers is incomplete and both of them send emails, so the adoption is too complex to be used as a starting point.

Below is a script that is easier to adopt.

Instructions

  1. Create a test form and note the form ID (between ../d/ and /edit in the url). New form

  2. Set the form to send responses to a new spreadsheet. Connect a spreadsheet

  3. You'll be redirected to the new spreadsheet. Note the active sheet name (rename it if you want) and add a header to the column to be used to hold the response edit URLs, i.e. Edit Url. NOTE: Capitalization is very important, so be very careful on how do you write it. Responses spreadsheet

  4. Go to Tools > Script editor to add a Google Apps Script project bounded to the spreadsheet. Spreadsheet scripts

  5. Replace the default code with the code below (remember to edit the global variables according to your case). Script code

  6. Save the project, and then add an installable form summit trigger (if you get an "Authorization required" dialog, click "Review Permissions" and then "Allow"). Form submit trigger

  7. Submit some sample responses to test the solution.

Code

/*
 * Global Variables
 */

// Form URL
var formURL = 'https://docs.google.com/forms/d/form-id/viewform';
// Sheet name used as destination of the form responses
var sheetName = 'Form Responses 1';
/*
 * Name of the column to be used to hold the response edit URLs 
 * It should match exactly the header of the related column, 
 * otherwise it will do nothing.
 */
var columnName = 'Edit Url' ;
// Responses starting row
var startRow = 2;

function getEditResponseUrls(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues(); 
  var columnIndex = headers[0].indexOf(columnName);
  var data = sheet.getDataRange().getValues();
  var form = FormApp.openByUrl(formURL);
  for(var i = startRow-1; i < data.length; i++) {
    if(data[i][0] != '' && data[i][columnIndex] == '') {
      var timestamp = data[i][0];
      var formSubmitted = form.getResponses(timestamp);
      if(formSubmitted.length < 1) continue;
      var editResponseUrl = formSubmitted[0].getEditResponseUrl();
      sheet.getRange(i+1, columnIndex+1).setValue(editResponseUrl);
    }
  }
}

Additional resources

I created a gist with the content from the question and answer. this b/c I'm exploring ways to better collaborate between end-users who write code.

  • 1
    I got it, it was the sheet name, and I took out '/viewform' at the end of the URL. Thanks so much! – nicks Feb 16 '16 at 21:33
  • 1
    @Rubén - I took the liberty of adding a little more detail to your great tutorial, as I struggled with a few of the steps when I followed it myself. – Sphinxxx Feb 17 '17 at 23:14
  • I "accepted" the changes to the instructions but not to the code but i created a gist. I'm wondering if this could help us to better communicate regarding code changes suggestions / code variants. – Rubén Feb 18 '17 at 4:15
  • 1
    Ok. The main reason I changed the code was to show that you can use FormApp.openById() with only the form ID instead of FormApp.openByUrl() with the whole URL, in case Google's URL format changes in the future. I'll leave a comment on your gist instead. – Sphinxxx Feb 18 '17 at 12:58
  • 2
    In my google script window, the "Current project's triggers" was under "Edit". – dvb Apr 6 '17 at 10:57

protected by Community May 7 '17 at 3:45

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.