This repository has been archived by the owner on Mar 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebRequest.js
190 lines (175 loc) · 6.6 KB
/
webRequest.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Report URL for data export from Shiftboard
function getReportUrl(tgtDate) {
return 'https://www.shiftboard.com/servola/reporting/report.cgi?' +
'type=coverage&ss=298255&deleted_teams=2&covered=1&' +
'format=tab_delimited&include=selected_fields&download=Download&' +
'start_date=' + tgtDate + '&' +
'end_date=' + tgtDate
}
// Common function to get Shiftboard data, do a callback when got
function retrieveShiftboardData(tgtDate, callback) {
var dateStr = getDateStr(tgtDate);
var url = getReportUrl(dateStr);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
let data = xhr.responseText;
// Check for present data
if (data.split('\n').length < 3) {
msg('No data to export for this date.');
stopRunning();
} else {
callback(data);
}
} else {
msg('Error: Could not get Shiftboard report.');
console.log(xhr.responseText);
stopRunning();
}
}
};
xhr.open('GET', url, true);
xhr.send();
}
// Common function to upload sheet to Drive
// Executes the callback function stored in the global variable 'relocateFunction'
function uploadSheet(spreadsheet, tgtDate) {
getDriveToken(false, function(token) {
var url = 'https://sheets.googleapis.com/v4/spreadsheets';
url += '?access_token=' + token;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
let result = JSON.parse(xhr.responseText);
// Auto-resize cols
resizeCols(result, token);
// Try to move into correct folder
var id = result.spreadsheetId;
relocateFunction(token, tgtDate, id);
} else {
msg('Error: Could not upload Google Sheet.');
console.log(xhr.responseText);
stopRunning();
}
}
};
xhr.open('POST', url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify(spreadsheet));
});
}
// Resizes cols to be automatic resize after data uploaded
function resizeCols(spreadsheetInfo, token) {
var url = 'https://sheets.googleapis.com/v4/spreadsheets/';
url += spreadsheetInfo.spreadsheetId + ':batchUpdate';
url += '?access_token=' + token;
var payload = {
requests: []
};
// Auto-resize for first five cols
let sheets = spreadsheetInfo.sheets;
for (let sheet of sheets) {
let sheetId = sheet.properties.sheetId;
let request = {
autoResizeDimensions: {
dimensions: {
sheetId: sheetId,
dimension: 'COLUMNS',
startIndex: 0,
endIndex: 4
}
}
};
payload.requests.push(request);
}
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status !== 200) {
msg('Error: Could not format Google Sheet.');
console.log(xhr.responseText);
}
}
};
xhr.open('POST', url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify(payload));
}
// Moves the file into the given folder.
// Removes current parent reference and adds a new parent
// Executes the callback function stored in the global variable 'finishFunction'
function relocateFileToFolder(token, fileId, folderId) {
// Do nothing if null params
if (token === null || fileId === null || folderId === null) {
return;
}
var url = 'https://www.googleapis.com/drive/v2/files/';
url += fileId;
url += '?access_token=' + token;
// Gets current file info, including parent
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
// Remove old parent and add new parent
var result = JSON.parse(xhr.responseText);
var currentParent = result.parents[0].id;
var urlUpdate = url;
urlUpdate += "&removeParents=" + currentParent;
urlUpdate += "&addParents=" + folderId;
var xhrUpdate = new XMLHttpRequest();
xhrUpdate.onreadystatechange = function() {
if (xhrUpdate.readyState === XMLHttpRequest.DONE) {
if (xhrUpdate.status === 200) {
var finalResult = JSON.parse(xhrUpdate.responseText);
var link = finalResult.alternateLink;
console.log(link);
// Finish function, report result
finishUploadFunction(link);
} else {
console.log(xhrUpdate.responseText);
}
}
};
xhrUpdate.open('PUT', urlUpdate, true);
xhrUpdate.send();
} else {
console.log(xhr.responseText);
}
}
};
xhr.open('GET', url, true);
xhr.send();
}
// Callsback the ID of the child folder, null
// if not present.
function findChildFolder(token, parent, childName, callback) {
var url = 'https://www.googleapis.com/drive/v2/files';
url += '?access_token=' + token + '&';
url += "q=title%3D'" + childName + "'";
if (parent !== null) {
url += "+and+'" + parent + "'+in+parents";
}
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
let result = JSON.parse(xhr.responseText);
if (result.items.length === 0) {
console.log(xhr.responseText);
callback(null);
} else {
let id = result.items[0].id;
callback(id);
}
} else {
console.log(xhr.responseText);
callback(null);
}
}
};
xhr.open('GET', url, true);
xhr.send();
}