【问题标题】:Google Form Responses in a Notification Email通知电子邮件中的 Google 表单回复
【发布时间】:2015-03-20 19:53:49
【问题描述】:

我在 Google 表单中使用表单通知插件。我已经编辑了 Code.gs 和 CreatorNotification.html 文件,一切正常 - 提交 Google 表单后,我会收到一封电子邮件。现在我正在尝试将该表单提交中的一些字段放入电子邮件中。但是随着我在下面添加的编辑,电子邮件通知停止工作。

在我的 Code.gs 脚本中:

function sendCreatorNotification() {
    var form = FormApp.getActiveForm();
    var settings = PropertiesService.getDocumentProperties();
    var responseStep = settings.getProperty('responseStep');
    responseStep = responseStep ? parseInt(responseStep) : 10;

    function onFormSubmit(e) {
        //Get information from form and set as variables
        var First_Name = e.value[2];

        // If the total number of form responses is an even multiple of the
        // response step setting, send a notification email(s) to the form
        // creator(s). For example, if the response step is 10, notifications
        // will be sent when there are 10, 20, 30, etc. total form responses
        // received.
        if (form.getResponses().length % responseStep == 0) {
            var addresses = settings.getProperty('creatorEmail').split(',');
            if (MailApp.getRemainingDailyQuota() > addresses.length) {
                var template = HtmlService.createTemplateFromFile('CreatorNotification');
                template.summary = form.getSummaryUrl();
                template.responses = form.getResponses().length;
                template.title = form.getTitle();
                template.responseStep = responseStep;
                template.formUrl = form.getEditUrl();
                template.notice = NOTICE;
                template.firstname = First_Name;
                var message = template.evaluate();
                MailApp.sendEmail(settings.getProperty('creatorEmail'),
                form.getTitle() + ': Case submission',
                message.getContent(), {
                    name: ADDON_TITLE,
                    htmlBody: message.getContent()
                });
            }
        }
    }
}

在 CreatorNotification.html 我有:

<p><i>Form Notifications</i> (a Google Forms add-on) has detected that the      form
titled <a href="<?= formUrl?>"><b><?= title ?></b></a> has received
<?= responses ?> responses so far.</p>

<p><a href="<?= summary ?>">Summary of form responses</a></p>

<p>First Name:</p> <?= firstname ?>

任何方向都将不胜感激。

【问题讨论】:

  • 如果您现在不知道,您需要调试代码并找出失败的行。在要检查变量值的地方添加Logger.log('this value: ' + variableName); 语句,然后查看日志,或使用调试器逐步执行代码。 Debugger and breakpoints
  • 您现在在sendCreatorNotification() 函数中拥有了onFormSubmit 函数。原来是这样的吗?
  • 我已经添加了 onFormSubmit 函数,希望它能够工作。它原本不在那里。如果我删除它,电子邮件通知将再次起作用。所以,我想问题是,如何将变量(表单中的一个或多个响应)填充到模板中?

标签: forms email google-sheets


【解决方案1】:

您正在尝试运行 两个 触发器。该“附加”代码创建了一个触发器。附加组件创建一个“可安装”触发器。您已经为要提交的表单设置了一个触发器。 onFormSubmit(e) 函数是一个“简单”触发器。因此,您设置了两个触发器以在提交表单时运行。所以,你制造了冲突。如果您查看 RESOURCES 菜单,在 CURRENT PROJECT 的 TRIGGERS 下,您应该会看到为 sendCreatorNotification() 函数定义的触发器。

您可以尝试以下方法。您不需要单独的 onFormSubmit(e) 简单触发器。 sendCreatorNotification() 函数具有可用的事件对象。只需将e 添加到sendCreatorNotification() 函数即可:

sendCreatorNotification(e)

编写代码以获取 e 的值。

【讨论】:

  • 不,我只添加了以下几行: function onFormSubmit(e) { //从表单获取信息并设置为变量 var First_Name = e.value[2];模板.名字 = 名字;原代码可以在这里找到:github.com/googlesamples/apps-script-form-notifications-addon
  • 好的,我现在正在使用 sendCreatorNotification(e) 并删除了多余的触发器。但是,它似乎不喜欢从 Google 表格中获取值的代码。调试模式显示此错误:Cannot read property "values" from undefined for the line: var First_Name = e.values[1];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多