-
Notifications
You must be signed in to change notification settings - Fork 4
/
Code.gs
210 lines (152 loc) · 6.26 KB
/
Code.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
var db = ScriptDb.getMyDb();
//Main Function Called when script is loaded
function doGet() {
var myapp = UiApp.createApplication().setTitle('Bloggy');
//create layout grid
var articleSubmitGrid = myapp.createGrid(3, 2).setId('articleSubmitGrid');//3 high 2 wide
articleSubmitGrid.setWidget(0, 0, myapp.createLabel('Title:'));
articleSubmitGrid.setWidget(0, 1, myapp.createTextBox().setName('titleText').setId('titleText'));
articleSubmitGrid.setWidget(1, 0, myapp.createLabel('Article:'));
articleSubmitGrid.setWidget(1, 1, myapp.createTextArea().setName('articleText').setId('articleText'));
articleSubmitGrid.setWidget(2, 0, myapp.createLabel('Author'));
articleSubmitGrid.setWidget(2, 1, myapp.createTextBox().setName('authorText').setId('authorText'));
//make button and panel
var articleSubmitButton = myapp.createButton('Submit').setId('articleSubmitButton');
var articleSubmitPanel = myapp.createVerticalPanel();
var constantMessage = myapp.createLabel("Remember you must have a folder named 'Bloggy' in your root and it must be shared to public.").setId('constantMessage');
articleSubmitPanel.setId('articleSubmitPanel');
articleSubmitPanel.add(articleSubmitGrid);
articleSubmitPanel.add(articleSubmitButton);
articleSubmitPanel.add(constantMessage);
myapp.add(articleSubmitPanel);
//create the server handler
var handler = myapp.createServerHandler('myClickHandler');
handler.addCallbackElement(articleSubmitPanel);
articleSubmitButton.addClickHandler(handler);
return myapp;
}
//click handler
function myClickHandler(e) {
var app = UiApp.getActiveApplication();
Logger.log(e);
var popPanel = app.createPopupPanel();
var message = app.createLabel("Default Text");
if (e.parameter.source == 'articleSubmitButton'){
var title = e.parameter.titleText;
var article = e.parameter.articleText;
var author = e.parameter.authorText;
var goodSave = saveArticleToDb(title, article, author);//goodSave returns the docId of new file as string
if(!(goodSave == "" || goodSave == null)){//make sure there is a docId otherwise the save failed.
var text = "You're article was submitted successfully! Here is the link to your new file: www.googledrive.com/host/" + goodSave;
message.setText(text);
clearForm();
//showResults(e);
} else {
message.setText("Article Submission Failed!");
}
popPanel.add(message);
popPanel.setAnimationEnabled(true);
popPanel.setPopupPosition(25, 75);
popPanel.setAutoHideEnabled(true);
popPanel.show();//pop up panel to show whether new file was made or not and show the URL of new file
}
app.close();
return app;
}
function clearForm(){
var app = UiApp.getActiveApplication();
var title = app.getElementById('titleText');
var article = app.getElementById('articleText');
var author = app.getElementById('authorText');
title.setText("");
article.setText("");
author.setText("");
app.close();
return app;
}
//save article to db
function saveArticleToDb(title, article, author){
var numItems = db.count({type: "blogPost"});
Logger.log(numItems);
var current = {type: "blogPost",
title: title,
article: article,
author: author,
docId: "",
id: numItems+1};//this is our auto increment - not necessary for this app but good to know
db.save(current);
var newDocId = updateWebfolder(current);//post the article to web and this var holds the newDocId
return newDocId;//used to show user the URL for the new file.
}
//publish file
function updateWebfolder(current){
var contents = current.article + current.author;
var file = DocsList.createFile(current.title, contents);//create the new post file.
var folder = DocsList.getFolder('Bloggy');
file.addToFolder(folder);
var ob = db.query({id: current.id}).next();//get the scriptDB info of the file(scriptDB holds the info of all posts.)
ob.docId = file.getId();//set the docId of the file we just created in the db
db.save(ob);
return ob.docId;//use this to display in our message to give URL.
}
//ONLY USED FOR TESTING
//display what is in the db
function getDBStuff(){
var results = db.query({});
while (results.hasNext()) {
var current = results.next();
Logger.log(current);
}
};
//delete all db records
function deleteAll() {
while (true) {
var result = db.query({}); // get everything, up to limit
if (result.getSize() == 0) {
break;
}
while (result.hasNext()) {
db.remove(result.next());
}
}
};
//check if file exists
/*function checkIfFileExists(searchTerm) {
try {var folder = DocsList.getFolder(searchTerm)
return true;
}
catch (e) {
return false;
}
};*/
//create new file or folder
/*function createFile(title, contents){
var file = DocsList.createFile("My New Drive File", "this is a new drive file that I created with app script");
var fileId = file.getId();
Logger.log(fileId);
file.addViewer('[email protected]');//this is where we add public, useful for Bloggy
var viewers = file.getViewers();
for(var i = 0;i < viewers.length;i++){
Logger.log(viewers[i].getEmail());
}
};*/
//move a file or folder
function moveFileToFolder(fileId, targetFolderId) {
var targetFolder = DocsList.getFolderById(targetFolderId);
var file = DocsList.getFileById(fileId);
file.addToFolder(targetFolder);
};
/*function showResults(e){
var app = UiApp.getActiveApplication();
var mygrid = app.createGrid(3, 1).setId('myGrid');
mygrid.setWidget(0, 0, app.createLabel(e.parameter.nameText));//getting value of parameter passed in by handler
mygrid.setWidget(1, 0, app.createLabel(e.parameter.ageText));
mygrid.setWidget(2, 0, app.createLabel(e.parameter.cityText));
var mybutton = app.createButton('New Button').setId('newButton');
var mypanel = app.getElementById('myPanel');
mypanel.add(mygrid);
mypanel.add(mybutton); //replacing the old lbls and txt with my new lbls and btn
var handler = app.createServerHandler('myClickHandler');//reusing the click handler I already made.
mybutton.addClickHandler(handler);//add click handler to new button
return app;
}*/