回复反馈

编码级别:新手
时长:15 分钟
项目类型:通过自定义菜单事件驱动型触发器实现自动化

目标

  • 了解解决方案的用途。
  • 了解 Apps 脚本服务在此解决方案中执行的操作。
  • 设置脚本。
  • 运行脚本。

关于此解决方案

针对 Google 表单的反馈自动创建电子邮件回复草稿。此解决方案主要关注学生的课程反馈,但您可以将其应用于通过 Google 表单收到反馈的任何用例。

从 Gmail 发送的表单提交回复

运作方式

该脚本会安装事件驱动型触发器,每当用户提交表单时,该触发器都会运行。每次提交表单时,脚本都会在 Gmail 中创建电子邮件草稿。该电子邮件会发送给提交表单的人员,其中包含表单回复和通用的致谢消息。您可以先修改电子邮件,然后再发送。

Apps 脚本服务

此解决方案使用以下服务:

  • 脚本服务 - 安装在用户提交表单时触发的事件驱动型触发器。
  • 电子表格服务 - 将表单回复发送到 Gmail。
  • Gmail 服务 - 创建包含感谢邮件和表单回复的电子邮件草稿。

前提条件

如需使用此示例,您需要满足以下前提条件:

  • Google 帐号(Google Workspace 帐号可能需要管理员批准)。
  • 能够访问互联网的网络浏览器。

设置脚本

点击以下按钮复制回复反馈电子表格示例。此解决方案的 Apps 脚本项目已附加到该电子表格中。
复制

运行脚本

  1. 点击表单回复工具 > 启用自动草稿回复。您可能需要刷新页面才能看到此自定义菜单。
  2. 出现提示时,为脚本授权。 如果 OAuth 同意屏幕显示 This app is not verify 警告,请依次选择 Advanced > Go to {Project Name} (unsafe),以继续操作。

  3. 再次点击表单回复工具 > 启用自动草稿回复

  4. 依次点击 Tools(工具)> Manage form(管理表单)> Go to live form(转到在线表单)。

  5. 填写表单,然后点击提交

  6. 打开 Gmail 并检查草稿。您应该会有一份包含表单回复的新草稿。

查看代码

要查看此解决方案的 Apps 脚本代码,请点击下面的查看源代码

查看源代码

Code.gs

solutions/automations/course-feedback-response/Code.js
// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/automations/course-feedback-response

/*
Copyright 2022 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/**
 * Creates custom menu for user to run scripts.
 */
function onOpen() {
  let ui = SpreadsheetApp.getUi();
  ui.createMenu('Form Reply Tool')
      .addItem('Enable auto draft replies', 'installTrigger')
      .addToUi();
}

/**
 * Installs a trigger on the Spreadsheet for when a Form response is submitted.
 */
function installTrigger() {
  ScriptApp.newTrigger('onFormSubmit')
      .forSpreadsheet(SpreadsheetApp.getActive())
      .onFormSubmit()
      .create();
}

/**
 * Creates a draft email for every response on a form
 *
 * @param {Object} event - Form submit event
 */
function onFormSubmit(e) {
  let responses = e.namedValues;

  // parse form response data
  let timestamp = responses.Timestamp[0];
  let email = responses['Email address'][0].trim();

  // create email body
  let emailBody = createEmailBody(responses);

  // create draft email
  createDraft(timestamp, email, emailBody);
}

/**
 * Creates email body and includes feedback from Google Form.
 *
 * @param {string} responses - The form response data
 * @return {string} - The email body as an HTML string
 */
function createEmailBody(responses) {
  // parse form response data
  let name = responses.Name[0].trim();
  let industry = responses['What industry do you work in?'][0];
  let source = responses['How did you find out about this course?'][0];
  let rating = responses['On a scale of 1 - 5 how would you rate this course?'][0];
  let productFeedback = responses['What could be different to make it a 5 rating?'][0];
  let otherFeedback = responses['Any other feedback?'][0];

  // create email body
  let htmlBody = 'Hi ' + name + ',<br><br>' +
    'Thanks for responding to our course feedback questionnaire.<br><br>' +
      'It\'s really useful to us to help improve this course.<br><br>' +
        'Have a great day!<br><br>' +
          'Thanks,<br>' +
            'Course Team<br><br>' +
              '****************************************************************<br><br>' +
                '<i>Your feedback:<br><br>' +
                  'What industry do you work in?<br><br>' +
                    industry + '<br><br>' +
                      'How did you find out about this course?<br><br>' +
                        source + '<br><br>' +
                          'On a scale of 1 - 5 how would you rate this course?<br><br>' +
                            rating + '<br><br>' +
                              'What could be different to make it a 5 rating?<br><br>' +
                                productFeedback + '<br><br>' +
                                  'Any other feedback?<br><br>' +
                                    otherFeedback + '<br><br></i>';

  return htmlBody;
}

/**
 * Create a draft email with the feedback
 *
 * @param {string} timestamp Timestamp for the form response
 * @param {string} email Email address from the form response
 * @param {string} emailBody The email body as an HTML string
 */
function createDraft(timestamp, email, emailBody) {
  console.log('draft email create process started');

  // create subject line
  let subjectLine = 'Thanks for your course feedback! ' + timestamp;

  // create draft email
  GmailApp.createDraft(
      email,
      subjectLine,
      '',
      {
        htmlBody: emailBody,
      }
  );
}

贡献者

此示例由 benlcollins.com 教育工作者 Ben Collins 和 Google 开发者专家创建。

此示例由 Google 在 Google 开发者专家的帮助下进行维护。

后续步骤