|
| 1 | +// To learn how to use this script, refer to the documentation: |
| 2 | +// https://developers.google.com/apps-script/samples/automations/agenda-maker |
| 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 | +/** |
| 21 | + * Checks if the folder for Agenda docs exists, and creates it if it doesn't. |
| 22 | + * |
| 23 | + * @return {*} Drive folder ID for the app. |
| 24 | + */ |
| 25 | +function checkFolder() { |
| 26 | + const folders = DriveApp.getFoldersByName('Agenda Maker - App'); |
| 27 | + // Finds the folder if it exists |
| 28 | + while (folders.hasNext()) { |
| 29 | + let folder = folders.next(); |
| 30 | + if ( |
| 31 | + folder.getDescription() == |
| 32 | + 'Apps Script App - Do not change this description' && |
| 33 | + folder.getOwner().getEmail() == Session.getActiveUser().getEmail() |
| 34 | + ) { |
| 35 | + return folder.getId(); |
| 36 | + } |
| 37 | + } |
| 38 | + // If the folder doesn't exist, creates one |
| 39 | + let folder = DriveApp.createFolder('Agenda Maker - App'); |
| 40 | + folder.setDescription('Apps Script App - Do not change this description'); |
| 41 | + return folder.getId(); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Finds the template agenda doc, or creates one if it doesn't exist. |
| 46 | + */ |
| 47 | +function getTemplateId(folderId) { |
| 48 | + const folder = DriveApp.getFolderById(folderId); |
| 49 | + const files = folder.getFilesByName('Agenda TEMPLATE##'); |
| 50 | + |
| 51 | + // If there is a file, returns the ID. |
| 52 | + while (files.hasNext()) { |
| 53 | + const file = files.next(); |
| 54 | + return file.getId(); |
| 55 | + } |
| 56 | + |
| 57 | + // Otherwise, creates the agenda template. |
| 58 | + // You can adjust the default template here |
| 59 | + const doc = DocumentApp.create('Agenda TEMPLATE##'); |
| 60 | + const body = doc.getBody(); |
| 61 | + |
| 62 | + body |
| 63 | + .appendParagraph('##Attendees##') |
| 64 | + .setHeading(DocumentApp.ParagraphHeading.HEADING1) |
| 65 | + .editAsText() |
| 66 | + .setBold(true); |
| 67 | + body.appendParagraph(' ').editAsText().setBold(false); |
| 68 | + |
| 69 | + body |
| 70 | + .appendParagraph('Overview') |
| 71 | + .setHeading(DocumentApp.ParagraphHeading.HEADING1) |
| 72 | + .editAsText() |
| 73 | + .setBold(true); |
| 74 | + body.appendParagraph(' '); |
| 75 | + body.appendParagraph('- Topic 1: ').editAsText().setBold(true); |
| 76 | + body.appendParagraph(' ').editAsText().setBold(false); |
| 77 | + body.appendParagraph('- Topic 2: ').editAsText().setBold(true); |
| 78 | + body.appendParagraph(' ').editAsText().setBold(false); |
| 79 | + body.appendParagraph('- Topic 3: ').editAsText().setBold(true); |
| 80 | + body.appendParagraph(' ').editAsText().setBold(false); |
| 81 | + |
| 82 | + body |
| 83 | + .appendParagraph('Next Steps') |
| 84 | + .setHeading(DocumentApp.ParagraphHeading.HEADING1) |
| 85 | + .editAsText() |
| 86 | + .setBold(true); |
| 87 | + body.appendParagraph('- Takeaway 1: ').editAsText().setBold(true); |
| 88 | + body.appendParagraph('- Responsible: ').editAsText().setBold(false); |
| 89 | + body.appendParagraph('- Accountable: '); |
| 90 | + body.appendParagraph('- Consult: '); |
| 91 | + body.appendParagraph('- Inform: '); |
| 92 | + body.appendParagraph(' '); |
| 93 | + body.appendParagraph('- Takeaway 2: ').editAsText().setBold(true); |
| 94 | + body.appendParagraph('- Responsible: ').editAsText().setBold(false); |
| 95 | + body.appendParagraph('- Accountable: '); |
| 96 | + body.appendParagraph('- Consult: '); |
| 97 | + body.appendParagraph('- Inform: '); |
| 98 | + body.appendParagraph(' '); |
| 99 | + body.appendParagraph('- Takeaway 3: ').editAsText().setBold(true); |
| 100 | + body.appendParagraph('- Responsible: ').editAsText().setBold(false); |
| 101 | + body.appendParagraph('- Accountable: '); |
| 102 | + body.appendParagraph('- Consult: '); |
| 103 | + body.appendParagraph('- Inform: '); |
| 104 | + |
| 105 | + doc.saveAndClose(); |
| 106 | + |
| 107 | + folder.addFile(DriveApp.getFileById(doc.getId())); |
| 108 | + |
| 109 | + return doc.getId(); |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * When there is a change to the calendar, searches for events that include "#agenda" |
| 114 | + * in the decrisption. |
| 115 | + * |
| 116 | + */ |
| 117 | +function onCalendarChange() { |
| 118 | + // Gets recent events with the #agenda tag |
| 119 | + const now = new Date(); |
| 120 | + const events = CalendarApp.getEvents( |
| 121 | + now, |
| 122 | + new Date(now.getTime() + 2 * 60 * 60 * 1000000), |
| 123 | + {search: '#agenda'}, |
| 124 | + ); |
| 125 | + |
| 126 | + const folderId = checkFolder(); |
| 127 | + const templateId = getTemplateId(folderId); |
| 128 | + |
| 129 | + const folder = DriveApp.getFolderById(folderId); |
| 130 | + |
| 131 | + // Loops through any events found |
| 132 | + for (i = 0; i < events.length; i++) { |
| 133 | + const event = events[i]; |
| 134 | + |
| 135 | + // Confirms whether the event has the #agenda tag |
| 136 | + let description = event.getDescription(); |
| 137 | + if (description.search('#agenda') == -1) continue; |
| 138 | + |
| 139 | + // Only works with events created by the owner of this calendar |
| 140 | + if (event.isOwnedByMe()) { |
| 141 | + // Creates a new document from the template for an agenda for this event |
| 142 | + const newDoc = DriveApp.getFileById(templateId).makeCopy(); |
| 143 | + newDoc.setName('Agenda for ' + event.getTitle()); |
| 144 | + |
| 145 | + const file = DriveApp.getFileById(newDoc.getId()); |
| 146 | + folder.addFile(file); |
| 147 | + |
| 148 | + const doc = DocumentApp.openById(newDoc.getId()); |
| 149 | + const body = doc.getBody(); |
| 150 | + |
| 151 | + // Fills in the template with information about the attendees from the |
| 152 | + // calendar event |
| 153 | + const conf = body.findText('##Attendees##'); |
| 154 | + if (conf) { |
| 155 | + const ref = conf.getStartOffset(); |
| 156 | + |
| 157 | + for (let i in event.getGuestList()) { |
| 158 | + let guest = event.getGuestList()[i]; |
| 159 | + |
| 160 | + body.insertParagraph(ref + 2, guest.getEmail()); |
| 161 | + } |
| 162 | + body.replaceText('##Attendees##', 'Attendees'); |
| 163 | + } |
| 164 | + |
| 165 | + // Replaces the tag with a link to the agenda document |
| 166 | + const agendaUrl = 'https://docs.google.com/document/d/' + newDoc.getId(); |
| 167 | + description = description.replace( |
| 168 | + '#agenda', |
| 169 | + '<a href=' + agendaUrl + '>Agenda Doc</a>', |
| 170 | + ); |
| 171 | + event.setDescription(description); |
| 172 | + |
| 173 | + // Invites attendees to the Google doc so they automatically receive access to the agenda |
| 174 | + newDoc.addEditor(newDoc.getOwner()); |
| 175 | + |
| 176 | + for (let i in event.getGuestList()) { |
| 177 | + let guest = event.getGuestList()[i]; |
| 178 | + |
| 179 | + newDoc.addEditor(guest.getEmail()); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + return; |
| 184 | +} |
| 185 | + |
| 186 | +/** |
| 187 | + * Creates an event-driven trigger that fires whenever there's a change to the calendar. |
| 188 | + */ |
| 189 | +function setUp() { |
| 190 | + let email = Session.getActiveUser().getEmail(); |
| 191 | + ScriptApp.newTrigger("onCalendarChange").forUserCalendar(email).onEventUpdated().create(); |
| 192 | +} |
0 commit comments