Tạo đơn đăng ký tham gia các phiên tại hội nghị

Cấp độ lập trình: Cơ bản
Thời lượng: 5 phút
Loại dự án: Tự động hoá bằng trình đơn tuỳ chỉnhđiều kiện kích hoạt dựa trên sự kiện

Mục tiêu

  • Tìm hiểu tác dụng của giải pháp.
  • Tìm hiểu chức năng của dịch vụ Apps Script trong Cloud.
  • Thiết lập tập lệnh.
  • Chạy tập lệnh.

Giới thiệu về giải pháp này

Tạo hệ thống đăng ký sự kiện toàn diện. Nếu bạn có một sự kiện sắp tới, chẳng hạn như một hội nghị, bạn có thể thiết lập lịch mới cho hội nghị truyền hình phiên hoạt động, tạo biểu mẫu đăng ký và tự động gửi email cho từng người tham dự hành trình.

Thông tin trong Trang tính được chuyển sang Biểu mẫu và Lịch

Cách hoạt động

Giải pháp này sử dụng trình đơn tuỳ chỉnh trong Google Trang tính để triển khai hệ thống đăng ký sự kiện. Kịch bản tạo lịch với hội nghị truyền hình các sự kiện được liệt kê trong bảng tính Trang tính. Sau đó, tập lệnh sẽ tạo biểu mẫu có danh sách các sự kiện mà người tham dự có thể đăng ký. Sau người tham dự điền vào biểu mẫu, tập lệnh thêm người tham dự vào sự kiện trên lịch và gửi hành trình qua email đến họ.

Dịch vụ Apps Script

Giải pháp này sử dụng các dịch vụ sau:

  • Dịch vụ bảng tính–Cung cấp thông tin sự kiện sang các dịch vụ khác.
  • Dịch vụ Lịch–Tạo một lịch mới cho sự kiện, thêm sự kiện vào lịch và thêm người tham dự vào các sự kiện mà họ đăng ký.
  • Dịch vụ cơ sở lưu trú – Lưu trữ Mã của lịch do dịch vụ Lịch tạo. Khi một người dùng nhấp vào Thiết lập hội nghị từ trình đơn Hội nghị tùy chỉnh, Dịch vụ thuộc tính kiểm tra xem hệ thống đăng ký sự kiện đã kiểm tra xem bằng cách kiểm tra xem có thuộc tính mã lịch hay không. Làm như vậy sẽ hữu ích để tránh tạo các biểu mẫu và lịch trùng lặp.
  • Dịch vụ Biểu mẫu – Tạo biểu mẫu từ thông tin trong bảng tính để người tham dự có thể đăng ký phiên hoạt động.
  • Dịch vụ tập lệnh – Tạo một điều kiện kích hoạt sẽ kích hoạt khi một người tham dự điền vào biểu mẫu.
  • Dịch vụ tài liệu–Nhận sự kiện về các sự kiện mà người tham dự đăng ký và thêm danh sách sang một tài liệu mới. Tập lệnh cấp cho người tham dự quyền chỉnh sửa tài liệu.
  • Dịch vụ gửi qua thư – Gửi tài liệu hành trình qua email đến người tham dự.

Điều kiện tiên quyết

Để sử dụng mẫu này, bạn cần có các điều kiện tiên quyết sau đây:

  • Tài khoản Google (Tài khoản Google Workspace có thể yêu cầu quản trị viên phê duyệt).
  • Một trình duyệt web có quyền truy cập vào Internet.

Thiết lập tập lệnh

  1. Nhấp vào nút sau để tạo bản sao Tạo thông tin đăng ký cho tại một hội nghị bảng tính mẫu. Apps Script dự án cho giải pháp này được đính kèm vào bảng tính.
    Tạo bản sao
  2. Nhấp vào Hội nghị > Thiết lập hội nghị truyền hình. Có thể bạn cần phải làm mới trang cho trình đơn tuỳ chỉnh này để xuất hiện.
  3. Khi được nhắc, hãy cho phép tập lệnh. Nếu màn hình xin phép bằng OAuth cho thấy cảnh báo, tức là Ứng dụng này chưa được xác minh, tiếp tục bằng cách chọn Nâng cao > Truy cập {Project Name} (không an toàn).

  4. Nhấp vào Hội nghị > Thiết lập .

