-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGetFolderTreeForDriveApp.js
126 lines (120 loc) · 4.16 KB
/
GetFolderTreeForDriveApp.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
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
/**
* ### Description
* Get folder tree using DriveApp (Drive service) from own drive and shared drive.
*
* ref: https://developers.google.com/apps-script/reference/drive
*
* Required scopes:
* - `https://www.googleapis.com/auth/drive.readonly`
*/
class GetFolderTreeForDriveApp {
/**
*
* @param {Object} object Source folder ID.
* @param {String} object.id Source folder ID. Default is root folder.
*/
constructor(object) {
const { id = "root" } = object;
/** @private */
this.id = id;
}
/**
* ### Description
* Get folder tree.
*
* @param {Object} object Object for retrieving folder tree.
* @returns {Array} Array including folder tree.
*/
getTree(object = {}) {
const loop = object => {
const { id = this.id, parents = { ids: [], names: [] }, folders = [] } = object;
const folder = DriveApp.getFolderById(id);
if (parents.ids.length == 0) {
parents.ids.push(id);
parents.names.push(folder.getName());
}
const pid = [...parents.ids];
const pn = [...parents.names];
const fols = folder.getFolders();
const folderList = [];
while (fols.hasNext()) {
const f = fols.next();
folderList.push({ id: f.getId(), treeIds: [...pid, f.getId()], treeNames: [...pn, f.getName()] });
}
if (folderList.length > 0) {
folders.push(...folderList);
folderList.forEach(({ id, treeIds, treeNames }) =>
loop({ id, parents: { ids: treeIds, names: treeNames }, folders })
);
}
return folders.map(({ treeIds, treeNames }) => ({ treeIds, treeNames }));
}
return loop(object);
}
/**
* ### Description
* Get folder tree with files in each folder.
*
* @param {Object} object Object for retrieving folder tree with files.
* @returns {Array} Array including folder tree with files.
*/
getTreeWithFiles(object = {}) {
const loop = object => {
const { id = this.id, parents = { ids: [], names: [] }, folders = [] } = object;
const folder = DriveApp.getFolderById(id);
if (parents.ids.length == 0) {
parents.ids.push(id);
parents.names.push(folder.getName());
folders.push({ id, treeIds: parents.ids, treeNames: parents.names, parent: { folderId: id, folderName: folder.getName() }, fileList: this.getFiles_(folder) });
}
const pid = [...parents.ids];
const pn = [...parents.names];
const fols = folder.getFolders();
const folderList = [];
while (fols.hasNext()) {
const f = fols.next();
const folderId = f.getId();
const folderName = f.getName();
folderList.push({ id: f.getId(), treeIds: [...pid, folderId], treeNames: [...pn, folderName], parent: { folderId, folderName }, fileList: this.getFiles_(f) });
}
if (folderList.length > 0) {
folders.push(...folderList);
folderList.forEach(({ id, treeIds, treeNames }) =>
loop({ id, parents: { ids: treeIds, names: treeNames }, folders })
);
}
return folders.map(({ treeIds, treeNames, parent, fileList }) => ({ treeIds, treeNames, parent, fileList }));
}
return loop(object);
}
/**
* ### Description
* Get filename with path.
*
* @param {String} delimiter Delimiter for showing path. Default is "/".
* @returns {Array} Array including filenames including each path.
*/
getFilenameWithPath(delimiter = "/") {
return this.getTreeWithFiles().flatMap(({ treeNames, fileList }) => {
const path = treeNames.join(delimiter);
return fileList.map(({ name }) => `${path}${delimiter}${name}`);
});
}
/**
* ### Description
* Get files under a folder.
*
* @param {DriveApp.Folder} folder Folder object.
* @returns {Array} Array including files.
* @private
*/
getFiles_(folder) {
const fileList = [];
const files = folder.getFiles();
while (files.hasNext()) {
const file = files.next();
fileList.push({ name: file.getName(), id: file.getId(), mimeType: file.getMimeType() });
}
return fileList;
}
}