-
Notifications
You must be signed in to change notification settings - Fork 2
/
Code.gs
309 lines (264 loc) · 9.41 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
function doGet(e) {
return HtmlService.createTemplateFromFile('index')
.evaluate()
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
.addMetaTag('viewport', 'width=device-width, initial-scale=1');
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
// This function is used to return two array of child folders and child folder's Id , where agrument is Parent Folder's Id
function Folder_Name_and_ID(parentFolder) {
let foldersArray = [];
let foldersArrayId = [];
while (parentFolder.hasNext()) {
let Folder = parentFolder.next();
foldersArray.push(Folder.getName());
foldersArrayId.push(Folder.getId());
}
return [foldersArray, foldersArrayId]
}
// This code is used for getting folders and displaying files -----------------------------------------------------
function Checker(parent_folder_id) {
const parentfiles = DriveApp.getFolderById(parent_folder_id).getFiles();
const parentfolders = DriveApp.getFolderById(parent_folder_id).getFolders();
const filesArray = [];
while (parentfiles.hasNext()) {
filesArray.push(parentfiles.next().getName());
break;
}
let [foldersArray, foldersArrayID] = Folder_Name_and_ID(parentfolders);
return [foldersArray, filesArray, foldersArrayID, parent_folder_id]
}
// This code is used for generating Years button --------------------------------------------------------------------
function Yearbtn() {
const Organised = DriveApp.getFolderById('ID_OF_FOLDER_CONTAINING_ALL_DATA').getFolders();
let [YearFoldersName, YearFoldersId] = Folder_Name_and_ID(Organised);
let FolderList = [];
for (i = 0; i < YearFoldersName.length; i++) {
let list = {
name: YearFoldersName[i],
id: YearFoldersId[i]
};
FolderList.push(list);
};
let byName = FolderList.slice(0);
byName.sort(function (a, b) {
let x = a.name.toLowerCase();
let y = b.name.toLowerCase();
return x < y ? -1 : x > y ? 1 : 0;
});
for (i = 0; i < byName.length; i++) {
YearFoldersName[i] = byName[i].name;
YearFoldersId[i] = byName[i].id;
};
return [YearFoldersName, YearFoldersId];
}
//yearFolderId
function CardsDisplay(yearFolderId) {
const Folders = DriveApp.getFolderById(yearFolderId).getFolders();
let [FoldersName, FoldersId] = Folder_Name_and_ID(Folders);
return [FoldersName, FoldersId];
}
// used to fetch the website using the embed url in grid view
function Websitefetch(website_to_fetch) {
var response = UrlFetchApp.fetch(website_to_fetch);
var content = response.getContentText();
return content
}
// getting id and name of folder for breadscrumb
function getData(id) {
var x = DriveApp.getFolderById(id);
var name = x.getName();
var id = x.getId();
return [id, name];
}
var allFolders = [];
function getFolders(id)
{
var folders = DriveApp.getFolderById(id).getFolders();
while(folders.hasNext())
{
var folder = folders.next();
var name = folder.getName();
var folderId = folder.getId();
var folderPath = findPath(folderId);
allFolders.push([name, folderId, folderPath]);
getFolders(folderId);
}
}
function foldersToSheet()
{
getFolders("ID_OF_FOLDER_CONTAINING_ALL_DATA");
var ss = SpreadsheetApp.openById("ID_OF_SPREADSHEET_WHERE_FILES_AND_FOLDER_INFO_IS_STORED_FOR_SEARCH");
var sheet = ss.getSheetByName("NAME_OF_SHEET_CONTAINING_FILES_AND_FOLDERS_INFO");
sheet.clear();
sheet.getRange(1, 1).setValue("Name");
sheet.getRange(1, 2).setValue("ID");
sheet.getRange(1, 3).setValue("Type");
sheet.getRange(1, 4).setValue("Path");
i = 0;
while(i < allFolders.length)
{
sheet.getRange(i+2, 1).setValue(allFolders[i][0]);
sheet.getRange(i+2, 2).setValue(allFolders[i][1]);
sheet.getRange(i+2, 3).setValue("Folder");
sheet.getRange(i+2, 4).setValue(allFolders[i][2]);
i += 1
}
}
var allFiles = []
function getFiles()
{
var ss = SpreadsheetApp.openById("ID_OF_SPREADSHEET_WHERE_FILES_AND_FOLDER_INFO_IS_STORED_FOR_SEARCH");
var sheet = ss.getSheetByName("NAME_OF_SHEET_CONTAINING_FILES_AND_FOLDERS_INFO");
var folders = sheet.getDataRange().getValues();
var i = 1;
while(i<folders.length)
{
var files = DriveApp.getFolderById(folders[i][1]).getFiles();
while(files.hasNext())
{
var file = files.next();
var name = file.getName();
var fileId = file.getId();
var filePath = folders[i][3] + "/" + name;
allFiles.push([name, fileId, filePath]);
}
i += 1;
}
}
function filesToSheet()
{
getFiles();
var ss = SpreadsheetApp.openById("ID_OF_SPREADSHEET_WHERE_FILES_AND_FOLDER_INFO_IS_STORED_FOR_SEARCH");
var sheet = ss.getSheetByName("NAME_OF_SHEET_CONTAINING_FILES_AND_FOLDERS_INFO");
var length = sheet.getLastRow();
var i = 0;
while(i < allFiles.length)
{
sheet.getRange(length+1, 1).setValue(allFiles[i][0]);
sheet.getRange(length+1, 2).setValue(allFiles[i][1]);
sheet.getRange(length+1, 3).setValue("File");
sheet.getRange(length+1, 4).setValue(allFiles[i][2]);
length += 1;
i += 1;
}
}
var pathline = [];
function getPath(id) {
var ids = DriveApp.getFileById(id).getParents();
while (ids.hasNext()) {
var id = ids.next();
pathline.push(id.getName());
if (id != null) {
getPath(id.getId());
}
}
}
function findPath(id) {
pathline = [];
getPath(id);
pathline.pop();
pathline.pop();
pathline.reverse();
pathline.push(DriveApp.getFileById(id).getName())
var final_path = pathline.join("/");
return final_path;
}
// returns the search results to javascript file
function searchFiles(query) {
var result = [];
var ss = SpreadsheetApp.openById("ID_OF_SPREADSHEET_WHERE_FILES_AND_FOLDER_INFO_IS_STORED_FOR_SEARCH");
var sheet = ss.getSheetByName("NAME_OF_SHEET_CONTAINING_FILES_AND_FOLDERS_INFO");
var data = sheet.getDataRange().getValues();
var i = 1;
while (i < data.length) {
try
{
if (data[i][0].toLowerCase().split(' ').join('').includes(query)) {
result.push(data[i]);
}
}
catch(error)
{
Logger.log(data[i][0]);
}
i += 1;
}
return result;
}
// getting all folders from spreadsheet to verify that while contributing user inputs folder ID from one f these folders itself
function getAllFolders() {
var allFolders = [];
var ss = SpreadsheetApp.openById("ID_OF_SPREADSHEET_WHERE_FILES_AND_FOLDER_INFO_IS_STORED_FOR_SEARCH");
var sheet = ss.getSheetByName("NAME_OF_SHEET_CONTAINING_FILES_AND_FOLDERS_INFO");
var data = sheet.getDataRange().getValues();
var i = 1;
while (i < data.length) {
if (data[i][2] == "Folder") {
allFolders.push(data[i][1]);
}
i += 1
}
return allFolders;
}
// for transferring files from contributed folder to MugLo Drive Folder
function sheetToDrive() {
var ss = SpreadsheetApp.openById("ID_OF_SPREADSHEET_CONTAINING_RESPONSES_FROM_GOOGLE_FORMS");
var responseSheet = ss.getSheetByName("NAME_OF_SHEET_CONTAINING_FORM_RESPONSES");
var responses = responseSheet.getDataRange().getValues();
var allFoldersInMuglo = getAllFolders();
var doc = DocumentApp.openById("ID_OF_GOOGLE_DOC_CONTAINING_INDEX_OF_ROW_FROM_WHERE_TO_START");
var docBody = doc.getBody().editAsText();
var ind = parseInt(docBody.getText());
var i = ind;
while (i < responses.length) {
if (responses[i][0] != "") {
try {
var parentFolderIdB64 = responses[i][4].replace("DONOTEDIT<", "");
parentFolderIdB64 = parentFolderIdB64.replace(">", "");
var parentFolderId = b64toString(parentFolderIdB64);
if (allFoldersInMuglo.includes(parentFolderId)) {
var folders = DriveApp.getFolderById(parentFolderId).getFoldersByName(responses[i][2]);
if (folders.hasNext()) {
var currentFolderId = folders.next().getId();
}
else {
var currentFolderId = DriveApp.getFolderById(parentFolderId).createFolder(responses[i][2]).getId();
}
var files = responses[i][3].split(", ");
var j = 0;
while (j < files.length) {
var file = files[j];
var fileId = file.replace("https://drive.google.com/open?id=", "");
Logger.log(fileId);
var fileToMove = DriveApp.getFileById(fileId);
fileToMove.getParents().next().removeFile(fileToMove);
DriveApp.getFolderById(currentFolderId).addFile(fileToMove);
j += 1;
}
responseSheet.getRange(i + 1, 1, 1, 5).setBackgroundRGB(28, 233, 21);
MailApp.sendEmail(responses[i][1], "Contribution to MugLo", "Your files have been successfully uploaded to MugLo. Here is the drive link to them: https://drive.google.com/open?id=" + currentFolderId + "\nThanks for contributing.");
}
else {
responseSheet.getRange(i + 1, 1, 1, 5).setBackgroundRGB(233, 35, 21);
MailApp.sendEmail(responses[i][1], "Failure in Uploading Files to MugLo", "An error occured while uploading your files! Please try uploading them again.");
}
}
catch (err) {
responseSheet.getRange(i + 1, 1, 1, 5).setBackgroundRGB(233, 35, 21);
MailApp.sendEmail(responses[i][1], "Failure in Uploading Files to MugLo", "An error occured while uploading your files! Please try uploading them again.");
}
}
i += 1
}
docBody.setText(i);
}
// decoding the b64 id again to normal
function b64toString(code) {
var decoded = Utilities.base64Decode(code);
var finalString = Utilities.newBlob(decoded).getDataAsString();
return finalString;
}