Gửi giấy chứng nhận tri ân dành riêng cho nhân viên

Cấp độ lập trình: Sơ cấp
Thời lượng: 15 phút
Loại dự án: Tự động hoá bằng trình đơn tuỳ chỉnh

Mục tiêu

  • Hiểu rõ chức năng của giải pháp.
  • Hiểu rõ chức năng của các dịch vụ Apps Script trong giải pháp.
  • Thiết lập môi trường.
  • 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ự động tuỳ chỉnh mẫu Giấy chứng nhận nhân viên trong Google Trang trình bày bằng dữ liệu nhân viên trong Google Trang tính, sau đó gửi giấy chứng nhận bằng Gmail.

Quy trình tự động tạo chứng chỉ cho nhân viên

Cách hoạt động

Tập lệnh sử dụng mẫu trang trình bày Giấy chứng nhận nhân viên trong Trang trình bày và một bảng tính Trang tính có thông tin chi tiết về nhân viên. Tập lệnh sao chép mẫu và thay thế các phần giữ chỗ bằng dữ liệu từ bảng tính. Sau khi tập lệnh tạo một trang trình bày cho mỗi nhân viên, tập lệnh sẽ trích xuất từng trang trình bày riêng lẻ dưới dạng tệp đính kèm PDF và gửi giấy chứng nhận cho nhân viên.

Dịch vụ Apps Script

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

  • Dịch vụ Google Drive: Sao chép mẫu Giấy chứng nhận nhân viên trong Trang trình bày.
  • Dịch vụ Trang tính: Cung cấp thông tin chi tiết về nhân viên và cập nhật trạng thái của từng nhân viên được liệt kê.
  • Dịch vụ Trang trình bày: Thay thế các phần giữ chỗ trong trang trình bày bằng dữ liệu nhân viên từ bảng tính.
  • Dịch vụ Gmail: Lấy các trang trình bày riêng lẻ dưới dạng tệp PDF và gửi cho nhân viên.

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

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

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

Thiết lập môi trường

  1. Nhấp vào nút sau để tạo bản sao của mẫu Trang trình bày Giấy chứng nhận nhân viên.

Tạo bản sao

  1. Ghi lại mã bản trình bày để sử dụng trong bước tiếp theo. Bạn có thể tìm thấy mã này trong URL:

    https://docs.google.com/presentation/d/PRESENTATION_ID/edit

  2. Trong Drive, hãy tạo một thư mục mới để lưu giữ giấy chứng nhận.

  3. Ghi lại mã thư mục để sử dụng trong bước tiếp theo. Bạn có thể tìm thấy mã này trong URL: https://drive.google.com/drive/folders/FOLDER_ID

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

  1. Nhấp vào nút sau để tạo bản sao của bảng tính Trang tính mẫu Giấy chứng nhận nhân viên. Dự án Apps Script cho giải pháp này được đính kèm với bảng tính:

    Tạo bản sao

  2. Trong bảng tính, hãy mở dự án Apps Script bằng cách nhấp vào Tiện ích mở rộng > Apps Script.

  3. Đối với biến slideTemplateId, hãy thay thế PRESENTATION_ID bằng mã của trang trình bày.

  4. Đối với biến tempFolderId, hãy thay thế FOLDER_ID bằng mã của thư mục.

  5. Nhấp vào biểu tượng Lưu Biểu tượng lưu trong trình chỉnh sửa tập lệnh Apps Script.

Chạy tập lệnh

  1. Chuyển lại bảng tính rồi nhấp vào Biểu dương > Tạo giấy chứng nhận. Bạn có thể cần làm mới trang để trình đơn tuỳ chỉnh này xuất hiện.
  2. Khi được nhắc, hãy cho phép tập lệnh chạy. <<../_snippets/oauth.md>>
  3. Nhấp lại vào Biểu dương > Tạo giấy chứng nhận.
  4. Sau khi cột trạng thái của tất cả các hàng được cập nhật thành Đã tạo, hãy nhấp vào Biểu dương > Gửi giấy chứng nhận.

Xem lại mã

Để xem lại mã Apps Script cho giải pháp này, hãy nhấp vào Xem mã nguồn:

Xem mã nguồn

Code.gs

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

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

const slideTemplateId = "PRESENTATION_ID";
const tempFolderId = "FOLDER_ID"; // Create an empty folder in Google Drive

/**
 * Creates a custom menu "Appreciation" in the spreadsheet
 * with drop-down options to create and send certificates
 */
