-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard_scripts.js
112 lines (108 loc) · 4.14 KB
/
dashboard_scripts.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
var people = [];
var detailReports = [];
var datesToAdd = [];
var times = [];
var available = 0;
var subtractions = 0;
var totalHours = 0;
var positiveHours = [];
var negativeHours = [];
function addToSheet(i){
// need to test if all staff were working during that week. count how many unique 'people' had work that week and if any of that work was 'out of office'
SpreadsheetApp.getActiveSheet().getRange('Dashboard!C' + (i+2)).setValue((available * 40) - subtractions);
SpreadsheetApp.getActiveSheet().getRange('Dashboard!B' + (i+2)).setValue(totalHours);
SpreadsheetApp.getActiveSheet().getRange('Dashboard!D' + (i+2)).setValue(totalHours/((available * 40) - subtractions));
}
function checkAvailableHours(datesToAdd){
// count how many people have hours logged for that date
available = 0;
var check = detailReports.filter(function(record){
if (record.dateTime === datesToAdd){
return record;
}
});
for(var i = 0; i < people.length; i++){
var person = check.filter(function(record){
return record.person == people[i].person;
});
if(person.length > 0){
available++;
}
}
}
function checkSubtractions(dateToAdd){
var total = 0;
for (var i = 0; i < negativeHours.length; i++){
if(negativeHours[i].dateTime === dateToAdd){
total = total + negativeHours[i].hours;
}
}
subtractions = total;
}
function checkTotalHours(dateToAdd){
totalHours = 0;
for (var i = 0; i < positiveHours.length; i++){
if(positiveHours[i].dateTime === dateToAdd){
if (positiveHours[i].project === 'Out of Office' && positiveHours[i].projects === 'Holiday' && positiveHours[i].projects === 'Personal'){
} else {
totalHours = totalHours + positiveHours[i].hours;
}
}
}
}
function getDetails(){
for(var i = 0; i < people.length; i++){
var sheet = SpreadsheetApp.openByUrl(people[i].URL);
var dates = sheet.getActiveSheet().getRange('Project_Log!B2:B1000').getValues();
var projects = sheet.getActiveSheet().getRange('Project_Log!A2:A1000').getValues();
var hours = sheet.getActiveSheet().getRange('Project_Log!C2:C1000').getValues();
for(var j = 0; j < dates.length; j++){
if(typeof dates[j][0] === 'object'){
// converts dates to milliseconds so that they can be compared
var timeToAdd = dates[j][0].getTime();
var index = times.indexOf(timeToAdd);
var currentPerson = people[i].person;
if (index < 0){
times.push(timeToAdd)
var date = dates[j][0].toString();
// cuts out extra characters I don't want in my strings
var smallDate = date.slice(4,16);
datesToAdd.push({date: smallDate, dateTime: timeToAdd});
}
if (projects[j][0] == 'Out of Office' || projects[j][0] == 'Holiday' || projects[j][0] == 'Personal'){
//detailReports.push({person: currentPerson, dateTime: timeToAdd, date: smallDate, hours: 0, project: projects[j][0]});
negativeHours.push({hours: hours[j][0], dateTime: timeToAdd, project: projects[j][0]});
positiveHours.push({hours: 0, dateTime: timeToAdd, project: projects[j][0]});
Logger.log('project: ' + projects[j][0] + hours[j][0]);
}
else {
detailReports.push({person: currentPerson, dateTime: timeToAdd, date: smallDate, hours: hours[j][0], project: projects[j][0]});
positiveHours.push({hours: hours[j][0], dateTime: timeToAdd, project: projects[j][0]});
}
}
}
}
}
function getPeople() {
var sheet = SpreadsheetApp.getActiveSheet().getRange('Reference!A2:B10').getValues();
for (var i = 0; i < sheet.length; i++){
if(sheet[i][0].length > 0){
people.push({person: sheet[i][0], URL: sheet[i][1]});
}
}
Logger.log(people);
}
function populateData(){
for (var i = 0; i < datesToAdd.length; i++){
SpreadsheetApp.getActiveSheet().getRange('Dashboard!A' + (i+2)).setValue(datesToAdd[i].date);
checkAvailableHours(datesToAdd[i].dateTime);
checkSubtractions(datesToAdd[i].dateTime);
checkTotalHours(datesToAdd[i].dateTime);
addToSheet(i);
}
}
function onEdit(){
getPeople();
getDetails();
populateData();
}