สร้างกําหนดการสําหรับการประชุม

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

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

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

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

สร้างเอกสารระเบียบวาระการประชุมใน Google เอกสารโดยอัตโนมัติและแนบไปกับการประชุมใน Google ปฏิทิน

ภาพหน้าจอของกำหนดการที่เพิ่มลงในกิจกรรมในปฏิทิน

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

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

บริการ Apps Script

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

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

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

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

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

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

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

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

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

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

ตรวจสอบโค้ด

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

ดูซอร์สโค้ด

Code.gs

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

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

/**
 * Checks if the folder for Agenda docs exists, and creates it if it doesn't.
 *
 * @return {*} Drive folder ID for the app.
 */
function checkFolder() {
  const folders = DriveApp.getFoldersByName('Agenda Maker - App');
  // Finds the folder if it exists
  while (folders.hasNext()) {
    let folder = folders.next();
    if (
      folder.getDescription() ==
        'Apps Script App - Do not change this description' &&
      folder.getOwner().getEmail() == Session.getActiveUser().getEmail()
    ) {
      return folder.getId();
    }
  }
  // If the folder doesn't exist, creates one
  let folder = DriveApp.createFolder('Agenda Maker - App');
  folder.setDescription('Apps Script App - Do not change this description');
  return folder.getId();
}

/**
 * Finds the template agenda doc, or creates one if it doesn't exist.
 */
function getTemplateId(folderId) {
  const folder = DriveApp.getFolderById(folderId);
  const files = folder.getFilesByName('Agenda TEMPLATE##');

  // If there is a file, returns the ID.
  while (files.hasNext()) {
    const file = files.next();
    return file.getId();
  }

  // Otherwise, creates the agenda template.
  // You can adjust the default template here
  const doc = DocumentApp.create('Agenda TEMPLATE##');
  const body = doc.getBody();

  body
      .appendParagraph('##Attendees##')
      .setHeading(DocumentApp.ParagraphHeading.HEADING1)
      .editAsText()
      .setBold(true);
  body.appendParagraph(' ').editAsText().setBold(false);

  body
      .appendParagraph('Overview')
      .setHeading(DocumentApp.ParagraphHeading.HEADING1)
      .editAsText()
      .setBold(true);
  body.appendParagraph(' ');
  body.appendParagraph('- Topic 1: ').editAsText().setBold(true);
  body.appendParagraph(' ').editAsText().setBold(false);
  body.appendParagraph('- Topic 2: ').editAsText().setBold(true);
  body.appendParagraph(' ').editAsText().setBold(false);
  body.appendParagraph('- Topic 3: ').editAsText().setBold(true);
  body.appendParagraph(' ').editAsText().setBold(false);

  body
      .appendParagraph('Next Steps')
      .setHeading(DocumentApp.ParagraphHeading.HEADING1)
      .editAsText()
      .setBold(true);
  body.appendParagraph('- Takeaway 1: ').editAsText().setBold(true);
  body.appendParagraph('- Responsible: ').editAsText().setBold(false);
  body.appendParagraph('- Accountable: ');
  body.appendParagraph('- Consult: ');
  body.appendParagraph('- Inform: ');
  body.appendParagraph(' ');
  body.appendParagraph('- Takeaway 2: ').editAsText().setBold(true);
  body.appendParagraph('- Responsible: ').editAsText().setBold(false);
  body.appendParagraph('- Accountable: ');
  body.appendParagraph('- Consult: ');
  body.appendParagraph('- Inform: ');
  body.appendParagraph(' ');
  body.appendParagraph('- Takeaway 3: ').editAsText().setBold(true);
  body.appendParagraph('- Responsible: ').editAsText().setBold(false);
  body.appendParagraph('- Accountable: ');
  body.appendParagraph('- Consult: ');
  body.appendParagraph('- Inform: ');

  doc.saveAndClose();

  folder.addFile(DriveApp.getFileById(doc.getId()));

  return doc.getId();
}

