forked from entaq/GoogleAppsScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptDbConsole.html
More file actions
107 lines (104 loc) · 4.03 KB
/
ScriptDbConsole.html
File metadata and controls
107 lines (104 loc) · 4.03 KB
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
<html>
<style>
pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }
</style>
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
function output(inp) {
$('#results').prepend(inp);
}
//courtesy of http://stackoverflow.com/a/7220510/662551
function syntaxHighlight(json) {
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
$(document).ready(function() {
$('#saveItem').click(function(){
var input = $('#textarea').val();
input = JSON.parse(input);
google.script.run.withSuccessHandler(handleMessageResponse).withFailureHandler(addToLog).saveObjToDb(input);
});
$('#runQuery').click(function(){
var input = $('#textarea').val();
input = JSON.parse(input);
google.script.run.withSuccessHandler(handleQueryResponse).withFailureHandler(addToLog).queryFromDb(input);
});
$('#deleteAll').click(function(){
var input = $('#textarea').val();
input = JSON.parse(input);
google.script.run.withSuccessHandler(handleMessageResponse).withFailureHandler(addToLog).deleteAll(input);
});
$('#loadId').click(function(){
var input = $('#textarea').val();
var re = /\s*,\s*/;
var idList = input.split(re);
google.script.run.withSuccessHandler(handleQueryResponse).withFailureHandler(addToLog).loadIDsFromDb(idList);
});
$('#deleteByIds').click(function(){
var input = $('#textarea').val();
var re = /\s*,\s*/;
var idList = input.split(re);
google.script.run.withSuccessHandler(handleMessageResponse).withFailureHandler(addToLog).deleteByIds(idList);
});
$('#count').click(function(){
var input = $('#textarea').val();
input = JSON.parse(input);
google.script.run.withSuccessHandler(handleMessageResponse).withFailureHandler(addToLog).getCount(input);
});
});
function handleQueryResponse(responseObj){
$('#results').prepend('<hr><hr>');
if(Object.keys(responseObj).length==0){
output("No results found for query!");
}
//TODO: for load by IDs, this will print index, not object IDs
for (var prop in responseObj) {
if (responseObj.hasOwnProperty(prop)) {
output('<pre>ID: ' + prop+'\n'+syntaxHighlight(JSON.stringify(responseObj[prop], undefined, 4))+'</pre>');
}
}
}
function handleMessageResponse(response){
$('#results').prepend('<hr><hr>');
output(response);
}
function addToLog(error){
$('#results').prepend('<hr><hr>');
output("Error " + JSON.stringify(error));
}
</script>
<body>
<h2>Simple ScriptDb tester</h2>
<b>Enter a JavaScript Object to store, a ScriptDb query to run or IDs to lookup and press the appropriate button below</b><br>
<textarea id="textarea" rows="10" cols="100"></textarea><br>
<button id="count">Get Count</button>
<button id="saveItem">Save Object</button>
<button id="runQuery">Run Query</button>
<button id="loadId">Load by ID</button>
<button id="deleteByIds">Delete by ID</button>
<button id="deleteAll">Delete for Query</button>
<br><br>
<div id="results"></div>
<br><br>
By <a href="https://plus.google.com/117678608428606781684/posts">Arun Nagarajan</a>
</body>
</html>