สร้างการลงชื่อสมัครใช้สำหรับเซสชันต่างๆ ในการประชุม

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

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

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

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

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

ข้อมูลจากชีตที่โอนไปยังฟอร์มและปฏิทิน

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

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

บริการ Apps Script

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

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

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

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

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

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

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

  4. คลิกการประชุม > ตั้งค่า การประชุมอีกครั้ง

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

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

(ไม่บังคับ) รีเซ็ตโซลูชัน

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

รีเซ็ตโซลูชัน

ขั้นตอนที่ 1: รีเซ็ตพร็อพเพอร์ตี้ของสคริปต์ที่จัดเก็บไว้

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

ทำตามขั้นตอนด้านล่างเพื่อนำพร็อพเพอร์ตี้รหัสปฏิทินที่มีอยู่ออก

  1. ในสเปรดชีต ให้คลิกส่วนเสริม > Apps Script
  2. ในโปรแกรมแก้ไข Apps Script ให้เลือก resetProperties จาก รายการแบบเลื่อนลงของฟังก์ชัน แล้วคลิกเรียกใช้

ขั้นตอนที่ 2: ลบปฏิทินการประชุม

ทุกครั้งที่สคริปต์ทำงาน ระบบจะสร้างปฏิทินใหม่ หากไม่ต้องการ เก็บปฏิทินเดิมที่สร้างไว้ ให้ทำตามขั้นตอนต่อไปนี้

  1. ไปที่ calendar.google.com
  2. คลิกตัวเลือกสำหรับปฏิทินการประชุมข้างปฏิทินการประชุม > การตั้งค่าและการแชร์
  3. เลื่อนไปที่ด้านล่างของการตั้งค่า แล้วคลิกลบ

ขั้นตอนที่ 3: ลบทริกเกอร์การส่งแบบฟอร์ม

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

  1. ในสเปรดชีต ให้คลิกส่วนเสริม > Apps Script
  2. ในโปรเจ็กต์ Apps Script ให้คลิกทริกเกอร์ ทางด้านซ้าย
  3. คลิกเพิ่มเติม > ลบทริกเกอร์ข้างทริกเกอร์

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

  1. ในสเปรดชีต ให้คลิกขวาที่ชีตคำตอบของแบบฟอร์ม แล้วคลิก ยกเลิกการลิงก์แบบฟอร์ม > ตกลง
  2. คลิกขวาที่ชีตคำตอบของแบบฟอร์มอีกครั้ง แล้วคลิกลบ > ตกลง
  3. ไปที่ forms.google.com
  4. คลิกขวาที่แบบฟอร์มการประชุม แล้วคลิกนำออก > ย้ายไปที่ถังขยะ

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

ตรวจสอบโค้ด

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

ดูซอร์สโค้ด

Code.gs

solutions/automations/event-session-signup/Code.js
// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/automations/event-session-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.
*/

/**
 * Inserts a custom menu when the spreadsheet opens.
 */
function onOpen() {
  SpreadsheetApp.getUi().createMenu('Conference')
      .addItem('Set up conference', 'setUpConference_')
      .addToUi();
}

/**
 * Uses the conference data in the spreadsheet to create
 * Google Calendar events, a Google Form, and a trigger that allows the script
 * to react to form responses.
 */
function setUpConference_() {
  let scriptProperties = PropertiesService.getScriptProperties();
  if (scriptProperties.getProperty('calId')) {
    Browser.msgBox('Your conference is already set up. Look in Google Drive for your'
                   + ' sign-up form!');
                   return;
  }
  let ss = SpreadsheetApp.getActive();
  let sheet = ss.getSheetByName('Conference Setup');
  let range = sheet.getDataRange();
  let values = range.getValues();
  setUpCalendar_(values, range);
  setUpForm_(ss, values);
  ScriptApp.newTrigger('onFormSubmit').forSpreadsheet(ss).onFormSubmit()
      .create();
}

/**
 * Creates a Google Calendar with events for each conference session in the
 * spreadsheet, then writes the event IDs to the spreadsheet for future use.
 * @param {Array<string[]>} values Cell values for the spreadsheet range.
 * @param {Range} range A spreadsheet range that contains conference data.
 */
function setUpCalendar_(values, range) {
  let cal = CalendarApp.createCalendar('Conference Calendar');
  // Start at 1 to skip the header row.
  for (let i = 1; i < values.length; i++) {
    let session = values[i];
    let title = session[0];
    let start = joinDateAndTime_(session[1], session[2]);
    let end = joinDateAndTime_(session[1], session[3]);
    let options = {location: session[4], sendInvites: true};
    let event = cal.createEvent(title, start, end, options)
        .setGuestsCanSeeGuests(false);
    session[5] = event.getId();
  }
  range.setValues(values);

  // Stores the ID for the Calendar, which is needed to retrieve events by ID.
  let scriptProperties = PropertiesService.getScriptProperties();
  scriptProperties.setProperty('calId', cal.getId());
}

/**
 * Creates a single Date object from separate date and time cells.
 *
 * @param {Date} date A Date object from which to extract the date.
 * @param {Date} time A Date object from which to extract the time.
 * @return {Date} A Date object representing the combined date and time.
 */
function joinDateAndTime_(date, time) {
  date = new Date(date);
  date.setHours(time.getHours());
  date.setMinutes(time.getMinutes());
  return date;
}

/**
 * Creates a Google Form that allows respondents to select which conference
 * sessions they would like to attend, grouped by date and start time in the
 * caller's time zone.
 *
 * @param {Spreadsheet} ss The spreadsheet that contains the conference data.
 * @param {Array<String[]>} values Cell values for the spreadsheet range.
 */
function setUpForm_(ss, values) {
  // Group the sessions by date and time so that they can be passed to the form.
  let schedule = {};
  // Start at 1 to skip the header row.
  for (let i = 1; i < values.length; i++) {
    let session = values[i];
    let day = session[1].toLocaleDateString();
    let time = session[2].toLocaleTimeString();
    if (!schedule[day]) {
      schedule[day] = {};
    }
    if (!schedule[day][time]) {
      schedule[day][time] = [];
    }
    schedule[day][time].push(session[0]);
  }

  // Creates the form and adds a multiple-choice question for each timeslot.
  let form = FormApp.create('Conference Form');
  form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId());
  form.addTextItem().setTitle('Name').setRequired(true);
  form.addTextItem().setTitle('Email').setRequired(true);
  Object.keys(schedule).forEach(function(day) {
    let header = form.addSectionHeaderItem().setTitle('Sessions for ' + day);
    Object.keys(schedule[day]).forEach(function(time) {
      let item = form.addMultipleChoiceItem().setTitle(time + ' ' + day)
          .setChoiceValues(schedule[day][time]);
    });
  });
}

/**
 * Sends out calendar invitations and a
 * personalized Google Docs itinerary after a user responds to the form.
 *
 * @param {Object} e The event parameter for form submission to a spreadsheet;
 *     see https://developers.google.com/apps-script/understanding_events
 */
function onFormSubmit(e) {
  let user = {name: e.namedValues['Name'][0], email: e.namedValues['Email'][0]};

  // Grab the session data again so that we can match it to the user's choices.
  let response = [];
  let values = SpreadsheetApp.getActive().getSheetByName('Conference Setup')
      .getDataRange().getValues();
  for (let i = 1; i < values.length; i++) {
    let session = values[i];
    let title = session[0];
    let day = session[1].toLocaleDateString();
    let time = session[2].toLocaleTimeString();
    let timeslot = time + ' ' + day;

    // For every selection in the response, find the matching timeslot and title
    // in the spreadsheet and add the session data to the response array.
    if (e.namedValues[timeslot] && e.namedValues[timeslot] == title) {
      response.push(session);
    }
  }
  sendInvites_(user, response);
  sendDoc_(user, response);
}

/**
 * Add the user as a guest for every session he or she selected.
 * @param {object} user An object that contains the user's name and email.
 * @param {Array<String[]>} response An array of data for the user's session choices.
 */
function sendInvites_(user, response) {
  let id = ScriptProperties.getProperty('calId');
  let cal = CalendarApp.getCalendarById(id);
  for (let i = 0; i < response.length; i++) {
    cal.getEventSeriesById(response[i][5]).addGuest(user.email);
  }
}

/**
 * Creates and shares a personalized Google Doc that shows the user's itinerary.
 * @param {object} user An object that contains the user's name and email.
 * @param {Array<string[]>} response An array of data for the user's session choices.
 */
function sendDoc_(user, response) {
  let doc = DocumentApp.create('Conference Itinerary for ' + user.name)
      .addEditor(user.email);
  let body = doc.getBody();
  let table = [['Session', 'Date', 'Time', 'Location']];
  for (let i = 0; i < response.length; i++) {
    table.push([response[i][0], response[i][1].toLocaleDateString(),
      response[i][2].toLocaleTimeString(), response[i][4]]);
  }
  body.insertParagraph(0, doc.getName())
      .setHeading(DocumentApp.ParagraphHeading.HEADING1);
  table = body.appendTable(table);
  table.getRow(0).editAsText().setBold(true);
  doc.saveAndClose();

  // Emails a link to the Doc as well as a PDF copy.
  MailApp.sendEmail({
    to: user.email,
    subject: doc.getName(),
    body: 'Thanks for registering! Here\'s your itinerary: ' + doc.getUrl(),
    attachments: doc.getAs(MimeType.PDF),
  });
}

/**
 * Removes the calId script property so that the 'setUpConference_()' can be run again.
 */
function resetProperties(){
  let scriptProperties = PropertiesService.getScriptProperties();
  scriptProperties.deleteAllProperties();
}

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

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

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