簡體   English   中英

對電子郵件的 Google 表單回復

[英]Google Form responses to email

我想在提交表單后收到電子郵件,其中僅包含已填寫的回復,而不是空白或空白字段。

我做了一些研究,發現下面的腳本會立即觸發電子郵件發送到表單中存在的電子郵件地址。 此外,電子郵件包含所有字段,包括空字段。

請幫我實現一個滿足我要求的腳本。

    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});

我找到了我的問題的解決方案。 我將其發布在下面以進行知識共享。 謝謝你。

將以下腳本粘貼到響應電子表格的腳本編輯器中。 首先運行初始化函數來啟動腳本,然后每當提交表單時,將觸發一封電子郵件,其中僅填寫表單中的字段。

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());
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM