回應意見回饋

程式設計層級:新手
時間長度:15 分鐘
專案類型:使用自訂選單事件導向觸發條件進行自動化作業

目標

  • 瞭解解決方案的功能。
  • 瞭解 Apps Script 服務在解決方案中的功能。
  • 設定指令碼。
  • 執行指令碼。

認識這項解決方案

自動根據 Google 表單的意見回饋建立電子郵件回覆草稿。這個解決方案的重點放在學生提供的課程意見回饋,但只要是透過 Google 表單收到意見回饋的用途,也可以全部套用。

透過 Gmail 傳送的表單提交回覆

運作方式

這個指令碼會安裝事件導向的觸發條件,在使用者每次提交表單時執行。每次提交表單時,指令碼都會在 Gmail 中建立電子郵件草稿。電子郵件會寄送給提交表單的使用者,並附上表單回應和一般的感謝訊息。您可以先編輯電子郵件 再傳送。

Apps Script 服務

這項解決方案使用下列服務:

  • 指令碼服務:安裝事件導向的觸發條件,會在使用者提交表單時觸發。
  • 試算表服務:將表單回應傳送至 Gmail。
  • Gmail 服務:建立包含感謝訊息和表單回覆的電子郵件草稿。

必要條件

如要使用這個範例,您必須具備以下必要條件:

  • Google 帳戶 (Google Workspace 帳戶可能需要取得管理員核准)。
  • 可連上網際網路的網路瀏覽器。

設定指令碼

點選下方按鈕,即可複製「回覆意見回饋」範例試算表。這項解決方案的 Apps Script 專案已附加到試算表。
建立副本

執行指令碼

  1. 按一下「表單回覆工具」>「啟用自動回覆草稿」。您可能需要重新整理頁面,系統才會顯示這個自訂選單。
  2. 出現提示訊息時,請授權執行指令碼。如果 OAuth 同意畫面顯示警告「This app has not verification」(這個應用程式尚未驗證),請依序選取「Advanced」(進階) >「Go to {Project Name} (unsafe)」 (前往 {Project Name} (不安全))

  3. 再按一下「表單回覆工具」>「啟用自動草稿回覆」

  4. 依序點選「工具」>「管理表單」>「前往即時表單」

  5. 填寫表單,然後按一下 [提交]

  6. 開啟 Gmail 並檢查草稿。您應該會看到包含表單回應的新草稿。

查看程式碼

如要查看這項解決方案的 Apps Script 程式碼,請點選下方的「查看原始碼」

查看原始碼

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

貢獻者

這個範例是由 Ben Collins、benlcollins.com 和 Google Developers 專家的教師製作。

本範例是由 Google 在 Google Developers 專家的協助下維護。

後續步驟