برای کارمندان گواهی های قدردانی شخصی ارسال کنید

سطح کدنویسی : مبتدی
مدت زمان : 15 دقیقه
نوع پروژه : اتوماسیون با منوی سفارشی

اهداف

  • درک کنید که راه حل چه کاری انجام می دهد.
  • آنچه را که سرویس های Apps Script در راه حل انجام می دهند، بدانید.
  • محیط خود را تنظیم کنید.
  • اسکریپت را تنظیم کنید
  • اسکریپت را اجرا کنید.

در مورد این راه حل

الگوی گواهی کارمند اسلایدهای Google را به صورت خودکار با داده های کارمندان در Google Sheets سفارشی کنید، سپس گواهی ها را با استفاده از Gmail ارسال کنید.

ایجاد گواهینامه کارمند

چگونه کار می کند

این اسکریپت از الگوی ارائه گواهی کارمند از اسلایدها و یک صفحه گسترده Sheets با جزئیات کارمند استفاده می کند. اسکریپت الگو را کپی می‌کند و جای‌بان‌ها را با داده‌های صفحه‌گسترده جایگزین می‌کند. هنگامی که اسکریپت یک اسلاید برای هر کارمند ایجاد می کند، هر اسلاید جداگانه را به عنوان پیوست PDF استخراج می کند و گواهی ها را برای کارمندان ارسال می کند.

خدمات اسکریپت برنامه ها

این راه حل از خدمات زیر استفاده می کند:

  • سرویس Drive - الگوی گواهی کارمند اسلایدها را کپی می کند.
  • سرویس صفحه گسترده - جزئیات کارمند را ارائه می دهد و وضعیت را در برابر هر کارمند فهرست شده به روز می کند.
  • سرویس اسلایدها - جای جایگیرها را در ارائه با داده های کارمند از صفحه گسترده جایگزین می کند.
  • سرویس Gmail – اسلایدهای فردی را به صورت PDF دریافت کرده و برای کارمندان ارسال می کند.

پیش نیازها

برای استفاده از این نمونه به پیش نیازهای زیر نیاز دارید:

  • یک حساب Google (حساب‌های Google Workspace ممکن است به تأیید سرپرست نیاز داشته باشند).
  • یک مرورگر وب با دسترسی به اینترنت.

محیط خود را تنظیم کنید

  1. برای ایجاد یک کپی از الگوی اسلایدهای گواهینامه های کارمند ، روی دکمه زیر کلیک کنید.
    یک کپی تهیه کنید

  2. شناسه ارائه خود را برای استفاده در مرحله بعد یادداشت کنید. شما می توانید شناسه را در URL پیدا کنید:

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

  3. در Drive، یک پوشه جدید برای نگهداری گواهی ها ایجاد کنید.

  4. شناسه پوشه خود را برای استفاده در مرحله بعد یادداشت کنید. می‌توانید شناسه را در URL پیدا کنید: https://drive.google.com/drive/folders/ FOLDER_ID

اسکریپت را تنظیم کنید

  1. روی دکمه زیر کلیک کنید تا از صفحه گسترده نمونه گواهینامه های کارمند کپی کنید. پروژه Apps Script برای این راه حل به صفحه گسترده پیوست شده است.
    یک کپی تهیه کنید

  2. در صفحه گسترده، پروژه Apps Script را با کلیک روی Extensions > Apps Script باز کنید.

  3. برای متغیر slideTemplateId ، PRESENTATION_ID را جایگزین کنید PRESENTATION_ID با شناسه ارائه شما

  4. برای متغیر tempFolderId ، FOLDER_ID جایگزین کنید FOLDER_ID با شناسه پوشه شما

  5. روی ذخیره کلیک کنید نماد ذخیره .

اسکریپت را اجرا کنید

  1. به صفحه‌گسترده برگردید و روی قدردانی > ایجاد گواهی‌ها کلیک کنید. ممکن است لازم باشد صفحه را بازخوانی کنید تا این منوی سفارشی ظاهر شود.
  2. وقتی از شما خواسته شد، اسکریپت را مجاز کنید. اگر صفحه رضایت OAuth اخطار را نشان می‌دهد، این برنامه تأیید نشده است ، با انتخاب پیشرفته > رفتن به {Project Name} (ناامن) ادامه دهید.

  3. روی قدردانی > ایجاد گواهی دوباره کلیک کنید.

  4. هنگامی که ستون وضعیت همه ردیف‌ها به Created به‌روزرسانی شد، روی قدردانی > ارسال گواهی‌ها کلیک کنید.

کد را مرور کنید

برای بررسی کد 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();
  }
}

مشارکت کنندگان

این نمونه توسط Sourabh Choraria، وبلاگ نویس و متخصص توسعه دهنده گوگل ایجاد شده است.

این نمونه توسط گوگل با کمک کارشناسان توسعه دهنده گوگل نگهداری می شود.

مراحل بعدی