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

程式設計層級:新手
時間長度:15 分鐘
專案類型:使用自訂選單進行自動化作業

目標

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

認識這項解決方案

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

建立員工憑證

運作方式

這個指令碼會使用投影片中的員工憑證簡報範本,以及含有員工詳細資料的試算表。指令碼會複製範本,並將預留位置替換為試算表中的資料。指令碼為每位員工建立一張投影片後,該指令碼會以 PDF 附件的形式擷取每張投影片,並將憑證傳送給員工。

Apps Script 服務

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

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

必要條件

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

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

設定環境

  1. 點選下方按鈕,建立「Employee credentials」(員工憑證) 簡報範本。
    建立副本

  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. 請點選下方按鈕,複製「Employee credentials」(員工憑證) 範例試算表。這個解決方案的 Apps Script 專案已附加到試算表。
    建立副本

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

  3. 針對 slideTemplateId 變數,請將 PRESENTATION_ID 替換成您的呈現 ID。

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

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

執行指令碼

  1. 切換回試算表,然後依序點選「Appreciation」>「Create credentials」(建立憑證)。您可能需要重新整理頁面,系統才會顯示這個自訂選單。
  2. 出現提示訊息時,請授權執行指令碼。如果 OAuth 同意畫面顯示警告「This app has not verification」(這個應用程式尚未驗證),請依序選取「Advanced」(進階) >「Go to {Project Name} (unsafe)」 (前往 {Project Name} (不安全))

  3. 再次依序點選「Appreciation」>「Create credentials」(建立憑證)

  4. 所有資料列的狀態欄都更新為「Created」(已建立) 後,請按一下「Appreciation」(Appreciation)>「Send credentials」(傳送憑證)

查看程式碼

如要查看這項解決方案的 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 Developer 專家建立。

本範例是由 Google 在 Google Developers 專家的協助下維護。

後續步驟