-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackend.gs
69 lines (62 loc) · 2.37 KB
/
backend.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
function doGet (e) {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('log').appendRow([JSON.stringify(e)])
let action = e.parameter.action
switch (action) {
case 'login':
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('login record').appendRow([decodeURI(e.queryString)])
return login();
case 'addTodo':
return addTodo(e); // 另一個處理函數
case 'todoList':
return todoList(e);
case 'register':
return register(e);
default:
result = "無效的動作";
}
}
function login(){
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('login record')
const rowNum = sheet.getLastRow()
const loginValue = sheet.getRange(rowNum, 4).getValue();
if (loginValue === true) {
const userID = sheet.getRange(rowNum,2).getValue();
return ContentService.createTextOutput("true")
} else {
return ContentService.createTextOutput("false")
}
}
function register(e){
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('log').appendRow([JSON.stringify(e)])
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('user data')
let userID = e.parameter.userID
let res = sheet.getDataRange().getValues()
.filter(row => row[1] === userID)
if (hasValue(res) === false) {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('user data').appendRow([e.queryString])
return ContentService.createTextOutput("true")
}
else {
return ContentService.createTextOutput("false")
}
}
function addTodo(e) {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('task').appendRow([e.queryString])
return ContentService.createTextOutput("true")
}
function todoList(e){
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('log').appendRow([JSON.stringify(e)])
let userID = e.parameter.userID
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('task')
let res = sheet.getDataRange().getValues()
.filter(row => row[1] === userID).map(row => row[0]);
const jsonOutput = JSON.stringify(res)
return ContentService.createTextOutput(jsonOutput)
}
function hasValue(arr) {
// 檢查是否為數組且不為空
if (!Array.isArray(arr)) {
return false; // 如果不是數組,返回 false
}
return arr.some(value => value !== undefined && value !== null && value !== '');
}