ตอบกลับความคิดเห็น

ระดับการเขียนโค้ด: ผู้เริ่มต้น
ระยะเวลา: 15 นาที
ประเภทโปรเจ็กต์: ระบบอัตโนมัติที่มีเมนูที่กำหนดเองและ ทริกเกอร์ที่ขับเคลื่อนด้วยเหตุการณ์

วัตถุประสงค์

  • ทำความเข้าใจสิ่งที่โซลูชันทำ
  • ทำความเข้าใจสิ่งที่บริการ Apps Script ทำภายในโซลูชัน
  • ตั้งค่าสคริปต์
  • เรียกใช้สคริปต์

เกี่ยวกับโซลูชันนี้

สร้างอีเมลตอบกลับฉบับร่างเพื่อตอบความคิดเห็นจาก Google ฟอร์มโดยอัตโนมัติ โซลูชันนี้มุ่งเน้นที่ความคิดเห็นเกี่ยวกับหลักสูตรจากนักเรียน แต่คุณสามารถนำไปใช้กับกรณีการใช้งานอื่นๆ ที่คุณได้รับความคิดเห็นโดยใช้ Google ฟอร์ม

การตอบกลับการส่งแบบฟอร์มที่ส่งจาก Gmail

วิธีการทำงาน

สคริปต์จะติดตั้งทริกเกอร์ที่ขับเคลื่อนด้วยเหตุการณ์ซึ่งจะทำงานทุกครั้งที่ผู้ใช้ส่งแบบฟอร์ม เมื่อมีการส่งแบบฟอร์มแต่ละครั้ง สคริปต์จะสร้างอีเมลฉบับร่างใน Gmail อีเมลจะส่งถึงผู้ที่ส่งแบบฟอร์มและมีคำตอบของแบบฟอร์มและข้อความขอบคุณทั่วไป คุณสามารถแก้ไขอีเมลก่อนส่งได้

บริการ Apps Script

โซลูชันนี้ใช้บริการต่อไปนี้

  • บริการ Script: ติดตั้งทริกเกอร์ที่ขับเคลื่อนด้วยเหตุการณ์ ซึ่งจะเริ่มทำงานเมื่อมีคนส่งแบบฟอร์ม
  • บริการสเปรดชีต: ส่งคำตอบของแบบฟอร์ม ไปยัง Gmail
  • บริการ Gmail: สร้าง อีเมลฉบับร่างพร้อมข้อความขอบคุณและคำตอบของแบบฟอร์ม

ข้อกำหนดเบื้องต้น

หากต้องการใช้ตัวอย่างนี้ คุณต้องมีข้อกำหนดเบื้องต้นต่อไปนี้

  • บัญชี 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>

ผู้ร่วมให้ข้อมูล

ตัวอย่างนี้สร้างขึ้นโดย Ben Collins นักการศึกษาที่ benlcollins.com และ ผู้เชี่ยวชาญสำหรับนักพัฒนาแอปของ Google

ตัวอย่างนี้ได้รับการดูแลโดย Google โดยได้รับความช่วยเหลือจาก Google Developer Experts

ขั้นตอนถัดไป