问题
I want to get email once the form is submitted with only the filled in responses but not the blank or empty fields.
I have did some research and found the below script which triggers email immediately to the email address present in the form. Also the email contains all the fields including the empty fields.
Please help me achieve a script which fulfills my requirement.
function setup() {
/* First, delete all previous triggers */
var triggers = ScriptApp.getProjectTriggers();
for (var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
/* Then add a trigger to send an email on form submit */
ScriptApp.newTrigger("sendConfirmationEmail")
.forForm(FormApp.getActiveForm())
.onFormSubmit()
.create();
}
function sendConfirmationEmail(e) {
// e is a Form Event object - see https://developers.google.com/apps-script/guides/triggers/events#google_forms_events
// Edit this to set the subject line for the sent email
var subject = "Registration Successful";
// This will show up as the sender's name
var sendername = "Your Name Goes Here";
// This is the body of the registration confirmation message
var message = "Thank you for registering.<br>We will be in touch.<br><br>";
message += "Your form responses were:<br><br>";
// response is a FormResponse - see https://developers.google.com/apps-script/reference/forms/form-response
var response = e.response;
var textbody, sendTo, bcc;
// Get the script owner's email address, in order to bcc: them
bcc = Session.getActiveUser().getEmail();
// Now loop around, getting the item responses and writing them into the email message
var itemResponses = response.getItemResponses();
for (var i = 0; i < itemResponses.length; i++) {
var itemResponse = itemResponses[i];
message += itemResponse.getItem().getTitle() +": " + itemResponse.getResponse() + "<br>";
// If this field is the email address, then use it to fill in the sendTo variable
// Check that your form item is named "Email Address" or edit to match
if (itemResponse.getItem().getTitle() == "Email Address") {
sendTo = itemResponse.getResponse();
}
}
message += "<br>If you wish to edit your response, please click on <a href=\"" + response.getEditResponseUrl() + "\">this link</a>.";
message += "<br><br>";
textbody = message.replace("<br>", "\n");
GmailApp.sendEmail(sendTo, subject, textbody,
{bcc: bcc, name: sendername, htmlBody: message});
回答1:
I have found the solution to my question. I am posting it below for knowledge sharing. Thank you.
Paste the below script in the responses spreadsheet's script editor. First run the initialise function to start the script, and later whenever the form is submitted a email will be triggered with only the filled out fields form the form.
function Initialize() {
try {
var triggers = ScriptApp.getProjectTriggers();
for (var i in triggers)
ScriptApp.deleteTrigger(triggers[i]);
ScriptApp.newTrigger("EmailGoogleFormData")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit().create();
} catch (error) {
throw new Error("Please add this code in the Google Spreadsheet");
}
}
function EmailGoogleFormData(e) {
if (!e) {
throw new Error("Please go the Run menu and choose Initialize");
}
try {
if (MailApp.getRemainingDailyQuota() > 0) {
// You may replace this with another email address
var email = "email address";
// Enter your subject for Google Form email notifications
var subject = "Form Submitted";
var key, entry,
message = "",
ss = SpreadsheetApp.getActiveSheet(),
cols = ss.getRange(1, 2, 1, ss.getLastColumn()).getValues()[0];
// Iterate through the Form Fields
for (var keys in cols) {
key = cols[keys];
entry = e.namedValues[key] ? e.namedValues[key].toString() : "";
// Only include form fields that are not blank
if ((entry !== "") && (entry.replace(/,/g, "") !== ""))
message += key + ' :: ' + entry + "\n\n";
}
MailApp.sendEmail(email, subject, message);
}
} catch (error) {
Logger.log(error.toString());
}
}
来源:https://stackoverflow.com/questions/32953998/google-form-responses-to-email