-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.gs
164 lines (143 loc) · 4.58 KB
/
script.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
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
/*
Author: Teeraphat Kullanankanjana
Version: 0.0.4
*/
function doGet(e) {
// Extract parameters from the request
var sheet_id = e.parameter.sheet_id;
var sheet_name = e.parameter.sheet_name;
var mode = e.parameter.mode;
// Open the spreadsheet and get the sheet
var ss = SpreadsheetApp.openById(sheet_id);
var sheet = ss.getSheetByName(sheet_name);
// Update a single cell
if (mode == "updateCell") {
var row = e.parameter.row;
var column = e.parameter.column;
var data = e.parameter.data;
var cell = sheet.getRange(row, column);
cell.setValue(data);
}
// Update a row with multiple values
else if (mode == "updateRow") {
var row = e.parameter.row;
var data = [];
var count = 0;
while (e.parameter["data" + count]) {
count++;
}
for (var i = 0; i < count; i++) {
var key = "data" + i;
var value = e.parameter[key];
data.push(value);
}
var range = sheet.getRange(row, 1, 1, data.length);
range.setValues([data]);
}
// Append a row with multiple values
else if (mode == "appendRow") {
var data = [];
var count = 0;
while (e.parameter["data" + count]) {
count++;
}
for (var i = 0; i < count; i++) {
var key = "data" + i;
var value = e.parameter[key];
data.push(value);
}
var lastRow = sheet.getLastRow();
var row = e.parameter.row || lastRow + 1; // If row parameter is not provided, append to last row + 1
if (row > 0) {
sheet.insertRowBefore(row);
lastRow = row - 1;
}
sheet.getRange(lastRow + 1, 1, 1, data.length).setValues([data]);
}
// Append a column with multiple values
else if (mode == "appendColumn") {
var data = [];
var count = 0;
while (e.parameter["data" + count]) {
count++;
}
for (var i = 0; i < count; i++) {
var key = "data" + i;
var value = e.parameter[key];
data.push([value]); // wrap the value in an array to create a column
}
var lastColumn = sheet.getLastColumn();
var column = e.parameter.column || lastColumn + 1; // If column parameter is not provided, append to last colum + 1
if (column > 0) {
sheet.insertColumnBefore(column);
lastColumn = column - 1;
}
sheet.getRange(1, lastColumn + 1, data.length, 1).setValues(data);
}
// Update a column with multiple values
else if (mode == "updateColumn") {
var column = e.parameter.column;
var data = [];
var count = 0;
while (e.parameter["data" + count]) {
count++;
}
for (var i = 0; i < count; i++) {
var key = "data" + i;
var value = e.parameter[key];
data.push([value]); // wrap the value in an array to create a column
}
var range = sheet.getRange(1, column, data.length, 1);
range.setValues(data);
}
// Get the value of a specific cell
else if (mode == "getCell") {
var row = e.parameter.row;
var column = e.parameter.column;
var cell = sheet.getRange(row, column);
var value = cell.getValue();
var html = "<html><head><title>Get The data </title></head><body><h1>start</h1><h1>" + value + "</h1><h1>finish</h1></body></html>";
return HtmlService.createHtmlOutput(html);
}
// Get the values of a specific row
else if (mode == "getRow") {
var row = e.parameter.row;
var range = sheet.getRange(row, 1, 1, sheet.getLastColumn());
var values = range.getValues()[0];
var heading = "";
for (var i = 0; i < values.length; i++) {
heading += values[i] + ",";
}
var html = "<html><head><title>Get Row Data</title></head><body><h1>start</h1><h1>" + heading + "</h1><h1>finish</h1></body></html>";
return HtmlService.createHtmlOutput(html);
}
// Get the values of a specific column
else if (mode == "getColumn") {
var column = e.parameter.column;
var range = sheet.getRange(1, column, sheet.getLastRow(), 1);
var values = range.getValues();
var heading = "";
for (var i = 0; i < values.length; i++) {
heading += values[i] + ",";
}
var html = "<html><head><title>Get Column Data</title></head><body><h1>start</h1><h1>" + heading + "</h1><h1>finish</h1></body></html>";
return HtmlService.createHtmlOutput(html);
}
// Delete a specific row
else if (mode == "deleteRow") {
var row = e.parameter.row;
sheet.deleteRow(row);
}
// Delete a specific column
else if (mode == "deleteColumn") {
var column = e.parameter.column;
sheet.deleteColumn(column);
}
// Clear the content of a specific cell
else if (mode == "deleteCell") {
var row = e.parameter.row;
var column = e.parameter.column;
var cell = sheet.getRange(row, column);
cell.clearContent();
}
}