How to add “Edit Response” link to Google Forms emails?

半城伤御伤魂 提交于 2019-11-27 19:03:48

The answer that this wasn't possible by @Henrique Abreu was true until very recently. Google seems to have added getEditResponseUrl() to the FormResponse class and with that it becomes possible to use code like this to get the edit URL for a bunch of existing forms:

function responseURL() {
 // Open a form by ID and log the responses to each question.
 var form = FormApp.openById('1gJw1MbMKmOYE40Og1ek0cRgtdofguIrAB8KhmB0BYXY'); //this is the ID in the url of your live form
 var formResponses = form.getResponses();
 for (var i = 0; i < formResponses.length; i++) {
   var formResponse = formResponses[i];
   Logger.log(formResponse.getEditResponseUrl());
 }
}

To make it automatically email the user as they respond one could add a trigger on form submit. As The situation I'm working with doesn't require people to log in with an apps account I don't have access to an email address automatically so I have a text question that captures the user's email address.

It does ask the question about whether or not editing the forms is what you want. I've been grappling with the relative advantages of editing an existing response or sending a prefilled form using toPrefilledUrl() so that I can see how things have changed over time. I guess this comes down to the value that tracking this will provide you.

If you are using Google Apps your responders can edit there form responses.

See: How to Edit Form Responses

--edit this is now possible. See other answers.

After user submits the form, on confirmation, s/he will see a link to edit his/her response. I'd like to include that link as a part of the confirmation email

That is not possible, period.

That link is not accessible anywhere and one can't guess/construct it. But, there's some workarounds that might suit you (some suggested here that I'll re-phrase), e.g.

Send a per-populated form link and have the user re-send it. You'd need to have some kind of control field (e.g. the username), so you can know and delete/ignore his older submits. Possibly automatically via a script.

You could also develop and publish an apps-script GUI and send a link to this apps script plus a parameter that you generate where you can determine which entry you should edit. The down-side of this approach is that it's somewhat cumbersome and overkill to re-design the whole form on Apps Script. But again, it works.

At last, you could open an "Enhancement Request" on Apps Script issue tracker and wait until they and Google Spreadsheet/Forms team get together to develop a solution.

Here is a clear blog post that shows you how to do it step by step and explains what's going on under the hood for AppsScripts newbies:

http://securitasdato.blogspot.com/2014/11/sending-confirmation-emails-from-google.html

While collectively you can get there from the all the excellent answers provided here, the script from that post worked best for me.

eskdale

Does this help - I haven't tried it but I was looking for the same thing a while ago and noticed this.

From this page https://developers.google.com/apps-script/reference/forms/

code from there contains this:

Logger.log('Published URL: ' + form.getPublishedUrl());
Logger.log('Editor URL: ' + form.getEditUrl());

Jon

Great, script works! Thanks.

For newbies, like me: Just paste the andre's code for function SendConfirmationMail(e) into your spreadsheet's code editor and set 'on form submit' trigger to run it. That's in spreadsheet script editor, not form script editor.

You need to hack in some values. Read the code. For me the confusing one was the need to replace the ********COLUMN SEQUENCE EX 14****** with the sheet column number where you want the edit urls to end up. I used 39 which is one column more than my form was using up.

However, I got runtime probs in this part:

for (var i in headers) {

            value = e.namedValues[headers[i]].toString();

            // Do not send the timestamp and blank fields            
            if ((i !== "0") && (value !== "")) {
                message += headers[i] + ' :: ' + value + "<br>";
            }
        }

Dunno why, but I replaced it with this:

 for (var keys in columns) {
        var key = columns[keys];
        if ( e.namedValues[key]) {
        message += key + ' :: '+ e.namedValues[key] + "<br>"; 
        } 
    }

Works for me.

Andre Vacari - Capo Software

Try This: (Credits is not for me, because i merge two solutions of the third part)

Source: Send Confirmation Email with Google Forms

/* Send Confirmation Email with Google Forms */

function Initialize() {

    var triggers = ScriptApp.getScriptTriggers();

    for (var i in triggers) {
        ScriptApp.deleteTrigger(triggers[i]);
    }

    ScriptApp.newTrigger("SendConfirmationMail")
        .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
        .onFormSubmit()
        .create();

}

function SendConfirmationMail(e) {
  var form = FormApp.openById('***YOUR FORM CODE***');
    //enter form ID here

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('***SHEET NAME***');

    //Change the sheet name as appropriate
  var data = sheet.getDataRange().getValues();
  var urlCol = ***************COLUMN SEQUENCE EX 14******; // column number where URL's should be populated; A = 1, B = 2 etc
  var responses = form.getResponses();
  var timestamps = [], urls = [], resultUrls = [], url;

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

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

    try {

        var ss, cc, sendername, subject, headers;
        var message, value, textbody, sender;

        // This is your email address and you will be in the CC
        cc = Session.getActiveUser().getEmail();

        // This will show up as the sender's name
        sendername = "****YOUR NAME******";

        // Optional but change the following variable
        // to have a custom subject for Google Docs emails
        subject = "Registro de Oportunidade submetido com sucesso";

        // This is the body of the auto-reply
        message = "Nós recebemos seu registro de oportunidade.<br>Muito Obrigado!<br><br>";

        ss = SpreadsheetApp.getActiveSheet();
        headers = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];

        // This is the submitter's email address
        sender = e.namedValues["********COLUMN NAME OF DESTINATION E-MAIL************"].toString();

        for (var i in headers) {

            value = e.namedValues[headers[i]].toString();

            // Do not send the timestamp and blank fields            
            if ((i !== "0") && (value !== "")) {
                message += headers[i] + ' :: ' + value + "<br>";
            }
        }

        message += "<br>Link to edit" + ' :: ' + url + "<br>";
        textbody = message.replace("<br>", "\n");

        GmailApp.sendEmail(sender, subject, textbody, 
                            {cc: cc, name: sendername, htmlBody: message});

    } catch (e) {
        Logger.log(e.toString());
    }

}

you can try to populate a form with the values given from that email address than delete previous answers ...

it's not a beautiful way but it can works ...

I don't think we have access to what that value is through the Spreadsheet API (which means Apps Script doesn't have it either). The closest I can think of would be the "key" value in this feed. You'd have to test to find out though. There's no other alternative that I know of other than accessing the Spreadsheet API directly. So first, you'd have to get the last row through the api use ?reverse=true&max-results=1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!