-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolderlisting.js
59 lines (53 loc) · 1.88 KB
/
folderlisting.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
function listFolder() {
// forces this code to this sheet
const testSpreadsSheetID = "1_aL5HOiEVUCf9Zz_qocSRdnd9ZSqBDT9tdke6V4rH_I";
const testSS = SpreadsheetApp.openById(testSpreadsSheetID);
//this is the name of the tab where the info will go
const testSheetname = "Folder-Listing";
const testSheet = testSS.getSheetByName(testSheetname);
// clear anything in the testSheetname
testSheet.clear()
// folder by ID we will raed files from
const folderID = '0B5biGEQ1GLz0U2tLVktJNmFLejg';
//Send the info to the function
var rslt = getListOfDocs(folderID, testSheet);
if (rslt) {
SpreadsheetApp.getActiveSpreadsheet().toast("All done!!", "Success", 5);
}
else {
SpreadsheetApp.getActiveSpreadsheet().toast("Something went wrong!!", "FAIL", 5)
}
}
/**
* Generates a list of doc names and URLs
*
* @param {string} FolderID the id of the folder with the docs
* @param {string} sheet reference to sheet where the info is to entered
* @return {boolean} done returns true if done
*/
function getListOfDocs(FolderID, sheet) {
//try {
var fld = DriveApp.getFolderById(FolderID);
const folderName = fld.getName();
SpreadsheetApp.getActiveSpreadsheet().toast("Listing files in ", "folderName", 5);
Logger.log(`Listing files in: ${folderName}`)
var files = fld.getFiles();
var rslts = [];
while (files.hasNext()) {
var file = files.next();
rslts.push([file.getName(), file.getUrl()]);
}
//alphabatize the list
rslts.sort();
var rng = sheet.getRange(2, sheet.getLastColumn() + 1, rslts.length, 2);
rng.setValues(rslts);
var headers = sheet.getRange(1, sheet.getLastColumn() - 1, 1, 2);
const headerName = "Folder: " + folderName;
headers.setValues([[headerName, 'Link to File']]);
return true;
// }
// catch (err) {
// Logger.log(`Listing files in: ${folderName}==> ${err}`)
// return false;
// }
}