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

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

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

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

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

สร้างอีเมลตอบกลับฉบับร่างโดยอัตโนมัติสำหรับความคิดเห็นจาก Google Forms โซลูชันนี้มุ่งเน้นที่ความคิดเห็นเกี่ยวกับหลักสูตรจากนักเรียน แต่คุณนำไปใช้กับกรณีการใช้งานใดก็ได้ที่คุณได้รับความคิดเห็นผ่าน 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 Developer Expert

ตัวอย่างนี้ได้รับการดูแลโดย Google ด้วยความช่วยเหลือจากผู้เชี่ยวชาญด้านการพัฒนาซอฟต์แวร์ของ Google

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