Just finished another Google Script and I wanted to share it with you all. There is an option when setting up a Google Form so that the submitter can go back and edit the responses. However, if the submitter does not bookmark the URL immediately, it’s impossible to go back later and find it.
This script solves that problem by doing two things: First, it adds the link to the re-edit URL on the spreadsheet itself so that whoever is managing the sheet can have a record of it. Second, it automatically emails the link back to the person submitting the form.
I’ve pasted the code below so you can copy directly from this post. To use the script, set up a Form and a Response sheet using Google Drive. Then go to the Response sheet and click Tools > Script Editor. Create a new Spreadsheet Script and delete all of the code that appears. Paste the code below into that window. Finally, set up an automatic trigger as described in the comments at the top of the code. If you have any questions, send me an email and I’ll try to help!
***Note*** This is an older version of the script that I’ve left up because it was getting lots of traffic. For a newer version, take a look at my post here.
/* Script created on 27 Sept 2014 by Paul Swanson (@teachertechpaul) TO PEOPLE USING THIS SCRIPT: The purpose of this script is to record the re-edit URL of a form submission and also to email that link back to the person who filled out the form. To use this script, you need to do a couple of things: 1 - Edit the 'COLUMN_NUMBER_OF_EMAIL' variable to match the column on your response sheet that contains the email address of the submitter 2 - Edit the emailSubject and emailBody variables to match your needs 3 - Click Resources, Current Project Triggers. Click the link: 'No triggers set up. Click here to add one now.' Set your trigger with the following properties: - Run: 'editFormSubmission' - Events: 'From spreadsheet' and 'On form submit' That's it! If you like this script, email me at paul@teacherpaul.org */ COLUMN_NUMBER_OF_EMAIL = 2; function editFormSubmission(e) { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName('Form Responses 1'); /* This section gets the Edit URL of the most recent submission so that it can be sent via email back to the user. It also matches the time stamp of the form reponse with that of the spreadsheet to insert the Edit Url in the sheet itself. */ var formUrl = ss.getFormUrl(); var form = FormApp.openByUrl(formUrl); var formResponses = form.getResponses(); var lastResponse = formResponses[formResponses.length - 1]; var formEditUrl = lastResponse.getEditResponseUrl(); var formEditCell = '=HYPERLINK("' + formEditUrl + '","Edit Link")'; var timeStamp = lastResponse.getTimestamp().valueOf(); var editUrlCol = getEditUrlCol(sheet, "Form Edit URL"); for (var row=1; row <= sheet.getLastRow(); row++) { var testTimeCell = sheet.getRange(row, 1).getValue().valueOf(); if (timeStamp == testTimeCell) { sheet.getRange(row, editUrlCol).setValue(formEditCell); } } Logger.log(lastResponse.getItemResponses()); var emailAddress = sheet.getRange(sheet.getLastRow(), [COLUMN_NUMBER_OF_EMAIL]).getValue(); var emailSubject = "Link to Re-Edit your Submission"; var emailBody = "Thank you for your submission! If you would like to continue editing it, please click the link below." + String.fromCharCode(10) + String.fromCharCode(10) + formEditUrl; Logger.log("Send email to " + emailAddress + " with subject: " + emailSubject); Logger.log("Email body: " + emailBody); MailApp.sendEmail(emailAddress, emailSubject, emailBody); } function getEditUrlCol(sheet, headerStr){ for (var col=1; col<= sheet.getLastColumn(); col++) { if (sheet.getRange(1, col).getValue() == headerStr) { return col; } } // If this section runs, it means header is not established sheet.getRange(1, sheet.getLastColumn() + 1).setValue("Form Edit URL"); return (sheet.getLastColumn()); }
24 thoughts on “Google Script – Edit Form Submission”
I keep getting an error:
TypeError: Cannot call method “getSheetByName” of null. (line 6, file “Code”)
Tried not changing it, and I’ve tried putting the sheet name in place of (‘Form Responses 1’).
I had (‘Wrestler Info Form 2014 (Responses)’)
but neither worked, both gave me the above error message.
Thanks.
never mind, was installing it onto the form not the spreadsheet. Got much further…. Now I get the error
TypeError: Cannot call method “getLastColumn” of null. (line 42, file “Code”)
for the line:
for (var col=1; col<= sheet.getLastColumn(); col++) {
it is also not posting the link url into the last column of the spreadsheet… I’m guessing that is where the error is.
Josh,
Thanks for responding. I think I was able to iron out the bugs that you found. Try it again and let me know if you have any problems with it.
Cheers,
Paul
Thanks! after much work, I finally figured out that I counted columns wrong (I had one hidden) so my email column # was wrong. Fixed that and everything works. Thanks for sharing!
Hello. This is great and it does exactly what I want. Except that I do not need it to send the email. How can I make this change so that it populates the spreadsheet but does not send the email. Thank you.
Scott – no problem. Just remove this section of code at the end of the script:
var emailAddress = sheet.getRange(sheet.getLastRow(), [COLUMN_NUMBER_OF_EMAIL]).getValue();
var emailSubject = “Link to Re-Edit your Submission”;
var emailBody = “Thank you for your submission! If you would like to continue editing it, please click the link below.” +
String.fromCharCode(10) + String.fromCharCode(10) + formEditUrl;
Logger.log(“Send email to ” + emailAddress + ” with subject: ” + emailSubject);
Logger.log(“Email body: ” + emailBody);
MailApp.sendEmail(emailAddress, emailSubject, emailBody);
Hi There
Getting an issue with the code:
TypeError: Cannot call method “getLastColumn” of undefined (line 43, file “Code”)
Any ideas
Could be a few different things. If you share the sheet with me at pswanson@unishanoi.org then I can take a look at the code if you like.
Sorry it’s taken me a while to get back to this – I just got back from a conference.
Paul
Hi,
I have a small question,
In your script you have this validation
” if (timeStamp == testTimeCell) {”
It seems that timeStamp is in miliseconds but testTimeCell is only in seconds.
1430750726178==1430750726000
Any idea how I can force miliseconds in google sheet?
Regards,
Mathieu
found it…
You need to be in the latest version of google sheet.
https://support.google.com/docs/answer/6082736
Thank you so much for Google script. This is really helpful.
Will it be possible to add some values on email body? I’d like to add recipient’s name on email body whoever write the form.
For example, xxx value needs to be pulled from response sheet.
“Dear xxx”
“Thank you for your submission! If you would like to continue editing it, please click the link below.”
Thank you so much!
Thank you for this script. It is exactly what I was looking for,
Thanks for the script! I followed the instruction and was able to receive the email with a working link and generate a new column with “Form Edit URL” heading, but strangely the spreadsheet isn’t populating with the Edit URL, it’s just blank. Can anyone help me out? Thanks!
Thank you so much! This is EXACTLY what I was looking for!
Unfortunately, I also am getting the error: TypeError: Cannot call method “getLastColumn” of undefined (line 43, file “Code”)
And so the column is not showing any of the Edit URLS, but it is sending the confirmation emails with the links, which is what’s most important!
This script is great. My problem is that it works when I run it in the script editor but the trigger doesn’t seem to be working from the form.
Thanks, Teacher Paul,
this solved a distinct problem i had. i did have a snag, in that i originally added a column into the sheet as the FIRST column to receive the edit URL. after a bit, i realized your script assumed Timestamp was in column 1. i rewrote to figure out where “Timestamp” is and voila!
. . .
var editUrlCol = getNamedCol(sheet, “Form Edit URL”);
var timeStampCol = getNamedCol(sheet, “Timestamp”);
for (var row=1; row <= sheet.getLastRow(); row++) {
var testTimeCell = sheet.getRange(row, timeStampCol).getValue().valueOf();
if (timeStamp == testTimeCell) {
sheet.getRange(row, editUrlCol).setValue(formEditCell);
}
}
. . .
function getNamedCol(sheet, headerStr){
for (var col=1; col<= sheet.getLastColumn(); col++) {
if (sheet.getRange(1, col).getValue() == headerStr) {
return col;
}
}
// If this section runs, it means header is not established
sheet.getRange(1, sheet.getLastColumn() + 1).setValue("Form Edit URL");
return (sheet.getLastColumn());
}
. . .
THANK YOU!
THANK YOU! I looked at add-ons for 2 full days just trying to come up with a way to enable an edit capability from within a Google Site (without coding). This is so simple, and allows the form to be recalled from anywhere that I render the link!
This is a really useful script! Thank you for sharing it.
This is awesome. Thanks.
Hi, somehow I broke the script that puts the edit link in the last column. Can you help?
Are you there? My email address is aschweifler@sportsbasement.com
Thank you very much for sharing this. It works wonderfully!