フィードバックへの対応

コーディング レベル: 初級
所要時間: 15 分
プロジェクト タイプ: カスタム メニューイベント ドリブン トリガーを使用した自動化

目標

  • ソリューションの機能について理解する。
  • ソリューション内での Apps Script サービスの機能について理解する。
  • スクリプトを設定する。
  • スクリプトを実行する。

このソリューションについて

Google フォームからのフィードバックに対するメール返信の下書きを自動的に作成します。このソリューションは生徒からのコースのフィードバックに焦点を当てていますが、Google フォームを使用してフィードバックを受け取るあらゆるユースケースに適用できます。

Gmail から送信されるフォームの回答

仕組み

このスクリプトは、ユーザーがフォームを送信するたびに実行されるイベント ドリブン トリガーをインストールします。フォームが送信されるたびに、スクリプトは Gmail でメールの下書きを作成します。メールはフォームを送信したユーザー宛てに送信され、フォームの回答と一般的なお礼のメッセージが含まれます。メールは送信前に編集できます。

Apps Script サービス

このソリューションでは、次のサービスを使用します。

前提条件

このサンプルを使用するには、次の前提条件を満たす必要があります。

  • Google アカウント(Google Workspace アカウントの場合、管理者の承認が必要となる可能性があります)。
  • インターネットにアクセスできるウェブブラウザ。

スクリプトを設定する

次のボタンをクリックして、フィードバックに返信する サンプル スプレッドシートのコピーを作成します。

コピーを作成

スクリプトを実行する

  1. [フォーム返信ツール] > [自動下書き返信を有効にする] をクリックします。このカスタム メニューが表示されない場合は、ページの更新が必要になることがあります。
  2. メッセージが表示されたら、スクリプトを承認します。 <<../_snippets/oauth.md>>
  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() {
  const 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) {
  const responses = e.namedValues;

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

  // create email body
  const 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
  const name = responses.Name[0].trim();
  const industry = responses["What industry do you work in?"][0];
  const source = responses["How did you find out about this course?"][0];
  const rating =
    responses["On a scale of 1 - 5 how would you rate this course?"][0];
  const productFeedback =
    responses["What could be different to make it a 5 rating?"][0];
  const otherFeedback = responses["Any other feedback?"][0];

  // create email body
  const 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
  const subjectLine = `Thanks for your course feedback! ${timestamp}`;

  // create draft email
  GmailApp.createDraft(email, subjectLine, "", {
    htmlBody: emailBody,
  });
}
</section>

寄稿者

このサンプルは、 benlcollins.com の教育者であり、 Google デベロッパー エキスパートの Ben Collins が作成しました。

このサンプルは、Google デベロッパー エキスパートの協力を得て Google が管理しています。

次のステップ