-
Notifications
You must be signed in to change notification settings - Fork 0
/
gas_gcal_gsheet_sync.js
120 lines (102 loc) · 4.98 KB
/
gas_gcal_gsheet_sync.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Google Apps Script for automating the creation of calendar events from a Google Sheets document.
* This script reads data from the "Publishing Schedule 2024" sheet and creates events in the
* specified team members' Google Calendars. The events are created at 10:00 AM UK time and
* include a popup reminder 10 minutes before the event.
*
* Key functionalities:
* - Events are only created for rows marked as "Dubbed" in the specified column (Column D, index 3).
* - The script skips past dates and creates events only for future dates, using the date in
* Column B (index 1).
* - The script processes rows starting from row 52.
* - The script uses script properties to store created event IDs to avoid duplicate entries.
* - Additional event details can be included from other columns if available.
*
* Important notes:
* - The script assumes that all users have access to the calendars and that their email addresses
* are correctly specified in the `emailAddresses` array.
* - The script does not use domain-wide delegation, so the events are created using the credentials
* of the user running the script.
* - Ensure the date format in the Google Sheets document is consistent and recognized by JavaScript.
* - Users must have the appropriate permissions to access and modify the calendars.
*
* To configure:
* - Update the `spreadsheetId` and `sheetName` with your own Google Sheets ID and sheet name.
* - Modify the `emailAddresses` array with the email addresses of the team members who should
* receive the calendar events.
*
* This script is intended to streamline calendar management for the team, ensuring everyone is
* informed of key post-launch checks for dubbed content.
*
* Author: Chad Ramey
* Date: July 30, 2024
*/
function createCalendarEvents() {
// IDs of the spreadsheet and the tab
var spreadsheetId = 'docID'; // Replace with your Google Sheets ID
var sheetName = 'sheeName';
// Open the spreadsheet and the specific tab
var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
// Get the data range starting from row 52
var dataRange = sheet.getRange('A52:H');
var data = dataRange.getValues();
// Email addresses of the team members
var emailAddresses = ['[email protected]', '[email protected]', 'email_3omain.com']; // Add new email here
// Get today's date
var currentDate = new Date();
var today = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate());
Logger.log('Current Date: ' + today);
var scriptProperties = PropertiesService.getScriptProperties();
// Iterate through the rows
for (var i = 0; i < data.length; i++) {
if (data[i][3] === 'Dubbed') { // Check if the class is Dubbed (column D, index 3)
var dateStr = data[i][1]; // Date (column B, index 1)
// Parse the date string and set the event time to 10:00 AM UK time
var eventDate = new Date(dateStr);
eventDate.setHours(10, 0, 0, 0); // 10:00 AM UK time
Logger.log('Event Date: ' + eventDate);
// Compare only the date parts
var eventDateOnly = new Date(eventDate.getFullYear(), eventDate.getMonth(), eventDate.getDate());
if (eventDateOnly < today) {
Logger.log('Skipping event for ' + dateStr + ' as it is in the past.');
continue;
}
// Create the event title
var eventTitle = 'Post-launch checks (dubbing) ' + data[i][7]; // Column H, index 7
// Additional info
var additionalInfo = '';
if (data[i][2]) { // Column C, index 2
additionalInfo += 'Column C: ' + data[i][2] + '\n';
}
if (data[i][4]) { // Column E, index 4
additionalInfo += 'Column E: ' + data[i][4] + '\n';
}
if (data[i][5]) { // Column F, index 5
additionalInfo += 'Column F: ' + data[i][5] + '\n';
}
// Create the event description
var eventDescription = eventTitle + '\n' + additionalInfo;
// Create the event in the calendar of each team member
emailAddresses.forEach(function(email) {
try {
var eventId = dateStr + '-' + email; // Unique ID for each event and user
if (!scriptProperties.getProperty(eventId)) {
var calendar = CalendarApp.getCalendarById(email);
if (!calendar) {
throw new Error('Calendar not found for email: ' + email);
}
var event = calendar.createEvent(eventTitle, eventDate, eventDate, {description: eventDescription, timeZone: 'Europe/London'});
// Add popup reminder
event.addPopupReminder(10); // Popup reminder 10 minutes before the event
Logger.log('Event created for email ' + email + ' with title: ' + eventTitle);
scriptProperties.setProperty(eventId, event.getId());
} else {
Logger.log('Event for ' + eventId + ' already exists. Skipping.');
}
} catch (e) {
Logger.log('Error creating event for email ' + email + ': ' + e.message);
}
});
}
}
}