简体   繁体   中英

Google Forms Email Notification When Updated

I am trying to write a script that will send specific emails based on whether or not someone submits a new response or edits their existing response. I need the subject line to read "First Name Last Name: Edit" or "First Name Last Name: Response" depending on what was submitted. If someone edits their response, only the values that were changed appear in the email; however, if they don't change their name, it doesn't show up in the subject line. Any ideas on how to change this so that it will show their name every time there is an edit or response? Thanks in advance!

function Initialize() {

try {

var triggers = ScriptApp.getProjectTriggers();

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

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

} catch (error) {
throw new Error("Please add this code in the Google Spreadsheet");
}
}
function formSubmitted(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 = "my email";

  // Enter your subject for Google Form email notifications

    var key, entry,
    ss = SpreadsheetApp.getActiveSheet(),
    cols = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0],
    message = "",
    first = e.values[1],
    last = e.values[2];
    var editSubject =  first + " " + last +": Edit";    
    var submitSubject = first + " " + last + ": Submitted";

    // 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";


  }

  }

if (e.range.getNotes()[0].join('')) {
MailApp.sendEmail(email, editSubject, message);
}
else {
MailApp.sendEmail(email, submitSubject, message);
}
}



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

You may want to check this documentation that uses triggers to send an email when a user responds to the form.

Sample request:

/**
 * Adjust the onFormSubmit trigger based on user's requests.
 */
function adjustFormSubmitTrigger() {
  var form = FormApp.getActiveForm();
  var triggers = ScriptApp.getUserTriggers(form);
  var settings = PropertiesService.getDocumentProperties();
  var triggerNeeded =
      settings.getProperty('creatorNotify') == 'true' ||
      settings.getProperty('respondentNotify') == 'true';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM