Trả lời phản hồi

Cấp độ lập trình: Cơ bản
Thời lượng: 15 phút
Loại dự án: Tự động hoá bằng trình đơn tuỳ chỉnhtrình kích hoạt dựa trên sự kiện

Mục tiêu

  • Hiểu rõ chức năng của giải pháp.
  • Tìm hiểu chức năng của các dịch vụ Apps Script trong giải pháp này.
  • Thiết lập tập lệnh.
  • Chạy tập lệnh.

Thông tin về giải pháp này

Tự động tạo email nháp để trả lời cho ý kiến phản hồi trên Google Biểu mẫu. Giải pháp này tập trung vào ý kiến phản hồi về khoá học của học viên. Tuy nhiên, bạn có thể áp dụng cho bất kỳ trường hợp sử dụng nào mà bạn nhận được ý kiến phản hồi thông qua Google Biểu mẫu.

Phản hồi gửi biểu mẫu được gửi từ Gmail

Cách thức hoạt động

Tập lệnh cài đặt một trình kích hoạt dựa trên sự kiện sẽ chạy mỗi khi người dùng gửi một biểu mẫu. Với mỗi lần gửi biểu mẫu, tập lệnh sẽ tạo một email nháp trong Gmail. Email được gửi đến người gửi biểu mẫu và bao gồm phản hồi biểu mẫu cũng như thư cảm ơn chung chung. Bạn có thể chỉnh sửa email trước khi gửi email.

Dịch vụ Apps Script

Giải pháp này sử dụng các dịch vụ sau:

  • Dịch vụ tập lệnh – Cài đặt điều kiện kích hoạt dựa trên sự kiện sẽ kích hoạt khi người dùng gửi một biểu mẫu.
  • Dịch vụ bảng tính – Gửi câu trả lời qua biểu mẫu đến Gmail.
  • Dịch vụ Gmail – Tạo thư nháp chứa thư cảm ơn và câu trả lời trong biểu mẫu.

Điều kiện tiên quyết

Để sử dụng mẫu này, bạn cần có các điều kiện tiên quyết sau:

  • Tài khoản Google (các tài khoản Google Workspace có thể phải được quản trị viên phê duyệt).
  • Một trình duyệt web có quyền truy cập vào Internet.

Thiết lập tập lệnh

Hãy nhấp vào nút sau đây để tạo bản sao của bảng tính mẫu Trả lời ý kiến phản hồi. Dự án Apps Script cho giải pháp này được đính kèm vào bảng tính.
Tạo bản sao

Chạy tập lệnh

  1. Nhấp vào Công cụ trả lời biểu mẫu > Bật tính năng tự động trả lời thư nháp. Bạn có thể cần phải làm mới trang để trình đơn tuỳ chỉnh này xuất hiện.
  2. Khi được nhắc, hãy cho phép tập lệnh chạy. Nếu màn hình xin phép bằng OAuth hiển thị cảnh báo, Ứng dụng này chưa được xác minh, hãy tiếp tục bằng cách chọn Nâng cao > Chuyển đến {Project Name} (không an toàn).

  3. Nhấp vào Công cụ trả lời biểu mẫu > Bật lại câu trả lời nháp tự động.

  4. Nhấp vào Công cụ > Quản lý biểu mẫu > Chuyển đến biểu mẫu trực tiếp.

  5. Điền thông tin vào biểu mẫu và nhấp vào Gửi.

  6. Mở Gmail rồi kiểm tra thư nháp. Bạn sẽ có một bản nháp mới với câu trả lời biểu mẫu.

Xem xét mã

Để xem lại mã Apps Script cho giải pháp này, hãy nhấp vào phần Xem mã nguồn bên dưới:

Xem mã nguồn

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

Người đóng góp

Mẫu này do Ben Collins, Nhà giáo dục tại benlcollins.com và Chuyên gia nhà phát triển của Google tạo ra.

Mẫu này được Google duy trì với sự trợ giúp của các chuyên gia nhà phát triển của Google.

Các bước tiếp theo