カスタマイズした感謝証明書を従業員に送信する

コーディング レベル: 初級
所要時間: 15 分
プロジェクト タイプ: カスタム メニューを使用した自動化

目標

  • ソリューションの機能を理解します。
  • ソリューション内で Apps Script サービスが何を行うかを理解します。
  • 環境をセットアップする。
  • スクリプトを設定します。
  • スクリプトを実行します。

このソリューションについて

Google スプレッドシートの従業員データを使用して Google スライドの従業員証明書テンプレートを自動的にカスタマイズし、Gmail を使用して証明書を送信します。

従業員証明書の作成

仕組み

このスクリプトでは、スライドの社員証明書プレゼンテーション テンプレートと、社員の詳細が記載されたスプレッドシートを使用します。スクリプトはテンプレートをコピーし、プレースホルダをスプレッドシートのデータに置き換えます。スクリプトは、従業員ごとにスライドを作成すると、各スライドを PDF 添付ファイルとして抽出し、従業員に証明書を送信します。

Apps Script サービス

このソリューションでは、次のサービスを使用します。

  • Drive サービス - スライドの社員証明書テンプレートをコピーします。
  • スプレッドシート サービス - 従業員の詳細を提供し、リストに表示された各従業員のステータスを更新します。
  • スライド サービス - スプレッドシートの従業員データを使用して、プレゼンテーションのプレースホルダを置き換えます。
  • Gmail サービス - 個々のスライドを PDF として取得し、従業員に送信します。

前提条件

このサンプルを使用するには、次の前提条件を満たしている必要があります。

  • Google アカウント(Google Workspace アカウントの場合、管理者の承認が必要となる可能性があります)。
  • インターネットにアクセスできるウェブブラウザ。

環境の設定

  1. 次のボタンをクリックして、社員証明書スライド テンプレートのコピーを作成します。
    コピーを作成

  2. 後の手順で使用するため、プレゼンテーション ID をメモしておきます。ID は URL で確認できます。

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

  3. ドライブに、証明書を保存する新しいフォルダを作成します。

  4. フォルダ ID をメモします。この 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 コードを確認するには、下の [ソースコードを表示] をクリックします。

ソースコードを表示

コード.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 が管理しています。

次のステップ