回應意見回饋

程式設計層級:入門
時間長度:15 分鐘
專案類型:透過自訂選單事件導向的觸發條件

目標

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

認識這項解決方案

根據 Google 表單自動建立意見回饋草稿。這個 解決方案著重於學生對課程的意見回饋,但您可以將其應用到任何 提供 Google 表單相關意見的應用實例

Gmail 傳送的表單提交回覆

運作方式

指令碼會安裝事件驅動的觸發條件,在使用者每次提交 表單中要求的資訊。每次提交表單後,指令碼會在 Gmail 中建立電子郵件草稿。 電子郵件會寄送給提交表單的使用者,並在信中附上表單 以及一般的感謝訊息您可以先編輯電子郵件地址 傳送。

Apps Script 服務

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

  • 指令碼服務:安裝事件導向 會在使用者提交表單時觸發
  • 試算表服務:傳送表單 回覆 Gmail。
  • Gmail 服務:使用以下工具建立電子郵件草稿: 感謝訊息和表單回覆

必要條件

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

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

設定指令碼

點選下方按鈕即可建立回覆意見回饋範例 試算表。這項操作的 Apps Script 專案 解決方案附加在試算表上
建立副本

執行指令碼

  1. 按一下「表單回覆工具」 >啟用自動回覆草稿功能。您可能需要重新整理頁面,才能執行這項操作 系統隨即會顯示自訂選單
  2. 出現提示時,請授權指令碼。 如果 OAuth 同意畫面顯示「這個應用程式未經驗證」警告, 如要繼續,請選取「進階」圖示 > 前往 {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,
      }
  );
}

貢獻者

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

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

後續步驟