function onOpen() {
  const ui = SpreadsheetApp.getUi();
  ui.createMenu("Appreciation")
    .addItem("Create certificates", "createCertificates")
    .addSeparator()
    .addItem("Send certificates", "sendCertificates")
    .addToUi();
}

/**
 * Creates a personalized certificate for each employee
 * and stores every individual Slides doc on Google Drive
 */
function createCertificates() {
  // Load the Google Slide template file
  const template = DriveApp.getFileById(slideTemplateId);

  // Get all employee data from the spreadsheet and identify the headers
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const values = sheet.getDataRange().getValues();
  const headers = values[0];
  const empNameIndex = headers.indexOf("Employee Name");
  const dateIndex = headers.indexOf("Date");
  const managerNameIndex = headers.indexOf("Manager Name");
  const titleIndex = headers.indexOf("Title");
  const compNameIndex = headers.indexOf("Company Name");
  const empEmailIndex = headers.indexOf("Employee Email");
  const empSlideIndex = headers.indexOf("Employee Slide");
  const statusIndex = headers.indexOf("Status");

  // Iterate through each row to capture individual details
  for (let i = 1; i < values.length; i++) {
    const rowData = values[i];
    const empName = rowData[empNameIndex];
    const date = rowData[dateIndex];
    const managerName = rowData[managerNameIndex];
    const title = rowData[titleIndex];
    const compName = rowData[compNameIndex];

    // Make a copy of the Slide template and rename it with employee name
    const tempFolder = DriveApp.getFolderById(tempFolderId);
    const empSlideId = template.makeCopy(tempFolder).setName(empName).getId();
    const empSlide = SlidesApp.openById(empSlideId).getSlides()[0];

    // Replace placeholder values with actual employee related details
    empSlide.replaceAllText("Employee Name", empName);
    empSlide.replaceAllText(
      "Date",
      `Date: ${Utilities.formatDate(
        date,
        Session.getScriptTimeZone(),
        "MMMM dd, yyyy",
      )}`,
    );
    empSlide.replaceAllText("Your Name", managerName);
    empSlide.replaceAllText("Title", title);
    empSlide.replaceAllText("Company Name", compName);

    // Update the spreadsheet with the new Slide Id and status
    sheet.getRange(i + 1, empSlideIndex + 1).setValue(empSlideId);
    sheet.getRange(i + 1, statusIndex + 1).setValue("CREATED");
    SpreadsheetApp.flush();
  }
}

/**
 * Send an email to each individual employee
 * with a PDF attachment of their appreciation certificate
 */
function sendCertificates() {
  // Get all employee data from the spreadsheet and identify the headers
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const values = sheet.getDataRange().getValues();
  const headers = values[0];
  const empNameIndex = headers.indexOf("Employee Name");
  const dateIndex = headers.indexOf("Date");
  const managerNameIndex = headers.indexOf("Manager Name");
  const titleIndex = headers.indexOf("Title");
  const compNameIndex = headers.indexOf("Company Name");
  const empEmailIndex = headers.indexOf("Employee Email");
  const empSlideIndex = headers.indexOf("Employee Slide");
  const statusIndex = headers.indexOf("Status");

  // Iterate through each row to capture individual details
  for (let i = 1; i < values.length; i++) {
    const rowData = values[i];
    const empName = rowData[empNameIndex];
    const date = rowData[dateIndex];
    const managerName = rowData[managerNameIndex];
    const title = rowData[titleIndex];
    const compName = rowData[compNameIndex];
    const empSlideId = rowData[empSlideIndex];
    const empEmail = rowData[empEmailIndex];

    // Load the employee's personalized Google Slide file
    const attachment = DriveApp.getFileById(empSlideId);

    // Setup the required parameters and send them the email
    const senderName = "CertBot";
    const subject = `${empName}, you're awesome!`;
    const body = `Please find your employee appreciation certificate attached.\n\n${compName} team`;
    GmailApp.sendEmail(empEmail, subject, body, {
      attachments: [attachment.getAs(MimeType.PDF)],
      name: senderName,
    });

    // Update the spreadsheet with email status
    sheet.getRange(i + 1, statusIndex + 1).setValue("SENT");
    SpreadsheetApp.flush();
  }
}
</section>

Người đóng góp

Mẫu này do Sourabh Choraria, blogger và Chuyên gia về nhà phát triển của Google tạo ra.

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

Các bước tiếp theo