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

编码水平:初级
时长:15 分钟
项目类型:使用自定义菜单实现自动化

目标

  • 了解解决方案的功能。
  • 了解 Apps 脚本服务在解决方案中的作用。
  • 设置环境。
  • 设置脚本。
  • 运行脚本。

关于此解决方案

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

员工证书创建

运作方式

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

Apps 脚本服务

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

  • 云端硬盘服务 - 复制 Google 幻灯片员工证书模板。
  • 电子表格服务 - 提供员工详细信息,并针对列出的每位员工更新状态。
  • Slides 服务 - 使用电子表格中的员工数据替换演示文稿中的占位符。
  • 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 脚本项目已附加到电子表格。
    制作副本

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

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

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

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

运行脚本

  1. 切换回电子表格,然后依次点击表彰 > 创建证书。您可能需要刷新页面才能看到此自定义菜单。
  2. 根据提示为脚本授权。 如果 OAuth 权限请求页面显示警告此应用未经过验证,请继续操作,依次选择高级 > 前往“{项目名称}”(不安全)

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

  4. 当所有行的“状态”列都更新为 Created 后,依次点击感谢 > 发送证书

查看代码

如需查看此解决方案的 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 开发者专家的帮助下维护。

后续步骤