-
Notifications
You must be signed in to change notification settings - Fork 1
/
google-script.js
169 lines (142 loc) · 3.87 KB
/
google-script.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
var SHEETS_ID = "YOUR_SHEET_KEY";
/**
* GET METHOD DATA
* -------------------------
* TABLE OF CONTENTS
* 1. GET ALL DATA
* 2. GET CUSTOM ROW
* 3. GET STASTIC DATA
* -------------------------
*/
function doGet(request) {
var ssID = SpreadsheetApp.openById(SHEETS_ID);
var tableName = request.parameter.tableName;
var sheet = ssID.getSheetByName(tableName);
var action = request.parameter.action;
switch (action) {
case "read":
return get_data(sheet, tableName);
break;
}
}
function get_data(sheet, tableName) {
var data = { success: false, data: [] };
var resultData = get_object_data(sheet);
if (resultData.length > 0) {
data = {
success: true,
data: resultData
};
}
var result = JSON.stringify(data);
return ContentService.createTextOutput(result).setMimeType(
ContentService.MimeType.JSON
);
}
function get_object_data(sheet) {
var allData = [];
var rangeObject = sheet.getDataRange().getValues();
var totalColumn = sheet.getLastColumn();
for (var row = 1; row < rangeObject.length; row++) {
var object = {};
for (var column = 0; column < totalColumn; column++) {
object[rangeObject[0][column]] = rangeObject[row][column];
}
allData.push(object);
}
return allData;
}
/**
* POST METHOD DATA
* THIS FUNCTION WILL BE HANDLE ALL FUNCTION WITH POST METHOD REQUEST
* --------------------------------------
* Table of Contents
* 1. INSERT DATA
* 2. UPDATE DATE
* --------------------------------------
*/
function doPost(e) {
var ssID = SpreadsheetApp.openById(SHEETS_ID);
var tableName = e.parameter.tableName;
var action = e.parameter.action;
var sheet = ssID.getSheetByName(tableName);
switch (action) {
case "insert":
return insert_data(e, sheet, "BELUM");
break;
case "update":
return update_data(e, sheet);
break;
case "qr_code":
return scan_qr(e, sheet);
break;
}
}
function insert_data(req, sheet, isHadir) {
var id = req.parameter.id;
var name = req.parameter.name;
sheet.appendRow([id, name, isHadir]);
var result = { success: true, message: "Data berhasil ditambahkan" };
return ContentService.createTextOutput(JSON.stringify(result)).setMimeType(
ContentService.MimeType.JSON
);
}
function scan_qr(req, sheet) {
var idQR = req.parameter.id;
var isSuccess = false;
var successFullyExchanged = 1;
var lastRow = sheet.getLastRow();
var result = {
success: false,
message: "Data QR tidak ditemukan. Mohon periksa kembali"
};
for (var row = 2; row <= lastRow; row++) {
var idQRFromServer = sheet.getRange(row, 1).getValue();
var statusFromServer = sheet.getRange(row, 2).getValue();
if (idQR == idQRFromServer) {
if (statusFromServer == successFullyExchanged) {
result = {
success: false,
message: "Kode QR sudah ditukarkan!"
};
} else {
sheet.getRange(row, 2).setValue(successFullyExchanged);
result = {
success: true,
message: "Kode QR berhasil ditukarkan."
};
}
row = lastRow;
}
}
return ContentService.createTextOutput(JSON.stringify(result)).setMimeType(
ContentService.MimeType.JSON
);
}
function update_data(req, sheet) {
var id = req.parameter.id;
var isHadir = req.parameter.isHadir;
var flag = 0;
var lastRow = sheet.getLastRow();
var result = {
success: false,
message: "Data tidak ditemukan"
};
for (var row = 2; row <= lastRow; row++) {
var idFromServer = sheet.getRange(row, 1).getValue();
if (id == idFromServer) {
sheet.getRange(row, 3).setValue(isHadir);
flag = 1;
row = lastRow;
}
}
if (flag === 1) {
result = {
success: true,
message: "Data berhasil diperbaharui"
};
}
return ContentService.createTextOutput(JSON.stringify(result)).setMimeType(
ContentService.MimeType.JSON
);
}