-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathGoogleSpreadSheet.js
executable file
·131 lines (101 loc) · 2.83 KB
/
GoogleSpreadSheet.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
// this Lib deprecated!
// Use GoogleTableSync Lib!
let libPrefix = "SpreadSheet-Lib-"
function throwError(err){
throw "Google SpreadSheet Lib: " + err;
}
function setUrlApp(appUrl){
Bot.sendMessage("This Lib deprecated! Please use GoogleTableSync Lib");
if(typeof(appUrl)!="string"){
throwError("Need pass Google App url")
}
if(appUrl.indexOf("https://script.google.com/")+1>0){
return Bot.setProperty(libPrefix + "app-url", appUrl, "string");
}
throwError("Seems it is not url for Google App: " + appUrl);
}
function getAppUrl(){
let result = Bot.getProperty(libPrefix + "app-url");
if(!result){
throwError("Need set Google App url before using")
}
return result;
}
function checkOptions(options){
if(!options){ throwError("Need pass options") }
if(!options.sheetName){ throwError("Need pass sheetName") }
if(!options.onSuccess){ throwError("Need pass onSuccess command name") }
}
function getCallback(options){
let onError = " ";
if(onError){ onError = options.onError }
return libPrefix + "onSuccess " + options.onSuccess + " " + onError;
}
function getErrCallback(options){
let onError = " ";
if(onError){ onError = options.onError }
return libPrefix + "onError " + onError;
}
function getHeader(options){
let appUrl = getAppUrl();
checkOptions(options);
let qrow = ""
if(options.rowIndex){ qrow = "&rowIndex=" + options.rowIndex }
HTTP.get({
url: appUrl + "?sheetName=" + options.sheetName + qrow,
success: getCallback(options),
error: getErrCallback(options),
})
}
function getRow(options){
getHeader(options);
}
function postRow(options, isEdit){
let appUrl = getAppUrl();
checkOptions(options);
// row: { "User": "Ivan", "Task":"create task", "Desc": "My cool DESC." }
if(!options.row){ throwError("Need pass table row data") }
if(isEdit&&(!options.rowIndex)){
throwError("Need pass rowIndex for editing")
}
HTTP.post( {
url: appUrl,
success: getCallback(options),
error: getErrCallback(options),
body: options
})
}
function addRow(options){
postRow(options, false);
}
function editRow(options){
postRow(options, true)
}
function onSuccess(){
let callback = params.split(" ")[0];
let errCalback = params.split(" ")[1];
var result = content.split("APP-RESULT")[1];
if(!result){
// error
var arr = content.split("width:600px");
var error = arr[1].split("<")[0]
return Bot.runCommand(errCalback, {error: error});
}
result = decodeURI(result);
result = JSON.parse(result)
Bot.runCommand(callback, result);
}
function onError(){
let errCalback = params;
Bot.sendMessage("Download error");
Bot.runCommand(errCalback);
}
publish({
setUrl: setUrlApp,
getHeader: getHeader,
getRow: getRow,
addRow: addRow,
editRow: editRow
})
on(libPrefix + "onSuccess", onSuccess );
on(libPrefix + "onError", onError);