forked from bushwickfc/bfc-fetch7shifts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertHoursWorked.js
52 lines (40 loc) · 1.5 KB
/
insertHoursWorked.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
var memberRowMap = {}
function insertHours() {
var shiftObjects = getShiftObjects(),
colToInsert = get7ShiftsColumn( getColumnFromUser() );
createMemberRowMap();
shiftObjects.forEach( function( shift ) {
// Logger.log( "Member: " + shift.member.firstName + " " + shift.member.lastName + " worked " + shift.hoursWorked() + " hours" )
// Logger.log( "date is: " + shift.start )
if ( memberRowMap[shift.member.lastName] ) {
updateCellValue( memberRowMap[shift.member.lastName], colToInsert, shift.hoursWorked() )
}
})
}
function createMemberRowMap() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
for ( var i = 0; i < data.length; i++ ) {
memberRowMap[data[i][2]] = i + 1;
}
}
function getShiftObjects() {
var jsonObj = parseJSON( fetch7Shifts() ),
shiftObjectsArray = createShiftObjs( jsonObj );
return shiftObjectsArray;
}
function updateCellValue( row, col, value ) {
var sheet = SpreadsheetApp.getActiveSheet().getRange( row, col );
var currentValue = parseInt( sheet.getValue() ) || 0;
sheet.setValue( currentValue + value );
}
function get7ShiftsColumn( columnName ) {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
return data[0].indexOf( columnName ) + 1;
}
function getColumnFromUser() {
var ui = SpreadsheetApp.getUi();
var response = ui.prompt( 'Please enter the Column Name to insert hours, e.g: 7Shifts' );
return response.getResponseText();
}