Repository navigation
Expand file tree
/
Copy pathScriptHistoryApp.js
More file actions
260 lines (247 loc) · 9.55 KB
/
Copy pathScriptHistoryApp.js
File metadata and controls
260 lines (247 loc) · 9.55 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
* GitHub https://github.com/tanaikech/ScriptHistoryApp<br>
* Library name
* @type {string}
* @const {string}
* @readonly
*/
var appName = "ScriptHistoryApp";
/**
* ### Description
* Run the script by giving object.
*
* @param {Object} object Object for running script.
* @returns {(ContentService.TextOutput|HtmlService.HtmlOutput|String)} ContentService.TextOutput or HtmlService.HtmlOutput or String is returned by depending the input object.
*/
function server(object = {}) {
if (!object || typeof object != "object" || Object.keys(object).length == 0) {
throw new Error("Please give valid object.");
}
// return new ScriptHistoryApp(object).main();
const lock = LockService.getScriptLock();
if (lock.tryLock(350000)) {
try {
return new ScriptHistoryApp(object).main();
} catch ({ stack }) {
throw new Error(stack);
} finally {
lock.releaseLock();
}
} else {
throw new Error("Timeout");
}
}
/**
* ### Description
* This is a Class ScriptHistoryApp for managing the script history using Google Apps Script.
*
* Author: Tanaike ( https://tanaikech.github.io/ )
*/
class ScriptHistoryApp {
/**
* ### Description
* Constructor of this class.
*
* @return {void}
*/
constructor(e) {
this.obj = e;
}
/**
* ### Description
* Main method.
*
* @returns {(ContentService.TextOutput|HtmlService.HtmlOutput|String)} ContentService.TextOutput or HtmlService.HtmlOutput or String is returned by depending the input object.
*/
main() {
const e = this.obj;
if (!e.hasOwnProperty("parameter")) {
throw new Error("Invalid object.");
}
const { scriptId = null, folderId = "root", process = "getHistories", load = null, htmlOutput = null, jsonOutput = null, deleteDate = null, search = "", internal = "false" } = e.parameter;
if (!scriptId) {
return ContentService.createTextOutput("No script ID.");
}
let returnValue;
if (process == "store") {
returnValue = this.storeScript_({ scriptId, folderId });
} else if (process == "getHistories" && jsonOutput == "true") {
returnValue = this.getScriptHistoriesAsJSON_({ scriptId, folderId, search });
} else if (process == "getHistories" && htmlOutput != "true") {
returnValue = this.getScriptHistoriesAsHTML_pre_({ scriptId, folderId, search });
} else if (process == "getHistories" && htmlOutput == "true") {
returnValue = this.getScriptHistoriesAsHTML_({ scriptId, folderId, search });
} else if (process == "loadHistory" && load) {
returnValue = this.loadScriptHistory_({ folderId, scriptId, load });
} else if (process == "deleteHistory" && deleteDate && deleteDate != "") {
returnValue = this.deleteHistory_({ folderId, scriptId, deleteDate });
}
if (internal == "true") {
returnValue = this.createHTMLOfScriptHistoriesAsHTML_({ scriptId, folderId, search });
}
return returnValue;
}
/**
* ### Description
* Store script from Google Apps Script project to a file.
*
* @param {Object} object Object for running script.
* @return {ContentService.TextOutput} ContentService.TextOutput is returned.
*/
storeScript_({ scriptId, folderId, type = true }) {
const res = this.fetchAppsScriptAPI_({ scriptId, method: "get" });
const resText = res.getContentText();
if (res.getResponseCode() != 200) {
return ContentService.createTextOutput(JSON.stringify({ error: resText }));
}
const { file, data } = this.getHistoryFile_({ folderId, scriptId });
const obj2 = data == "" ? [] : JSON.parse(data);
const date = new Date().toISOString();
const newData = { date, data: JSON.parse(resText) };
obj2.push(newData);
file.setContent(JSON.stringify(obj2));
const msg = `Save as ${date}`;
return type ? ContentService.createTextOutput(msg) : msg;
}
/**
* ### Description
* Get script histories as JSON.
*
* @param {Object} object Object for running script.
* @return {ContentService.TextOutput} ContentService.TextOutput is returned.
*/
getScriptHistoriesAsJSON_({ scriptId, folderId, search }) {
let { data } = this.getHistoryFile_({ folderId, scriptId });
if (search) {
let obj = data == "" ? [] : JSON.parse(data);
const reg = new RegExp(search, "gi");
obj = obj.filter(({ data }) => data.files.some(({ source }) => reg.test(source)));
data = JSON.stringify(obj);
}
return ContentService.createTextOutput(data);
}
/**
* ### Description
* Get script histories as JSON.
*
* @param {Object} object Object for running script.
* @return {ContentService.TextOutput} ContentService.TextOutput is returned.
*/
getScriptHistoriesAsHTML_pre_({ scriptId, folderId, search }) {
const url = `${ScriptApp.getService().getUrl()}?process=getHistories&htmlOutput=true&folderId=${folderId}&scriptId=${scriptId}&search=${search}`;
const msg = { message: `Please access the URL "${url}" using your bowser.`, url };
return ContentService.createTextOutput(JSON.stringify(msg));
}
/**
* ### Description
* Get script histories as HTML.
*
* @param {Object} object Object for running script.
* @return {HtmlService.HtmlOutput} HtmlService.HtmlOutput is returned.
*/
getScriptHistoriesAsHTML_({ scriptId, folderId, search }) {
const html = this.createHTMLOfScriptHistoriesAsHTML_({ scriptId, folderId, search });
const h = HtmlService.createTemplateFromFile("index");
h.scriptId = scriptId;
h.data = html;
return h.evaluate();
}
/**
* ### Description
* Create HTML data for "getScriptHistoriesAsHTML_".
*
* @param {Object} object Object for running script.
* @return {String} HTML as string.
*/
createHTMLOfScriptHistoriesAsHTML_({ scriptId, folderId, search }) {
const { data } = this.getHistoryFile_({ folderId, scriptId });
let obj = data == "" ? [] : JSON.parse(data);
if (search) {
const reg = new RegExp(search, "gi");
obj = obj.filter(({ data }) => data.files.some(({ source }) => reg.test(source)));
}
if (obj.length == 0) {
return "No histories.";
}
const html = obj.map(({ date, data: { files } }) => {
const temp = [
`<input type="button" value="Back to '${date}'." onclick="main(this,'${date}','loadHistory')">`,
`<input type="button" value="Delete this history." onclick="main(this,'${date}','deleteHistory')">`,
`<h2 class="open_h2 btn">${date}</h2>`,
`<div class="open_parent open">`
];
files.forEach(({ name, source }) => {
temp.push(`<h3 class="open_h3 btn">${name}</h3><textarea class="open_textarea open">${source}</textarea>`);
});
temp.push("</div>");
return temp.join("");
}).join("");
return html;
}
/**
* ### Description
* Load script history to Google Apps Script project.
*
* @param {Object} object Object for running script.
* @return {HtmlService.HtmlOutput} HtmlService.HtmlOutput is returned.
*/
loadScriptHistory_({ folderId, scriptId, load }) {
this.storeScript_({ scriptId, folderId, type: false });
const { data } = this.getHistoryFile_({ folderId, scriptId });
const obj = data == "" ? [] : JSON.parse(data);
const script = obj.find(({ date }) => date == load);
if (script) {
const res = this.fetchAppsScriptAPI_({ scriptId, method: "put", payload: script.data });
const resText = res.getContentText();
if (res.getResponseCode() != 200) {
return ContentService.createTextOutput(JSON.stringify({ error: resText }));
}
return ContentService.createTextOutput("Done.");
}
return ContentService.createTextOutput(`The history of "${load}" was not found.`);
}
/**
* ### Description
* Delete a history.
*
* @param {Object} object Object for running script.
* @return {UrlFetchApp.HTTPResponse} UrlFetchApp.HTTPResponse is returned.
*/
deleteHistory_({ folderId, scriptId, deleteDate }) {
const { file, data } = this.getHistoryFile_({ folderId, scriptId });
const obj = data == "" ? [] : JSON.parse(data);
const newDate = obj.filter(({ date }) => date != deleteDate);
file.setContent(JSON.stringify(newDate));
return "Done.";
}
/**
* ### Description
* Request Google Apps Script API.
*
* @param {Object} object Object for running script.
* @return {UrlFetchApp.HTTPResponse} UrlFetchApp.HTTPResponse is returned.
*/
fetchAppsScriptAPI_({ scriptId, method, payload = null }) {
const url = `https://script.googleapis.com/v1/projects/${scriptId}/content`;
const options = { method, headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() }, muteHttpExceptions: true };
if (payload) {
options.payload = JSON.stringify(payload);
options.contentType = "application/json";
}
return UrlFetchApp.fetch(url, options);
}
/**
* ### Description
* Request Google Apps Script API.
*
* @param {Object} object Object for running script.
* @return {Object} Object including file object and text data of the histories is returned.
*/
getHistoryFile_({ folderId, scriptId }) {
const folder = DriveApp.getFolderById(folderId);
const files = folder.getFilesByName(scriptId);
const file = files.hasNext() ? files.next() : folder.createFile(scriptId, "", MimeType.PLAIN_TEXT);
const data = file.getBlob().getDataAsString();
return { file, data };
}
}