向员工发送个性化的感谢证书

编码级别:初级
时长:15 分钟
项目类型:通过自定义菜单实现自动化

目标

  • 了解解决方案的用途。
  • 了解 Apps 脚本服务在此解决方案中执行的操作。
  • 设置环境。
  • 设置脚本。
  • 运行脚本。

关于此解决方案

使用 Google 表格中的员工数据自动自定义 Google 幻灯片员工证书模板,然后使用 Gmail 发送证书。

创建员工证书

运作方式

该脚本使用 Google 幻灯片中的员工证书演示文稿模板和包含员工详细信息的 Google 表格电子表格。该脚本会复制模板并使用电子表格中的数据替换占位符。脚本为每个员工创建幻灯片后,会将每张幻灯片提取为 PDF 附件,并将证书发送给员工。

Apps 脚本服务

此解决方案使用以下服务:

  • Drive service - 复制 Google 幻灯片员工证书模板。
  • 电子表格服务 - 提供员工详细信息,并针对列出的每位员工更新状态。
  • 幻灯片服务 - 将演示文稿中的占位符替换为电子表格中的员工数据。
  • Gmail 服务 - 获取各张幻灯片的 PDF 文件,并将其发送给员工。

前提条件

如需使用此示例,您需要满足以下前提条件:

  • Google 帐号(Google Workspace 帐号可能需要管理员批准)。
  • 能够访问互联网的网络浏览器。

设置您的环境

  1. 点击以下按钮创建 Employee Certificate 幻灯片模板的副本。
    复制

  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 脚本项目已附加到该电子表格中。
    复制

  2. 在电子表格中,依次点击扩展程序 > Apps 脚本,打开 Apps 脚本项目。

  3. 对于 slideTemplateId 变量,请将 PRESENTATION_ID 替换为您的演示文稿的 ID。

  4. 对于 tempFolderId 变量,请将 FOLDER_ID 替换为您的文件夹的 ID。

  5. 点击“保存”“保存”图标

运行脚本

  1. 切换回电子表格,然后依次点击感谢 > 创建证书。您可能需要刷新页面才能看到此自定义菜单。
  2. 出现提示时,为脚本授权。 如果 OAuth 同意屏幕显示 This app is not verify 警告,请依次选择 Advanced > Go to {Project Name} (unsafe),以继续操作。

  3. 依次点击感谢 > 创建证书

  4. 所有行的状态列都更新为已创建后,依次点击感谢 > 发送证书

查看代码

要查看此解决方案的 Apps 脚本代码,请点击下面的查看源代码

查看源代码

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 开发者专家的帮助下进行维护。

后续步骤