|
| 1 | +// To learn how to use this script, refer to the documentation: |
| 2 | +// https://developers.google.com/apps-script/samples/automations/employee-certificate |
| 3 | + |
| 4 | +/* |
| 5 | +Copyright 2022 Google LLC |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +*/ |
| 19 | + |
| 20 | +let slideTemplateId = "PRESENTATION_ID"; |
| 21 | +let tempFolderId = "FOLDER_ID"; // Create an empty folder in Google Drive |
| 22 | + |
1 | 23 | /** |
2 | | - * Copyright 2022 Google LLC |
3 | | - * |
4 | | - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | - * you may not use this file except in compliance with the License. |
6 | | - * You may obtain a copy of the License at |
7 | | - * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | - * |
10 | | - * Unless required by applicable law or agreed to in writing, software |
11 | | - * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | - * See the License for the specific language governing permissions and |
14 | | - * limitations under the License. |
| 24 | + * Creates a custom menu "Appreciation" in the spreadsheet |
| 25 | + * with drop-down options to create and send certificates |
15 | 26 | */ |
| 27 | +function onOpen(e) { |
| 28 | + let ui = SpreadsheetApp.getUi(); |
| 29 | + ui.createMenu('Appreciation') |
| 30 | + .addItem('Create certificates', 'createCertificates') |
| 31 | + .addSeparator() |
| 32 | + .addItem('Send certificates', 'sendCertificates') |
| 33 | + .addToUi(); |
| 34 | +} |
16 | 35 |
|
17 | | -function myFunction() { |
| 36 | +/** |
| 37 | + * Creates a personalized certificate for each employee |
| 38 | + * and stores every individual Slides doc on Google Drive |
| 39 | + */ |
| 40 | +function createCertificates() { |
| 41 | + |
| 42 | + // Load the Google Slide template file |
| 43 | + let template = DriveApp.getFileById(slideTemplateId); |
| 44 | + |
| 45 | + // Get all employee data from the spreadsheet and identify the headers |
| 46 | + let sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); |
| 47 | + let values = sheet.getDataRange().getValues(); |
| 48 | + let headers = values[0]; |
| 49 | + let empNameIndex = headers.indexOf("Employee Name"); |
| 50 | + let dateIndex = headers.indexOf("Date"); |
| 51 | + let managerNameIndex = headers.indexOf("Manager Name"); |
| 52 | + let titleIndex = headers.indexOf("Title"); |
| 53 | + let compNameIndex = headers.indexOf("Company Name"); |
| 54 | + let empEmailIndex = headers.indexOf("Employee Email"); |
| 55 | + let empSlideIndex = headers.indexOf("Employee Slide"); |
| 56 | + let statusIndex = headers.indexOf("Status"); |
| 57 | + |
| 58 | + // Iterate through each row to capture individual details |
| 59 | + for (let i = 1; i < values.length; i++) { |
| 60 | + let rowData = values[i]; |
| 61 | + let empName = rowData[empNameIndex]; |
| 62 | + let date = rowData[dateIndex]; |
| 63 | + let managerName = rowData[managerNameIndex]; |
| 64 | + let title = rowData[titleIndex]; |
| 65 | + let compName = rowData[compNameIndex]; |
| 66 | + |
| 67 | + // Make a copy of the Slide template and rename it with employee name |
| 68 | + let tempFolder = DriveApp.getFolderById(tempFolderId); |
| 69 | + let empSlideId = template.makeCopy(tempFolder).setName(empName).getId(); |
| 70 | + let empSlide = SlidesApp.openById(empSlideId).getSlides()[0]; |
| 71 | + |
| 72 | + // Replace placeholder values with actual employee related details |
| 73 | + empSlide.replaceAllText("Employee Name", empName); |
| 74 | + empSlide.replaceAllText("Date", "Date: " + Utilities.formatDate(date, Session.getScriptTimeZone(), "MMMM dd, yyyy")); |
| 75 | + empSlide.replaceAllText("Your Name", managerName); |
| 76 | + empSlide.replaceAllText("Title", title); |
| 77 | + empSlide.replaceAllText("Company Name", compName); |
| 78 | + |
| 79 | + // Update the spreadsheet with the new Slide Id and status |
| 80 | + sheet.getRange(i + 1, empSlideIndex + 1).setValue(empSlideId); |
| 81 | + sheet.getRange(i + 1, statusIndex + 1).setValue("CREATED"); |
| 82 | + SpreadsheetApp.flush(); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Send an email to each individual employee |
| 88 | + * with a PDF attachment of their appreciation certificate |
| 89 | + */ |
| 90 | +function sendCertificates() { |
| 91 | + |
| 92 | + // Get all employee data from the spreadsheet and identify the headers |
| 93 | + let sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); |
| 94 | + let values = sheet.getDataRange().getValues(); |
| 95 | + let headers = values[0]; |
| 96 | + let empNameIndex = headers.indexOf("Employee Name"); |
| 97 | + let dateIndex = headers.indexOf("Date"); |
| 98 | + let managerNameIndex = headers.indexOf("Manager Name"); |
| 99 | + let titleIndex = headers.indexOf("Title"); |
| 100 | + let compNameIndex = headers.indexOf("Company Name"); |
| 101 | + let empEmailIndex = headers.indexOf("Employee Email"); |
| 102 | + let empSlideIndex = headers.indexOf("Employee Slide"); |
| 103 | + let statusIndex = headers.indexOf("Status"); |
18 | 104 |
|
| 105 | + // Iterate through each row to capture individual details |
| 106 | + for (let i = 1; i < values.length; i++) { |
| 107 | + let rowData = values[i]; |
| 108 | + let empName = rowData[empNameIndex]; |
| 109 | + let date = rowData[dateIndex]; |
| 110 | + let managerName = rowData[managerNameIndex]; |
| 111 | + let title = rowData[titleIndex]; |
| 112 | + let compName = rowData[compNameIndex]; |
| 113 | + let empSlideId = rowData[empSlideIndex]; |
| 114 | + let empEmail = rowData[empEmailIndex]; |
| 115 | + |
| 116 | + // Load the employee's personalized Google Slide file |
| 117 | + let attachment = DriveApp.getFileById(empSlideId); |
| 118 | + |
| 119 | + // Setup the required parameters and send them the email |
| 120 | + let senderName = "CertBot"; |
| 121 | + let subject = empName + ", you're awesome!"; |
| 122 | + let body = "Please find your employee appreciation certificate attached." |
| 123 | + + "\n\n" + compName + " team"; |
| 124 | + GmailApp.sendEmail(empEmail, subject, body, { |
| 125 | + attachments: [attachment.getAs(MimeType.PDF)], |
| 126 | + name: senderName |
| 127 | + }); |
| 128 | + |
| 129 | + // Update the spreadsheet with email status |
| 130 | + sheet.getRange(i + 1, statusIndex + 1).setValue("SENT"); |
| 131 | + SpreadsheetApp.flush(); |
| 132 | + } |
19 | 133 | } |
0 commit comments