ドライブしたい(DriveApp
)
1const folder = DriveApp.getFolderById("フォルダーID");
2const files = folder.getFiles(); // FileIterator
3const subFolders = folder.getFolders(); // FolderIterator
4const parent = folder.getParents()
DriveApp.getFolderByID
で指定したIDのフォルダを取得できます。
そのフォルダ内のアイテム(ファイルとサブフォルダ)は、それぞれ
getFiles()
とgetFolders()
で取得できます
また、親ディレクトリはgetParents()
で取得できます。
フォルダ内のファイルを取得したい(getFiles
)
1function listAllFiles(folder) {
2 const files = folder.getFiles(); // フォルダ内のファイルを FileIterator としてを取得
3 while (files.hasNext()) {
4 const file = files.nex();
5 Logger.log(file.getName());
6 }
7}
8
9const folderId = "フォルダのID";
10const folder = DriveApp.getFolderById(folderId);
11listAllFiles(folder);
getFiles()
で、その中にあるすべてのファイルを取得できます。
ただし、戻り値がFileIterator
となっているため、hasNext()
とnext()
を使って、1件ずつ取り出す必要があります。
1function getFilesAsArray(folder) {
2 const iterators = folder.getFiles();
3
4 const files = []
5 while (iterator.hasNext) {
6 files.push(iterator.next());
7 }
8 return files;
9}
10
11const folderId = "フォルダのID";
12const folder = DriveApp.getFolderById(folderId);
13const files = getAllFilesAsArray(folder);
上記のサンプルではFileItereator
を配列に詰め替えています。
サブフォルダの一覧を取得したい
1function listSubFolders(folder) {
2 const subFolders = folder.getFolders();
3
4 while (folders.hasNext()) {
5 const sub = folders.next();
6 Logger.log(sub.getName())
7 }
8}
9
10const folderId = "フォルダのID";
11const folder = DriveApp.getFolderById(folderId);
12listSubFolders(folder);
getFolders
でフォルダ内のサブフォルダをすべて取得できます。
ただし、戻り値がFolderIterator
となっているため、hasNext()
とnext()
を使って、1件ずつ取り出す必要があります。
フォルダ内を再帰的に取得したい
1function treeFolder(folder, maxDepth, options = {}) {
2 const {
3 startDepth = 0,
4 depth = 0,
5 parentId = null
6 } = options;
7
8 if (depth > maxDepth) return [];
9
10 const result = []
11 const isVisibleDepth = depth >= startDepth;
12
13 // フォルダ自身
14 if (isVisibleDepth) {
15 result.push({
16 type: 'folder',
17 depth,
18 parentId,
19 dateCreated: folder.getDateCreated(),
20 dateUpdated: folder.getLastUpdated(),
21 description: folder.getDescription(),
22 id: folder.getId(),
23 name: folder.getName(),
24 owner: folder.getOwner().getEmail(),
25 sharingAccess: folder.getSharingAccess(), // 共有レベル(ANYONEなど)
26 sharingPermission: folder.getSharingPermission(), // 共有権限(VIEW | EDIT)
27 url: folder.getUrl(),
28 children: [], // あとで再帰的に追加
29 });
30 }
31
32 // ファイルを取得
33 const files = folder.getFiles();
34 const fileList = [];
35 while (files.hasNext()) {
36 const file = files.next();
37 if (isVisibleDepth) {
38 fileList.push({
39 type: 'file',
40 depth,
41 parentId: folder.getId(),
42 dateCreated: file.getDateCreated(),
43 dateUpdated: file.getLastUpdated(),
44 description: file.getDescription(),
45 id: file.getId(),
46 mimeType: file.getMimeType(),
47 name: file.getName(),
48 owner: file.getOwner().getEmail(),
49 sharingAccess: file.getSharingAccess(),
50 sharingPermission: file.getSharingPermission(),
51 size: file.getSize(),
52 url: file.getUrl(),
53 });
54 }
55 }
56
57 // サブフォルダを取得
58 const subFolders = folder.getFolders();
59 const folderList = [];
60 while (subFolders.hasNext()) {
61 const child = subFolders.next()
62 const subFolder = treeFolder(child, maxDepth, {
63 startDepth,
64 depth: depth+1,
65 parentId: folder.getId()
66 });
67 if (subFolder.length > 0) {
68 folderList.push(...subFolder);
69 }
70 }
71
72 // resultに集約
73 if (isVisibleDepth) {
74 result[0].children = [...fileList, ...folderList];
75 return result;
76 } else {
77 return [...fileList, ...folderList];
78 }
79}
80
81const folder = DriveApp.getFolderById("フォルダID");
82const tree = treeFolder(folder, 3);
83console.log(JSON.stringify(tree, null, 2));
指定したフォルダ内のアイテムを、再帰的に拾ってくるサンプルです。
tree
コマンドのように、探索の深さを変更できるようにしてあります。
1function flattenTree(tree) {
2 const flatList = [];
3
4 // nodes: Array
5 function walk(nodes) {
6 for (const node of nodes) {
7 const { children, ...rest } = node;
8 flatList.push(rest);
9
10 if (children && children.length > 0) {
11 walk(children);
12 }
13 }
14 }
15
16 walk(tree);
17 return flatList;
18}
19
20const folder = DriveApp.getFolderById("フォルダID");
21const tree = treeFolder(folder, 3);
22const flat = flattenTree(tree);
23
24console.log(JSON.stringify(flat, null, 2));
ドライブ容量したい
1const limit = DriveApp.getStorageLimit();
2const used = DriveApp.getStorageUser();
ユーザーのドライブ容量を確認できます。
ファイルを共有したい(setSharing
)
1const folder = DriveApp.createFolder("SharedFolder");
2
3// リンクを知っている全員に共有
4// 閲覧権限
5const access = DriveApp.Access.ANYONE_WITH_LINK;
6const permission = DriveApp.Permission.VIEW;
7folder.setSharing(access, permission);
8
9// 限定
10// 編集権限
11const access = DriveApp.Access.PRIVATE;
12const permission = DriveApp.Permission.EDIT;
13folder.setSharing(access, permission);
setSharing
メソッドで、ファイルの共有設定を有効にできます。
引数には共有方法(DriveApp.Access
)と
権限(DriveApp.Permission
)を指定します。
指定できる値とその内容は Enum Access と Enum Permission を参照してください。