傳送個人化感謝證明給員工

程式設計程度:初學者
時間長度:15 分鐘
專案類型:使用自訂選單進行自動化

目標

  • 瞭解解決方案的功能。
  • 瞭解解決方案中的 Apps Script 服務功能。
  • 設定環境。
  • 設定指令碼。
  • 執行指令碼。

認識這項解決方案

自動使用 Google 試算表中的員工資料,自訂 Google 簡報的員工證書範本,然後透過 Gmail 傳送證書。

建立員工憑證

運作方式

這項指令碼會使用 Google 簡報的「員工證書」範本,以及包含員工詳細資料的 Google 試算表。指令碼會複製範本,並將預留位置替換為試算表中的資料。腳本為每位員工建立投影片後,會將每張投影片擷取為 PDF 附件,並將認證傳送給員工。

Apps Script 服務

這項解決方案會使用下列服務:

必要條件

如要使用這個範例,您必須符合下列先決條件:

  • Google 帳戶 (Google Workspace 帳戶可能需要管理員核准)。
  • 可連上網際網路的網路瀏覽器。

設定環境

  1. 點選下方按鈕,複製「員工認證」 簡報範本。
    建立副本

  2. 記下簡報 ID,後續步驟將會用到。您可以在網址中找到 ID:

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

  3. 在雲端硬碟中,建立新資料夾來存放認證。

  4. 記下資料夾 ID,以供後續步驟使用。您可以在網址中找到 ID: https://drive.google.com/drive/folders/FOLDER_ID

設定指令碼

  1. 點選下方按鈕,複製員工證書範例試算表。這項解決方案的 Apps Script 專案已附加至試算表。
    建立副本

  2. 在試算表中,依序點選「擴充功能」>「Apps Script」,開啟 Apps Script 專案。

  3. 針對 slideTemplateId 變數,請將 PRESENTATION_ID 替換為簡報 ID。

  4. 針對 tempFolderId 變數,請將 FOLDER_ID 替換為資料夾的 ID。

  5. 按一下「儲存」圖示 「儲存」圖示

執行指令碼

  1. 切換回試算表,然後依序點選「Appreciation」(感謝) >「Create certificates」(建立認證)。你可能需要重新整理頁面,才能看到這個自訂選單。
  2. 出現提示訊息時,請授權執行指令碼。 如果 OAuth 同意畫面顯示「這個應用程式未經驗證」警告,請依序選取「進階」>「前往『{專案名稱}』(不安全)」,繼續操作。

  3. 依序點選「Appreciation」(感謝) >「Create certificates」(建立認證)

  4. 所有資料列的狀態欄更新為「已建立」後,依序點選「感謝」>「傳送認證」

檢查程式碼

如要查看這項解決方案的 Apps Script 程式碼,請按一下下方的「查看原始碼」

查看原始碼

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

貢獻者

這個範例是由 Sourabh Choraria 建立,他是網誌作者兼 Google 開發人員專家。

這個範例由 Google 維護,並由 Google 開發人員專家協助。

後續步驟