Chạy tập lệnh

  1. Nhấp vào Công cụ > Quản lý biểu mẫu > Chuyển tới biểu mẫu trực tiếp.
  2. Điền thông tin rồi gửi biểu mẫu.
  3. Truy cập vào calendar.google.com.
  4. Ở bên trái, hãy nhớ chọn hộp bên cạnh Lịch hội nghị.
  5. Chuyển đến ngày diễn ra những sự kiện mà bạn đã đăng ký và xác nhận rằng bạn đã được thêm vào với tư cách là người tham dự.

(Không bắt buộc) Đặt lại giải pháp

Nếu bạn muốn thử lại hoặc tuỳ chỉnh giải pháp này để sử dụng thông tin sự kiện của riêng bạn, bạn cần đặt lại một số mục đã được thiết lập khi bạn chạy tập lệnh lần đầu tiên. Để xem các bước đặt lại giải pháp, hãy nhấp vào Đặt lại giải pháp dưới đây:

Đặt lại giải pháp

Bước 1: Đặt lại thuộc tính của tập lệnh đã lưu trữ

Nếu muốn chạy tập lệnh nhiều lần, bạn sẽ được nhắc hội nghị truyền hình đã được thiết lập. Hãy tìm biểu mẫu đăng ký của bạn trong Google Drive! Điều này xảy ra vì sau khi Lịch hội nghị được tạo, mã lịch được lưu trữ dưới dạng thuộc tính của tập lệnh. Khi chạy, tập lệnh sẽ kiểm tra xem thuộc tính mã lịch đã tồn tại và ngừng chạy nếu thuộc tính này ngừng chạy.

Hãy làm theo các bước dưới đây để xoá tài sản mã lịch hiện có:

  1. Trong bảng tính, hãy nhấp vào Tiện ích > Apps Script.
  2. Trong trình chỉnh sửa Apps Script, hãy chọn resetProperties trong danh sách thả xuống chứa hàm rồi nhấp vào Run (Chạy).

Bước 2: Xoá Lịch hội nghị truyền hình

Mỗi khi tập lệnh chạy, tập lệnh sẽ tạo một lịch mới. Nếu bạn không muốn giữ lại lịch gốc đã tạo, hãy làm theo các bước sau:

  1. Truy cập vào calendar.google.com.
  2. Bên cạnh Lịch hội nghị truyền hình, nhấp vào Tùy chọn cho Lịch hội nghị truyền hình > Cài đặt và chia sẻ.
  3. Di chuyển xuống cuối phần cài đặt rồi nhấp vào Xoá.

Bước 3: Xoá điều kiện kích hoạt gửi biểu mẫu

Tập lệnh sẽ tạo điều kiện kích hoạt để gửi biểu mẫu mỗi khi bạn chạy tập lệnh đó. Người nhận tránh nhiều điều kiện kích hoạt dẫn đến email trùng lặp, hãy loại bỏ email gốc điều kiện kích hoạt. Hãy làm theo các bước sau:

  1. Trong bảng tính, hãy nhấp vào Tiện ích > Apps Script.
  2. Trong dự án Apps Script, hãy nhấp vào Trình kích hoạt ở bên trái .
  3. Bên cạnh điều kiện kích hoạt, hãy nhấp vào biểu tượng Thêm > Xoá điều kiện kích hoạt.

Mỗi lần chạy tập lệnh, bạn sẽ tạo một biểu mẫu mới. Hãy làm theo các bước sau để huỷ liên kết biểu mẫu khỏi bảng tính của bạn và xoá biểu mẫu đó:

  1. Trong bảng tính, hãy nhấp chuột phải vào trang tính Câu trả lời qua biểu mẫu rồi nhấp vào Huỷ liên kết biểu mẫu > Ok.
  2. Nhấp chuột phải vào trang tính Câu trả lời biểu mẫu một lần nữa rồi nhấp vào Xoá > Ok.
  3. Truy cập vào trang forms.google.com.
  4. Nhấp chuột phải vào Biểu mẫu hội nghị rồi nhấp vào Xoá > Chuyển vào thùng rác.

Sau khi đặt lại giải pháp, bạn có thể thêm dữ liệu của riêng mình hoặc tiếp tục hãy sử dụng dữ liệu mẫu và chạy lại tập lệnh.

Xem lại đoạn mã

Để xem mã Apps Script cho giải pháp này, hãy nhấp vào Xem mã nguồn ở bên dưới:

Xem mã nguồn

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

Người đóng góp

Mẫu này được Google duy trì với sự trợ giúp của Chuyên gia nhà phát triển của Google.

Các bước tiếp theo