Google表单对电子邮件的回复 [英] Google Form responses to email

查看:98
本文介绍了Google表单对电子邮件的回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提交表格后,我只希望收到一封电子邮件,其中只填写填写的回复,而没有空白或空白字段.

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

推荐答案

我已经找到了解决我问题的方法.我将其发布在下面以进行知识共享.谢谢.

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

这篇关于Google表单对电子邮件的回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