0

I had set up a code previously but it was deleted by someone on our team but now I can't seem to get it working. I need the google forms to automatically add the link to edit the form to the google sheet when a form is submitted.

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 = 241; // Column where the response URL is recorded.
  responseSheet.getRange(row, responseColumn).setValue(responseUrl);
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
Benson
  • 1
  • 1
  • Welcome to [Stack Overflow](https://stackoverflow.com/tour). Please give the exact error message you see in [My Executions](https://script.google.com/home/executions). Please do not paraphrase errors but quote verbatim the exact error message you received. – doubleunary Jan 04 '23 at 17:23
  • 1
    If your trigger was for a Spreadsheet then e.range.getSheet() is the response sheet and e.values and e.namedValues are the responses. – Cooper Jan 04 '23 at 18:52

0 Answers0