Skip to content

Commit 9b3351e

Browse files
authored
Update ggsheet.py
Fix issues provided by KorkhovPavel - Improve the start_index and end_index finders in getCell(), getColumn(), and getRow(). - Fix the index range of the returned data from getCell(), getColumn(), and getRow(). - Sync the gen_scriptFile() to script.gs version 0.0.3.
1 parent e69f427 commit 9b3351e

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

ggsheet.py

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import ujson
44
import os
55

6-
__version__ = '0.0.2'
6+
__version__ = '0.0.3'
77
__author__ = 'Teeraphat Kullanankanjana'
88

99
class MicroGoogleSheet():
@@ -100,15 +100,10 @@ def getCell(self, row=1, column=1):
100100

101101
if responseCode > 0:
102102
html_code = response.text
103-
start_index = html_code.find('<body>')
104-
end_index = html_code.find('</body>')
103+
start_index = html_code.find('start')
104+
end_index = html_code.find('finish')
105105
script_content = html_code[start_index:end_index]
106-
start_index = script_content.find('script type="text/javascript"')
107-
end_index = script_content.find('</script>')
108-
script_content = script_content[start_index:end_index]
109-
script_content = script_content[script_content.find(
110-
'body')+18:script_content.find('/body')-21]
111-
return script_content
106+
return script_content[29:-24]
112107
else:
113108
return "Error Code:{}".format(responseCode)
114109

@@ -121,15 +116,10 @@ def getRow(self, row=1):
121116
responseCode = response.status_code
122117
if responseCode > 0:
123118
html_code = response.text
124-
start_index = html_code.find('<body>')
125-
end_index = html_code.find('</body>')
119+
start_index = html_code.find('start')
120+
end_index = html_code.find('finish')
126121
script_content = html_code[start_index:end_index]
127-
start_index = script_content.find('script type="text/javascript"')
128-
end_index = script_content.find('</script>')
129-
script_content = script_content[start_index:end_index]
130-
script_content = script_content[script_content.find(
131-
'body')+18:script_content.find('/body')-21]
132-
return script_content
122+
return script_content[29:-24]
133123
else:
134124
return "Error Code:{}".format(responseCode)
135125

@@ -142,15 +132,10 @@ def getColumn(self, column=1):
142132
responseCode = response.status_code
143133
if responseCode > 0:
144134
html_code = response.text
145-
start_index = html_code.find('<body>')
146-
end_index = html_code.find('</body>')
135+
start_index = html_code.find('start')
136+
end_index = html_code.find('finish')
147137
script_content = html_code[start_index:end_index]
148-
start_index = script_content.find('script type="text/javascript"')
149-
end_index = script_content.find('</script>')
150-
script_content = script_content[start_index:end_index]
151-
script_content = script_content[script_content.find(
152-
'body')+18:script_content.find('/body')-21]
153-
return script_content
138+
return script_content[29:-24]
154139
else:
155140
return "Error Code:{}".format(responseCode)
156141

@@ -183,22 +168,31 @@ def deleteColumn(self,column=1):
183168

