Home > Blockchain >  How can I get back the URL of the current google forms response in google scripts?
How can I get back the URL of the current google forms response in google scripts?

Time:11-19

So I'm working on a discord webhook in google scripts that returns the questions and the answers for those questions of the form when someone submits it. I would like to get the hyperlink of that current or that time the latest response but as I read a tons of documentations I couldn't find any function that returns this link.

This is the code that works, but without that thing I mentioned above:

function onSubmit(e) {
    var form = FormApp.getActiveForm();
    var allResponses = form.getResponses();
    var latestResponse = allResponses[allResponses.length - 1];
    var response = latestResponse.getItemResponses();
    var answerarray = [];
    var questionarray = [];
 

    for (var i = 0; i < response.length; i  ) {
        var question = response[i].getItem().getTitle();
        var answer = response[i].getResponse();
        try {
            var parts = answer.match(/[\s\S]{1,1024}/g) || [];
        } catch (e) {
            var parts = answer;
        }

        if (answer == "") {
            continue;
        }
        for (var j = 0; j < parts.length; j  ) {
            answerarray.push(parts[j]);
            questionarray.push(question);

        }
    }

Could you help me out?

CodePudding user response:

Updated Based On Comments

If you're trying to get a URL to the link for a screen like this based on an individual response ID, that is not possible currently. Here is a list of all the properties and methods for responses.

However, there really shouldn't be a need to visit that page. You can access all of the information from that screen using google app scripts. If you really wanted a link to a user's response, you could generate a google sheet or document upon submission and then fill it in with whatever format you wanted (or make it mimic the pre-built one).

Original Answer

Since you tagged google sheets as part of your question, I will take a guess that you are looking for the URL of the Spreadsheet or perhaps the time of submission?

Spreadsheet URL

If your form is connected to a spreadsheet, then you can get the URL of your form variable by using the following code:

  //You could combine for one line, but broken out to illustrate
  var theId = aForm.getDestinationId();
  var theSpreadsheet = SpreadsheetApp.openById(theId);
  var theURL = theSpreadsheet.getFormUrl();

Time Of Latest Response

This can be acquired without the spreadsheet. If you use your same variable answer you should be good with:

 var theTime = answer.getTimestamp();
  • Related