-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.gs
151 lines (129 loc) · 4.72 KB
/
utils.gs
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
function setup(){
var username=promptForText("Enter Peloton Username");
if(username==null) return;
var password=promptForText("Enter Peloton Password");
if (password==null) return;
var data=processLogin(username,password);
ui.alert("Your session ID has been set to "+data.session_id+"\n Your User ID set to "+data.user_id);
}
function isBlank(str) {
return (!str || /^\s*$/.test(str));
}
function getDataAsObjects(sheet, range){
if(!range || range==null) range=sheet.getDataRange();
var rows=range.getValues();
var result=[];
if(rows.length>1){
var fields=rows[0];
for(var row=1; row<rows.length;++row){
var obj={};
for(var col=0; col<fields.length;++col){
obj[fields[col]]=rows[row][col];
}
// row number in google sheets (1-based)
obj._row=1+row;
result.push(obj);
}
}
return result;
}
function testEventStart(){
eventStart("test","1,2,3,4");
}
function testEventEnd(){
eventEnd(1,4);
}
function eventStart(name, arguments){
Logger.log("EventStart: "+name+" /// "+arguments);
logSheet=SpreadsheetApp.getActive().getSheetByName(LOG_SHEET_NAME);
var id=1+ logSheet.getDataRange().getLastRow();
var data=[[name,new Date(),arguments, null, null, null]];
logSheet.getRange(id, 1,1, 6).setValues(data);
return id;
}
function eventEnd(id, result){
logSheet=SpreadsheetApp.getActive().getSheetByName(LOG_SHEET_NAME);
var start=logSheet.getRange(id,2).getValue();
var now=new Date();
var duration=0;
if(start) duration=now.getTime()-start.getTime();
var data=[[result, new Date(), duration]];
logSheet.getRange(id,4,1, 3).setValues(data);
Logger.log("Event End: "+id+" /// "+result +" /// Duration: "+duration+"ms");
}
function processLogin(username, password){
var sheet = SpreadsheetApp.getActive().getSheetByName(CONFIG_SHEET_NAME);
var ui = SpreadsheetApp.getUi(); // Same variations.
var auth={
"username_or_email": username,
"password": password
};
var response=UrlFetchApp.fetch(
getConfigDetails().peloton.http_base+"/auth/login",
{'method':'POST','contentType': 'application/json', 'payload':JSON.stringify(auth)}
);
var json = response.getContentText();
var data = JSON.parse(json);
sheet.getRange(SESSION_ID_CELL).setValue(data.session_id);
sheet.getRange(USER_ID_CELL).setValue(data.user_id);
sheet.getRange(USERNAME_CELL).setValue(username);
return data;
}
function promptForText(msg) {
var ui = SpreadsheetApp.getUi();
var result = ui.prompt(
msg+":",
ui.ButtonSet.OK_CANCEL);
// Process the user's response.
var button = result.getSelectedButton();
if(button == ui.Button.CANCEL) return null;
var text = result.getResponseText();
return text;
}
function getConfigDetails(){
var cfg = SpreadsheetApp.getActive().getSheetByName(CONFIG_SHEET_NAME);
var session_id=cfg.getRange(SESSION_ID_CELL).getValue();
var user_id=cfg.getRange(USER_ID_CELL).getValue();
var tz=cfg.getRange(TIME_ZONE_CELL).getValue();
var eligible_ride_age=cfg.getRange(ELIGIBLE_RIDE_AGE_CELL).getValue();
var dataSettings={
results_join_col:cfg.getRange(DATA_RESULTS_JOIN_COL).getValue(),
join_sheet_name:cfg.getRange(DATA_JOIN_SHEETNAME_CELL).getValue(),
join_range:cfg.getRange(DATA_JOIN_RANGE_CELL).getValue(),
col1_name:cfg.getRange(DATA_JOIN_COL1_NAME_CELL).getValue(),
col1_column:cfg.getRange(DATA_JOIN_COL1_COLUMN_CELL).getValue(),
col2_name:cfg.getRange(DATA_JOIN_COL2_NAME_CELL).getValue(),
col2_column:cfg.getRange(DATA_JOIN_COL2_COLUMN_CELL).getValue(),
col3_name:cfg.getRange(DATA_JOIN_COL3_NAME_CELL).getValue(),
col3_column:cfg.getRange(DATA_JOIN_COL3_COLUMN_CELL).getValue(),
results_join2_col:cfg.getRange(DATA_RESULTS_JOIN2_COL).getValue(),
join2_sheet_name:cfg.getRange(DATA_JOIN2_SHEETNAME_CELL).getValue(),
join2_range:cfg.getRange(DATA_JOIN2_RANGE_CELL).getValue(),
join2_col1_column:cfg.getRange(DATA_JOIN2_COL1_COLUMN_CELL).getValue(),
join2_col1_name:cfg.getRange(DATA_JOIN2_COL1_NAME_CELL).getValue()
};
return {
"email":{
"to": cfg.getRange(EMAIL_TO_CELL).getValue(),
"cc": cfg.getRange(EMAIL_CC_CELL).getValue(),
"subject": cfg.getRange(EMAIL_SUBJECT_CELL).getValue()
},
"dataSettings" :dataSettings,
"peloton":{
"distance_units":cfg.getRange(DISTANCE_UNIT_CELL).getValue(),
"http_base":PELOTON_API_BASE,
"session_id":session_id,
"user_id":user_id,
"timezone":tz,
"eligible_ride_age":eligible_ride_age,
"http_options":
{
'headers':
{
'peloton-platform':PELOTON_PLATFORM,
'cookie':'peloton_session_id='+session_id
}
}
}
};
}