/**
 * When there is a change to the calendar, searches for events that include "#agenda"
 * in the decrisption.
 *
 */
function onCalendarChange() {
  // Gets recent events with the #agenda tag
  const now = new Date();
  const events = CalendarApp.getEvents(
      now,
      new Date(now.getTime() + 2 * 60 * 60 * 1000000),
      {search: '#agenda'},
  );

  const folderId = checkFolder();
  const templateId = getTemplateId(folderId);

  const folder = DriveApp.getFolderById(folderId);

  // Loops through any events found
  for (i = 0; i < events.length; i++) {
    const event = events[i];

    // Confirms whether the event has the #agenda tag
    let description = event.getDescription();
    if (description.search('#agenda') == -1) continue;

    // Only works with events created by the owner of this calendar
    if (event.isOwnedByMe()) {
      // Creates a new document from the template for an agenda for this event
      const newDoc = DriveApp.getFileById(templateId).makeCopy();
      newDoc.setName('Agenda for ' + event.getTitle());

      const file = DriveApp.getFileById(newDoc.getId());
      folder.addFile(file);

      const doc = DocumentApp.openById(newDoc.getId());
      const body = doc.getBody();

      // Fills in the template with information about the attendees from the
      // calendar event
      const conf = body.findText('##Attendees##');
      if (conf) {
        const ref = conf.getStartOffset();

        for (let i in event.getGuestList()) {
          let guest = event.getGuestList()[i];

          body.insertParagraph(ref + 2, guest.getEmail());
        }
        body.replaceText('##Attendees##', 'Attendees');
      }

      // Replaces the tag with a link to the agenda document
      const agendaUrl = 'https://docs.google.com/document/d/' + newDoc.getId();
      description = description.replace(
          '#agenda',
          '<a href=' + agendaUrl + '>Agenda Doc</a>',
      );
      event.setDescription(description);

      // Invites attendees to the Google doc so they automatically receive access to the agenda
      newDoc.addEditor(newDoc.getOwner());

      for (let i in event.getGuestList()) {
        let guest = event.getGuestList()[i];

        newDoc.addEditor(guest.getEmail());
      }
    }
  }
  return;
}

/**
 * Creates an event-driven trigger that fires whenever there's a change to the calendar.
 */
function setUp() {
  let email = Session.getActiveUser().getEmail();
  ScriptApp.newTrigger("onCalendarChange").forUserCalendar(email).onEventUpdated().create();
}

การปรับเปลี่ยน

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

อัปเดตสิทธิ์ในเอกสารระเบียบวาระสำหรับผู้เข้าร่วม

สคริปต์จะให้สิทธิ์แก่ผู้เข้าร่วมในการแก้ไข หากต้องการ จำกัดสิทธิ์ให้ดูเท่านั้น ให้แทนที่เมธอด addEditor ด้วยเมธอด addViewer ในส่วนต่อไปนี้ของโค้ด

     for (let i in event.getGuestList()) {
       let guest = event.getGuestList()[i];

       newDoc.addEditor(guest.getEmail());

แก้ไขเทมเพลตเอกสารระเบียบวาระ

หากต้องการอัปเดตเทมเพลตเอกสารระเบียบวาระ ให้ทำตามขั้นตอนต่อไปนี้

  1. หลังจากสร้างระเบียบวาระแรกในกิจกรรมในปฏิทินแล้ว ให้เปิด Google ไดรฟ์
  2. เปิดโฟลเดอร์ชื่อ Agenda Maker - App
  3. เปิดเอกสารเทมเพลตวาระการประชุม## แล้วทำการแก้ไข

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

ตัวอย่างนี้สร้างโดย Jeremy Glassenberg ที่ปรึกษาด้านการจัดการผลิตภัณฑ์และกลยุทธ์แพลตฟอร์ม ติดตาม Jeremy ได้ที่ Twitter @jglassenberg

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

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