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

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

目標

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

認識這項解決方案

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

建立員工證

運作方式

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

Apps Script 服務

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

  • 雲端硬碟服務:複製簡報員工證範本。
  • 試算表服務:提供員工詳細資料,並針對每位列出的員工更新狀態。
  • 簡報服務:將簡報中的預留位置替換為試算表中的員工資料。
  • Gmail 服務:取得個別投影片面並轉換為 PDF,再傳送給員工。

必要條件

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

  • 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 同意畫面顯示「This app isn't verified」警告,請依序選取「Advanced」「Go to {Project Name} (unsafe)」(前往「{Project Name}」(不安全))。

  3. 依序按一下「感謝」「建立證書」

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

貢獻者

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

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

後續步驟