-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
252 lines (221 loc) · 8.18 KB
/
index.html
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<!DOCTYPE html>
<html>
<head>
<title>Goktug's Questions</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="style.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Lilita+One&display=swap" rel="stylesheet">
</head>
<body>
<h1>Goktug's Questions</h1>
<!--Add buttons to initiate auth sequence and sign out-->
<button id="authorize_button" onclick="handleAuthClick()">Authorize</button>
<button id="signout_button" onclick="handleSignoutClick()">Sign Out</button>
<form id="form">
<select name="lecture" id="lecture">
<option value="fda labsession">fda labsession</option>
<option value="fda lecture">fda lecture</option>
<option value="laa lecture">laa lecture</option>
<option value="laa instruction">laa instruction</option>
<option value="discrete structures lecture">discrete structures lecture</option>
<option value="discrete structures discussion group">discrete structures discussion group</option>
</select>
<input type="number" name="amount" id="amount" value="1" min="1">
<input type="text" placeholder="comment" id="comment" name="comment">
<button type="submit">Submit</button>
</form>
<img src="bar.png" alt="bar">
<pre id="content" style="white-space: pre-wrap;"></pre>
<script type="text/javascript">
/* exported gapiLoaded */
/* exported gisLoaded */
/* exported handleAuthClick */
/* exported handleSignoutClick */
// TODO(developer): Set to client ID and API key from the Developer Console
const CLIENT_ID = '1097652578638-t72dqmdvuk6bbod7of659cgqilbnrn9b.apps.googleusercontent.com';
const API_KEY = 'AIzaSyDgi-LpoyenajnjPFIpizdhlfJkNW_jXX0';
// Discovery doc URL for APIs used by the quickstart
const DISCOVERY_DOC = 'https://sheets.googleapis.com/$discovery/rest?version=v4';
// Authorization scopes required by the API; multiple scopes can be
// included, separated by spaces.
const SCOPES = 'https://www.googleapis.com/auth/spreadsheets';
let tokenClient;
let gapiInited = false;
let gisInited = false;
document.getElementById('authorize_button').style.visibility = 'hidden';
document.getElementById('signout_button').style.visibility = 'hidden';
/**
* Callback after api.js is loaded.
*/
function gapiLoaded() {
gapi.load('client', initializeGapiClient);
}
/**
* Callback after the API client is loaded. Loads the
* discovery doc to initialize the API.
*/
async function initializeGapiClient() {
await gapi.client.init({
apiKey: API_KEY,
discoveryDocs: [DISCOVERY_DOC],
});
gapiInited = true;
maybeEnableButtons();
}
/**
* Callback after Google Identity Services are loaded.
*/
function gisLoaded() {
tokenClient = google.accounts.oauth2.initTokenClient({
client_id: CLIENT_ID,
scope: SCOPES,
callback: '', // defined later
});
gisInited = true;
maybeEnableButtons();
}
/**
* Enables user interaction after all libraries are loaded.
*/
function maybeEnableButtons() {
if (gapiInited && gisInited) {
document.getElementById('authorize_button').style.visibility = 'visible';
}
}
/**
* Sign in the user upon button click.
*/
function handleAuthClick() {
tokenClient.callback = async (resp) => {
if (resp.error !== undefined) {
throw (resp);
}
document.getElementById('signout_button').style.visibility = 'visible';
document.getElementById('authorize_button').innerText = 'Refresh';
await listQuestions();
};
if (gapi.client.getToken() === null) {
// Prompt the user to select a Google Account and ask for consent to share their data
// when establishing a new session.
tokenClient.requestAccessToken({ prompt: 'consent' });
} else {
// Skip display of account chooser and consent dialog for an existing session.
tokenClient.requestAccessToken({ prompt: '' });
}
}
/**
* Sign out the user upon button click.
*/
function handleSignoutClick() {
const token = gapi.client.getToken();
if (token !== null) {
google.accounts.oauth2.revoke(token.access_token);
gapi.client.setToken('');
document.getElementById('content').innerText = '';
document.getElementById('authorize_button').innerText = 'Authorize';
document.getElementById('signout_button').style.visibility = 'hidden';
}
}
function appendValues(spreadsheetId, range, valueInputOption, _values, callback) {
let values = []
for (let i = 0; i < _values.length; i++) {
row = _values[i]
values.push([
row[2],
row[3],
row[0],
row[1]
])
}
const body = {
values: values,
};
try {
gapi.client.sheets.spreadsheets.values.append({
spreadsheetId: spreadsheetId,
range: range,
valueInputOption: valueInputOption,
resource: body,
}).then((response) => {
const result = response.result;
console.log(`${result.updates.updatedCells} cells appended.`);
if (callback) callback(response);
});
} catch (err) {
document.getElementById('content').innerText = err.message;
return;
}
}
const form = document.querySelector("#form");
function sendData() {
const array = []
for (let index = 0; index < document.querySelector('#amount').value; index++) {
const today = new Date(Date.now());
// Associate the FormData object with the form element
const formData = new FormData(form);
var minutes = today.getMinutes();
if (minutes.length = 1) {
minutes = "0" + minutes;
}
formData.append("date", today.getDate() + "/" + (today.getMonth() + 1) + "/" + today.getFullYear())
formData.append("time", today.getHours() + ":" + minutes)
var row = []
for (var pair of formData.entries()) {
if (pair[0] == 'amount') continue;
row.push(pair[1])
}
array.push(row)
}
appendValues('1YBhZJD_jkzJEGYbT-Q58bDe6M2bhUES6rvWlGtQn-pY', 'Sheet1!A2:D', "USER_ENTERED", array)
listQuestions()
}
form.addEventListener("submit", (event) => {
event.preventDefault();
sendData();
});
async function listQuestions() {
let response;
try {
// Fetch first 10 files
response = await gapi.client.sheets.spreadsheets.values.get({
spreadsheetId: '1YBhZJD_jkzJEGYbT-Q58bDe6M2bhUES6rvWlGtQn-pY',
range: 'Sheet1!A2:D',
});
} catch (err) {
document.getElementById('content').innerText = err.message;
return;
}
const range = response.result;
if (!range || !range.values || range.values.length == 0) {
document.getElementById('content').innerText = 'No values found.';
return;
}
console.table(range)
const total = "Total questions: " + String(range.values.length) + "\n";
// Flatten to string to display
const output = createTable(range.values);
document.getElementById('content').innerText = total;
document.getElementById('content').appendChild(output);
}
function createTable(tableData) {
var table = document.createElement('table');
var tableBody = document.createElement('tbody');
tableData.forEach(function (rowData) {
var row = document.createElement('tr');
rowData.forEach(function (cellData) {
var cell = document.createElement('td');
cell.appendChild(document.createTextNode(cellData));
row.appendChild(cell);
});
tableBody.appendChild(row);
});
table.appendChild(tableBody);
return table;
}
</script>
<script async defer src="https://apis.google.com/js/api.js" onload="gapiLoaded()"></script>
<script async defer src="https://accounts.google.com/gsi/client" onload="gisLoaded()"></script>
</body>
</html>