Tworzenie planu spotkań

Poziom kodowania: początkujący
Czas trwania: 15 minut
Typ projektu: automatyzacja z regułą opartą na zdarzeniach

Cele

  • Poznaj działanie danego rozwiązania.
  • Dowiedz się, jak usługi Apps Script w danym rozwiązaniu działają.
  • Skonfiguruj skrypt.
  • Uruchom skrypt.

Informacje o rozwiązaniu

Automatycznie twórz dokumenty planów w Dokumentach Google i dołączaj je do spotkań w Kalendarzu Google.

Zrzut ekranu z planem spotkania dodanym do wydarzenia w Kalendarzu

Jak to działa

Skrypt tworzy szablon dokumentu planu spotkania. Gdy aktualizujesz kalendarz, skrypt sprawdza, czy któreś z Twoich wydarzeń zawierają w opisie ciąg „#agenda”. Jeśli tag jest obecny, skrypt tworzy kopię szablonu, dodaje go do wydarzenia w kalendarzu i udostępnia uczestnikom wydarzenia.

Usługi Apps Script

W tym rozwiązaniu są używane następujące usługi:

  • Usługa Dysku – sprawdza, czy dokument szablonu istnieje. Jeśli nie, tworzy nowy folder dla dokumentu szablonu. Tworzy kopię szablonu dokumentu dla każdego nowego planu spotkania.
  • Usługa dokumentów – tworzy szablon planu dnia.
  • Usługa kalendarza – wyszukuje wydarzenia za pomocą tagu „#agenda” i aktualizuje opis wydarzenia o link do dokumentu z planem dnia.
  • Usługa podstawowa – używa klasy Session do pobierania adresu e-mail użytkownika. Pomaga to utworzyć regułę dla bieżącego użytkownika.
  • Usługa skryptu – tworzy regułę, która uruchamia się, gdy w kalendarzu użytkownika zachodzi zmiana.

Wymagania wstępne

Aby korzystać z tego przykładu, musisz spełnić te wymagania wstępne:

  • konta Google (konta Google Workspace mogą wymagać zatwierdzenia przez administratora),
  • Przeglądarka z dostępem do internetu.

Konfigurowanie skryptu

  1. Kliknij przycisk poniżej, aby otworzyć przykładowy projekt Tworzenie planu spotkań Apps Script.
    Otwórz projekt
  2. Kliknij Przegląd .
  3. Na stronie przeglądu kliknij Utwórz kopię Ikona tworzenia kopii.
  4. W skopiowanym projekcie z menu funkcji wybierz setUp.
  5. Kliknij Uruchom.
  6. Gdy pojawi się odpowiedni komunikat, autoryzuj skrypt. Jeśli na ekranie zgody OAuth pojawi się ostrzeżenie Ta aplikacja nie jest zweryfikowana, wybierz Zaawansowane > Otwórz {Project Name} (niebezpieczny).

Uruchom skrypt

  1. Otwórz Kalendarz Google.
  2. Utwórz nowe wydarzenie lub edytuj istniejące.
  3. W opisie dodaj #agenda i zapisz wydarzenie.
  4. Sprawdź, czy dotarł e-mail z powiadomieniem o udostępnieniu Ci dokumentu. Możesz też odświeżyć Kalendarz i ponownie kliknąć wydarzenie, aby zobaczyć link do dokumentu z planem.

Wszyscy uczestnicy otrzymają e-maila z powiadomieniem o dostępie do planu spotkania. Skrypt daje uczestnikom uprawnienia do edytowania, ale możesz go edytować, aby zaktualizować uprawnienia uczestników w dokumencie planu dnia.

Sprawdź kod

Aby sprawdzić kod Apps Script tego rozwiązania, kliknij Wyświetl kod źródłowy poniżej:

Pokaż kod źródłowy

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();
}

Modyfikacje

Próbkę możesz edytować według swoich potrzeb. Poniżej znajdziesz kilka opcjonalnych zmian, które możesz wprowadzić.

Aktualizowanie uprawnień uczestników do dokumentu planu dnia

Skrypt daje uczestnikom uprawnienia do edytowania. Jeśli chcesz ograniczyć uprawnienia tylko do wyświetlania, zastąp metodę addEditor metodą addViewer w tej części kodu:

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

       newDoc.addEditor(guest.getEmail());

Edytowanie szablonu dokumentu planu spotkania

Aby zaktualizować szablon dokumentu planu spotkania, wykonaj te czynności:

  1. Po utworzeniu pierwszego planu spotkania w wydarzeniu w kalendarzu otwórz Dysk Google.
  2. Otwórz folder Agenda Maker – App.
  3. Otwórz dokument Plan dnia TEMPLATE## i wprowadź zmiany.

Współtwórcy

Autorem tego fragmentu jest Jeremy Glassenberg, konsultant ds. zarządzania usługami i strategii platformy. Znajdź Jeremy'ego na Twitterze @jglassenberg.

Ta próbka jest udostępniana przez Google z pomocą Ekspertów Produktowych Google.

Dalsze kroki