직원에게 맞춤형 감사 인증서 보내기

코딩 수준: 초급
시간: 15분
프로젝트 유형: 맞춤 메뉴를 사용한 자동화

목표

  • 솔루션의 기능을 이해합니다.
  • 솔루션 내에서 Apps Script 서비스의 기능을 이해합니다.
  • 환경을 설정합니다.
  • 스크립트를 설정합니다.
  • 스크립트를 실행합니다.

이 솔루션 정보

Google Sheets의 직원 데이터로 Google Slides 직원 인증서 템플릿을 자동으로 맞춤설정한 다음 Gmail을 사용하여 인증서를 전송합니다.

직원 인증서 생성

사용 방법

이 스크립트는 직원 세부정보가 포함된 Slides와 Sheets 스프레드시트의 직원 인증서 프레젠테이션 템플릿을 사용합니다. 스크립트가 템플릿을 복사하고 자리표시자를 스프레드시트의 데이터로 바꿉니다. 스크립트가 모든 직원을 위한 슬라이드를 만들면 각 개별 슬라이드를 PDF 첨부파일로 추출하여 직원에게 인증서를 보냅니다.

Apps Script 서비스

이 솔루션은 다음 서비스를 사용합니다.

  • Drive 서비스: Slides 직원 인증서 템플릿을 복사합니다.
  • 스프레드시트 서비스 – 직원 세부정보를 제공하고 나열된 각 직원에 대한 상태를 업데이트합니다.
  • Slides 서비스: 프레젠테이션의 자리표시자를 스프레드시트의 직원 데이터로 대체합니다.
  • Gmail 서비스 – 개별 슬라이드를 PDF로 가져와 직원에게 보냅니다.

기본 요건

이 샘플을 사용하려면 다음과 같은 기본 요건이 필요합니다.

  • Google 계정 (Google Workspace 계정은 관리자 승인이 필요할 수 있음)
  • 인터넷 액세스가 가능한 웹브라우저

환경 설정

  1. 다음 버튼을 클릭하여 직원 인증서 Slides 템플릿의 사본을 만듭니다.
    사본 만들기

  2. 이후 단계에서 사용할 수 있도록 프레젠테이션 ID를 기록해 둡니다. ID는 URL에서 찾을 수 있습니다.

    https://docs.google.com/presentation/d/PRESENTATION_ID/edit

  3. Drive에서 인증서를 보관할 새 폴더를 만듭니다.

  4. 이후 단계에서 사용할 수 있도록 폴더 ID를 기록해 둡니다. ID는 URL에서 찾을 수 있습니다. 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. 스프레드시트로 다시 전환하고 감사 > 인증서 만들기를 클릭합니다. 이 맞춤 메뉴를 표시하려면 페이지를 새로고침해야 할 수 있습니다.
  2. 메시지가 표시되면 스크립트를 승인합니다. OAuth 동의 화면에 확인되지 않은 앱입니다라는 경고가 표시되면 고급 > {프로젝트 이름}(으)로 이동(안전하지 않음)을 선택하여 계속 진행합니다.

  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 Developer Expert인 Sourabh Choraria가 만들었습니다.

이 샘플은 Google Developer Experts의 도움으로 Google에서 관리합니다.

다음 단계