| Note: This example is not production ready. I wanted to post it quickly for Scouters who wanted to adapt it for their use. I will produce a production ready version and put it in the Gallery. If you can wait for that, I recommend it. |
To enable an email response for form submission we must add a script to our spreadsheet and trigger it to run each time a new row is added. In this example the content of the response email is also encoded in the spreadsheet. This script will not work correctly if two people were to submit their responses simultaneously. I will document that process below. Our script looks at the data that has been received, uses it to create and send an email.
The first part of the script defines an email address for the script owner. This is where replies to the confirmation email will go and is also where any errors will be reported. We also define the subject of the confirmation email.
function emailResponse() {
var admin = 'scerruti@email.invalid';
var subject = 'Form Submission Accepted';
var admin = 'scerruti@email.invalid';
var subject = 'Form Submission Accepted';
The next part of our script grants us access to the form data. Sheet1 in this case is the form data. We use getLastRow to get the row number of the data that was just entered. We get the email address of the recipient from the form data, assumed here to be in column B.
var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1");
var NumberofRows = sheet.getLastRow();
var recipient = sheet.setActiveCell("B"+(NumberofRows)).getValue();
var NumberofRows = sheet.getLastRow();
var recipient = sheet.setActiveCell("B"+(NumberofRows)).getValue();
Next we are going to set the value of the named range "LASTROW". This populates the sheet that contains our response email, Sheet2, with the correct form values.
SpreadsheetApp.getActive()
.getRangeByName("LASTROW").setValue(NumberofRows);
var responseSheet = SpreadsheetApp.getActive()
.getSheetByName("Sheet2");
.getRangeByName("LASTROW").setValue(NumberofRows);
var responseSheet = SpreadsheetApp.getActive()
.getSheetByName("Sheet2");
Now the script constructs a plain text and html version of the email by copying the contents of the first 11 cells of column A.
var body = "";
var bodyHTML = "";
for (row = 1; row < 11; row++)
{
var temp = responseSheet.setActiveCell("A"+(row)).getValue();
body += '\n' + temp;
bodyHTML += ''+temp+' ';
}>
var bodyHTML = "";
for (row = 1; row < 11; row++)
{
var temp = responseSheet.setActiveCell("A"+(row)).getValue();
body += '\n' + temp;
bodyHTML += ''+temp+' ';
}>
Finally the script sends the email, or if an error occurs sends an email to the administrator.
var advancedArgs = {htmlBody:bodyHTML, replyTo:admin};
try {
MailApp.sendEmail(recipient, subject, body, advancedArgs);
} catch(e){
MailApp.sendEmail(admin, "Error - Pack 721 Form Submission Response", e.message);
}
}
try {
MailApp.sendEmail(recipient, subject, body, advancedArgs);
} catch(e){
MailApp.sendEmail(admin, "Error - Pack 721 Form Submission Response", e.message);
}
}
To make use of this script:
- Add a new sheet to the spreadsheet that backs your form, Insert -> New sheet, leave the new sheet named Sheet2.
- Somewhere on Sheet2, but not in column A, select a cell and name it LASTROW, Insert -> Named range -> Define new range....
- Create your email in column A of the new sheet referencing data from the form using the following example as a guide.
=CONCATENATE("Dear "; INDIRECT(ADDRESS(LASTROW;3;TRUE;TRUE;"Sheet1"));",")
- Use CONCATENATE to mix form content with text.
- Use INDIRECT and ACCESS to access form data
- 3 in this example is the column number
- Add the script to the spreadsheet, Tools -> Script editor ....
- Configure the script to run when the form is submitted, Resources -> Current script's triggers ...
I realize this post may be a little technical. This is primarily because the script was a quick and dirty concept. Once I have a more polished script available in the Gallery the instructions will be greatly simplified.
Resources:
No comments:
Post a Comment