forked from ivanching0319/20240226
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgooglebackend.js
117 lines (95 loc) · 3.04 KB
/
googlebackend.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
function doGet(e) {
// Log all requests
try {
let ss = SpreadsheetApp.getActiveSpreadsheet();
// If null, fall back to specific ID
if (!ss) {
ss = SpreadsheetApp.openById('XXX'); // Replace with your sheet ID
}
const logSheet = ss.getSheetByName('log');
if (logSheet) {
logSheet.appendRow([JSON.stringify(e)]);
}
const action = e.parameter.action;
switch (action) {
case 'login':
return handleLogin(e);
case 'register':
return handleRegister(e);
case 'addTodo':
return addTodo(e);
case 'todoList':
return getTodoList(e);
default:
return createErrorResponse('Invalid action');
}
} catch (error) {
console.error('Spreadsheet access error:', error);
return createErrorResponse('Failed to access spreadsheet');
}
}
function handleLogin(e) {
const userID = e.parameter.userID;
const password = e.parameter.pwd;
const userSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('user data');
const users = userSheet.getDataRange().getValues();
// Find user
const user = users.find(row => row[1] === userID && row[2] === password);
if (user) {
return ContentService.createTextOutput("true");
}
return ContentService.createTextOutput("false");
}
function handleRegister(e) {
const userID = e.parameter.userID;
const password = e.parameter.pwd;
const userSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('user data');
const users = userSheet.getDataRange().getValues();
// Check if user exists
if (users.some(row => row[1] === userID)) {
return ContentService.createTextOutput("false");
}
// Add new user
userSheet.appendRow([new Date(), userID, password]);
return ContentService.createTextOutput("true");
}
function addTodo(e) {
const userID = e.parameter.userID;
const title = e.parameter.titleID;
const description = e.parameter.descriptionID;
const date = e.parameter.dateID;
try {
const taskSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('task');
taskSheet.appendRow([
title,
userID,
description,
date,
new Date() // timestamp
]);
return ContentService.createTextOutput("true");
} catch (error) {
return ContentService.createTextOutput("false");
}
}
function getTodoList(e) {
const userID = e.parameter.userID;
const taskSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('task');
const tasks = taskSheet.getDataRange().getValues();
// Filter tasks by userID and format response
const userTasks = tasks
.filter(row => row[1] === userID)
.map(row => ({
titleID: row[0],
descriptionID: row[2],
dateID: row[3]
}));
return ContentService.createTextOutput(JSON.stringify(userTasks))
.setMimeType(ContentService.MimeType.JSON);
}
function createErrorResponse(message) {
return ContentService.createTextOutput(JSON.stringify({
status: 'error',
message: message
})).setMimeType(ContentService.MimeType.JSON);
}