0
votes

I want to add the Edit Response URL of a Google Form to its spreadsheet and tried the code from the link below, but nothing happens. Any idea what's the problem?

https://ctrlq.org/code/20540-edit-form-response-spreadsheet-url

/*
* Written by Amit Agarwal
* Web: digitalinspiration.com
* Email: [email protected]
* MIT License 
*/

// Create the Form Submit Trigger
function createFormTrigger() {
  var triggerName = "addFormResponseUrl";
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  ScriptApp.newTrigger(triggerName)
    .forSpreadsheet(spreadsheet)
    .onFormSubmit()
    .create();
}

function addFormResponseUrl(e) {

  // Get the Google Form linked to the response
  var responseSheet = e.range.getSheet();
  var googleFormUrl = responseSheet.getFormUrl();
  var googleForm = FormApp.openByUrl(googleFormUrl);

  // Get the form response based on the timestamp
  var timestamp = new Date(e.namedValues.Timestamp[0]);
  var formResponse = googleForm.getResponses(timestamp).pop();

  // Get the Form response URL and add it to the Google Spreadsheet
  var responseUrl = formResponse.getEditResponseUrl();
  var row = e.range.getRow();
  var responseColumn = 10; // Column where the response URL is recorded.
  responseSheet.getRange(row, responseColumn).setValue(responseUrl);
}
2
Did you create the trigger? Did you update the trigger name for your function. Does your function have an event parameter?Cooper

2 Answers

0
votes

Done all the above, but something still not there. Solved it with this workaround for now, triggering all of the responses on every form submission -

function responseURL() {
  var form = FormApp.openById('PUzzMB0ElYOtqGIk6ir1sBrDhowmU8o-afiDqiET');
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('formData');
  var formResponses = form.getResponses();
  for (var i = 0; i < formResponses.length; i++) {
    var formResponse = formResponses[i];
    sheet.getRange(i+2, 4).setValue(formResponse.getEditResponseUrl());
  }
}
0
votes

Elegant way to resolve the editResponseUrl in the response sheet Worked for me after setting the function as a submit trigger of the form in the script editor.

https://gist.github.com/ooobo/a3d60d9d2c4a9fdf5f72

// install as a google script linked to the form, not the spreadsheet. must add a trigger to run assignEditUrls() on form submit.
// benefit of linking to the form is that copying the spreadsheet will copy the spreadsheet, form and script.
function assignEditUrls() {
  var form = FormApp.getActiveForm();
  var ss = SpreadsheetApp.openById(form.getDestinationId());
  var sheet = ss.getSheets()[0];

  var data = sheet.getDataRange().getValues();
  var urlCol = 2; // 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('=HYPERLINK("' + responses[i].getEditResponseUrl() + '", "edit")');
  }
  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);  
}