184169
def gen_scriptFile(self):
185170
code = """
171+
/*
172+
Author: Teeraphat Kullanankanjana
173+
Version: 0.0.3
174+
*/
175+
186176
function doGet(e) {
177+
// Extract parameters from the request
187178
var sheet_id = e.parameter.sheet_id;
188179
var sheet_name = e.parameter.sheet_name;
189180
var mode = e.parameter.mode;
190-
181+
182+
// Open the spreadsheet and get the sheet
191183
var ss = SpreadsheetApp.openById(sheet_id);
192184
var sheet = ss.getSheetByName(sheet_name);
193-
185+
186+
// Update a single cell
194187
if (mode == "updateCell") {
195188
var row = e.parameter.row;
196189
var column = e.parameter.column;
197190
var data = e.parameter.data;
198-
199191
var cell = sheet.getRange(row, column);
200192
cell.setValue(data);
201193
}
194+
195+
// Update a row with multiple values
202196
else if (mode == "updateRow") {
203197
var row = e.parameter.row;
204198
var data = [];
@@ -214,7 +208,8 @@ def gen_scriptFile(self):
214208
var range = sheet.getRange(row, 1, 1, data.length);
215209
range.setValues([data]);
216210
}
217-
211+
212+
// Append a row with multiple values
218213
else if (mode == "appendRow") {
219214
var data = [];
220215
var count = 0;
@@ -235,7 +230,8 @@ def gen_scriptFile(self):
235230
}
236231
sheet.getRange(lastRow + 1, 1, 1, data.length).setValues([data]);
237232
}
238-
233+
234+
// Append a column with multiple values
239235
else if (mode == "appendColumn") {
240236
var data = [];
241237
var count = 0;
@@ -256,6 +252,8 @@ def gen_scriptFile(self):
256252
}
257253
sheet.getRange(1, lastColumn + 1, data.length, 1).setValues(data);
258254
}
255+
256+
// Update a column with multiple values
259257
else if (mode == "updateColumn") {
260258
var column = e.parameter.column;
261259
var data = [];
@@ -271,17 +269,20 @@ def gen_scriptFile(self):
271269
var range = sheet.getRange(1, column, data.length, 1);
272270
range.setValues(data);
273271
}
274-
272+
273+
// Get the value of a specific cell
275274
else if (mode == "getCell") {
276275
var row = e.parameter.row;
277276
var column = e.parameter.column;
278277
279278
var cell = sheet.getRange(row, column);
280279
var value = cell.getValue();
281280
282-
var html = "<html><head><title>Get The data </title></head><body><h1>" + value + "</h1></body></html>";
281+
var html = "<html><head><title>Get The data </title></head><body><h1>start</h1><h1>" + value + "</h1><h1>finish</h1></body></html>";
283282
return HtmlService.createHtmlOutput(html);
284283
}
284+
285+
// Get the values of a specific row
285286
else if (mode == "getRow") {
286287
var row = e.parameter.row;
287288
var range = sheet.getRange(row, 1, 1, sheet.getLastColumn());
@@ -292,9 +293,11 @@ def gen_scriptFile(self):
292293
heading += values[i] + " ";
293294
}
294295
295-
var html = "<html><head><title>Get Row Data</title></head><body><h1>" + heading + "</h1></body></html>";
296+
var html = "<html><head><title>Get Row Data</title></head><body><h1>start</h1><h1>" + heading + "</h1><h1>finish</h1></body></html>";
296297
return HtmlService.createHtmlOutput(html);
297298
}
299+
300+
// Get the values of a specific column
298301
else if (mode == "getColumn") {
299302
var column = e.parameter.column;
300303
var range = sheet.getRange(1, column, sheet.getLastRow(), 1);
@@ -304,20 +307,24 @@ def gen_scriptFile(self):
304307
heading += values[i] + " ";
305308
}
306309
307-
var html = "<html><head><title>Get Row Data</title></head><body><h1>" + heading + "</h1></body></html>";
310+
var html = "<html><head><title>Get Column Data</title></head><body><h1>start</h1><h1>" + heading + "</h1><h1>finish</h1></body></html>";
308311
return HtmlService.createHtmlOutput(html);
309312
310313
}
314+
315+
// Delete a specific row
311316
else if (mode == "deleteRow") {
312317
var row = e.parameter.row;
313318
sheet.deleteRow(row);
314319
}
315320
321+
// Delete a specific column
316322
else if (mode == "deleteColumn") {
317323
var column = e.parameter.column;
318324
sheet.deleteColumn(column);
319325
}
320326
327+
// Clear the content of a specific cell
321328
else if (mode == "deleteCell") {
322329
var row = e.parameter.row;
323330
var column = e.parameter.column;
@@ -328,3 +335,4 @@ def gen_scriptFile(self):
328335
"""
329336
with open('script.txt', 'w') as file:
330337
file.write(code)
338+

0 commit comments

Comments
 (0)