ส่งเนื้อหาที่ดูแลจัดการ

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

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

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

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

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

การสาธิตการส่งเนื้อหาด้วย Google ฟอร์มและ Gmail

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

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

บริการ Apps Script

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

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

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

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

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

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

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

  2. ในสเปรดชีตที่คัดลอก ให้คลิกส่วนขยาย > Apps Script

  3. เลือก installTrigger ในเมนูฟังก์ชันแบบเลื่อนลง

  4. คลิกเรียกใช้

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

สำคัญ: หากเรียกใช้ installTrigger มากกว่า 1 ครั้ง สคริปต์จะสร้าง ทริกเกอร์หลายรายการ ซึ่งแต่ละรายการจะส่งอีเมลเมื่อผู้ใช้ส่งแบบฟอร์ม หากต้องการลบทริกเกอร์ที่ไม่จำเป็นและหลีกเลี่ยงอีเมลที่ซ้ำกัน ให้คลิกทริกเกอร์ คลิกขวาที่ทริกเกอร์เพิ่มเติมแต่ละรายการ แล้วคลิกลบทริกเกอร์

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

  1. เปลี่ยนกลับไปที่สเปรดชีต แล้วคลิกเครื่องมือ > จัดการแบบฟอร์ม > ไปที่แบบฟอร์มที่ใช้งานจริง
  2. กรอกข้อมูลในแบบฟอร์ม และคลิกส่ง
  3. ตรวจสอบอีเมลเพื่อดูอีเมลที่มีลิงก์ไปยังเนื้อหาที่คุณเลือก

ตรวจสอบโค้ด

หากต้องการตรวจสอบโค้ด Apps Script สำหรับโซลูชันนี้ ให้คลิกดูซอร์สโค้ดด้านล่าง

ดูซอร์สโค้ด

Code.gs

solutions/automations/content-signup/Code.js
// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/automations/content-signup

/*
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.
*/

// To use your own template doc, update the below variable with the URL of your own Google Doc template.
// Make sure you update the sharing settings so that 'anyone'  or 'anyone in your organization' can view.
const EMAIL_TEMPLATE_DOC_URL = 'https://docs.google.com/document/d/1enes74gWsMG3dkK3SFO08apXkr0rcYBd3JHKOb2Nksk/edit?usp=sharing';
// Update this variable to customize the email subject.
const EMAIL_SUBJECT = 'Hello, here is the content you requested';

// Update this variable to the content titles and URLs you want to offer. Make sure you update the form so that the content titles listed here match the content titles you list in the form.
const topicUrls = {
  'Google Calendar how-to videos': 'https://www.youtube.com/playlist?list=PLU8ezI8GYqs7IPb_UdmUNKyUCqjzGO9PJ',
  'Google Drive how-to videos': 'https://www.youtube.com/playlist?list=PLU8ezI8GYqs7Y5d1cgZm2Obq7leVtLkT4',
  'Google Docs how-to videos': 'https://www.youtube.com/playlist?list=PLU8ezI8GYqs4JKwZ-fpBP-zSoWPL8Sit7',
  'Google Sheets how-to videos': 'https://www.youtube.com/playlist?list=PLU8ezI8GYqs61ciKpXf_KkV7ZRbRHVG38',
};

/**
 * Installs a trigger on the spreadsheet for when someone submits a form.
 */
function installTrigger() {
  ScriptApp.newTrigger('onFormSubmit')
      .forSpreadsheet(SpreadsheetApp.getActive())
      .onFormSubmit()
      .create();
}

/**
 * Sends a customized email for every form response.
 * 
 * @param {Object} event - Form submit event
 */
function onFormSubmit(e) {
  let responses = e.namedValues;

  // If the question title is a label, it can be accessed as an object field.
  // If it has spaces or other characters, it can be accessed as a dictionary.
  let timestamp = responses.Timestamp[0];
  let email = responses['Email address'][0].trim();
  let name = responses.Name[0].trim();
  let topicsString = responses.Topics[0].toLowerCase();

  // Parse topics of interest into a list (since there are multiple items
  // that are saved in the row as blob of text).
  let topics = Object.keys(topicUrls).filter(function(topic) {
    // indexOf searches for the topic in topicsString and returns a non-negative
    // index if the topic is found, or it will return -1 if it's not found.
    return topicsString.indexOf(topic.toLowerCase()) != -1;
  });

  // If there is at least one topic selected, send an email to the recipient.
  let status = '';
  if (topics.length > 0) {
    MailApp.sendEmail({
      to: email,
      subject: EMAIL_SUBJECT,
      htmlBody: createEmailBody(name, topics),
    });
    status = 'Sent';
  }
  else {
    status = 'No topics selected';
  }

  // Append the status on the spreadsheet to the responses' row.
  let sheet = SpreadsheetApp.getActiveSheet();
  let row = sheet.getActiveRange().getRow();
  let column = e.values.length + 1;
  sheet.getRange(row, column).setValue(status);

  console.log("status=" + status + "; responses=" + JSON.stringify(responses));
}

/**
 * Creates email body and includes the links based on topic.
 *
 * @param {string} recipient - The recipient's email address.
 * @param {string[]} topics - List of topics to include in the email body.
 * @return {string} - The email body as an HTML string.
 */
function createEmailBody(name, topics) {
  let topicsHtml = topics.map(function(topic) {
  let url = topicUrls[topic];
    return '<li><a href="' + url + '">' + topic + '</a></li>';
  }).join('');
  topicsHtml = '<ul>' + topicsHtml + '</ul>';

  // Make sure to update the emailTemplateDocId at the top.
  let docId = DocumentApp.openByUrl(EMAIL_TEMPLATE_DOC_URL).getId();
  let emailBody = docToHtml(docId);
  emailBody = emailBody.replace(/{{NAME}}/g, name);
  emailBody = emailBody.replace(/{{TOPICS}}/g, topicsHtml);
  return emailBody;
}

/**
 * Downloads a Google Doc as an HTML string.
 * 
 * @param {string} docId - The ID of a Google Doc to fetch content from.
 * @return {string} The Google Doc rendered as an HTML string.
 */
function docToHtml(docId) {

  // Downloads a Google Doc as an HTML string.
  let url = "https://docs.google.com/feeds/download/documents/export/Export?id=" +
            docId + "&exportFormat=html";
  let param = {
    method: "get",
    headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
    muteHttpExceptions: true,
  };
  return UrlFetchApp.fetch(url, param).getContentText();
}

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

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

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