简体   繁体   English

更新时的Google Forms电子邮件通知

[英]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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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