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

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

目标

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

关于此解决方案

自动自定义 Google 幻灯片员工证书模板 处理员工数据,然后使用 Gmail。

创建员工证书

工作原理

该脚本使用来自 的员工证书演示文稿模板, 幻灯片和一张员工表格 。脚本会复制模板并 将占位符替换为电子表格中的数据。将脚本 为每位员工制作一张幻灯片,并将每张幻灯片提取为 PDF 格式 并将证书发送给员工。

Apps 脚本服务

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

  • 云端硬盘服务 - 复制 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. 点击下面的按钮,复制 Employeecertificate(员工证书)示例。 电子表格。此 API 的 Apps 脚本项目 解决方案。
    复制

  2. 在电子表格中,点击以下图标打开 Apps 脚本项目: 扩展程序 > Apps 脚本

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

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

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

运行脚本

  1. 切换回电子表格,然后点击感谢 > 创建证书。你可能需要 刷新页面即可显示此自定义菜单。
  2. 出现提示时,为脚本授权。 如果 OAuth 同意屏幕显示以下警告:“此应用未经验证”, 选择高级 > 以继续 前往“{Project Name}”(不安全)

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

后续步骤