-
Notifications
You must be signed in to change notification settings - Fork 2
/
code.rtf
83 lines (65 loc) · 2.89 KB
/
code.rtf
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
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var lastRow = sheet.getLastRow();
var calendar = CalendarApp.getCalendarById("cs637team3@gmail.com");
//Obtains appointment request information from spreadsheet.
function getRequest(){
//portion of code which sets values based on data submitted with request.
this.timestamp = sheet.getRange(lastRow,1).getValue();
this.email = sheet.getRange(lastRow, 2).getValue();
this.name = sheet.getRange(lastRow, 3).getValue();
this.date = new Date(sheet.getRange(lastRow,5).getValue());
this.start_time = sheet.getRange(lastRow, 6).getValue();
this.end_time = sheet.getRange(lastRow, 7).getValue();
//Adjust start time and end time
this.date.setHours(this.start_time.getHours());
this.date.setMinutes(this.start_time.getMinutes());
this.endTime = new Date(this.date);
this.endTime.setHours(this.end_time.getHours());
this.endTime.setMinutes(this.end_time.getMinutes());
return this;
}
//function to check for appointment conflicts
function conflictCHKR(request){
var conflicts = calendar.getEvents(request.date, request.endTime);
//For function debugging purposes
//Logger.log(conflicts.length);
//Sets email subject line and message based on conflicts.
if(conflicts.length <1){
request.header = "Confirmation - Your appointment has been scheduled.";
request.message = "Your appointment has been scehduled. Your appointment start time is " + request.date + " and end time is " + request.endTime;
} else {
request.header = "Time Conflict - Your appointment is not scheduled.";
request.message = "There appears to be a scheduling conflict with your requested time slot. There is a conflicting appointment between your request time of " + request.date + " and end time of " + request.endTime + ". Please submit another appointment request by visiting: https://sunzc5.wixsite.com/barbershop .";
}
return conflicts.length;
}
//Creates Google Calendar event from information obtained from appointment request
function createEvent(request){
var event = calendar.createEvent(request.name, request.date, request.endTime);
//For function debugging purposes
/*Logger.log(request.name);
Logger.log(request.date);
Logger.log(request.endTime);*/
}
//Function to send email to appointment requester.
function sendEmail(request){
MailApp.sendEmail(request.email, request.header, request.message);
//For function debugging purposes
/*Logger.log(request.email);
Logger.log(request.header);
Logger.log(request.message);*/
}
//main function
function main(){
var request = getRequest();
//If get conflitcs returns a value of 1 or higher the event is not created.
if (conflictCHKR(request) < 1){
createEvent(request);
}
//sends email to requestor before finishing main.
sendEmail(request);
//For function debugging purposes
/*Logger.log(request.name);
Logger.log(request.date);*/
}