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

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

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

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

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

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

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

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

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

บริการ Apps Script

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

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

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

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

  • บัญชี Google (บัญชี Google Workspace อาจต้อง ได้รับการอนุมัติจากผู้ดูแลระบบ)
  • เว็บเบราว์เซอร์ที่มีสิทธิ์เข้าถึงอินเทอร์เน็ต

ตั้งค่าสคริปต์

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

เรียกใช้สคริปต์

  1. คลิกเครื่องมือตอบกลับแบบฟอร์ม > เปิดใช้การตอบกลับแบบร่างอัตโนมัติ คุณอาจต้องรีเฟรชหน้าเว็บเพื่อให้เมนูที่กำหนดเองนี้ปรากฏ
  2. ให้สิทธิ์สคริปต์เมื่อได้รับข้อความแจ้ง หากหน้าจอคำยินยอมของ OAuth แสดงคำเตือนว่า แอปนี้ยังไม่ได้รับการยืนยัน ให้ดำเนินการต่อโดยเลือกขั้นสูง > ไปที่ {ชื่อโปรเจ็กต์} (ไม่ปลอดภัย)

  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

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

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