FilesApp is a GAS library for retrieving file and folder list in Google Drive using Google Apps Script (GAS). Also this can create a tree from all files and folders in Google Drive.
When I create some applications using Google Drive, there are often the case which is required to retrieve the file list and folder list. I had prepared the script each time for each case so far. But recently, I thought that if there is a library for retrieving the file and folder list (as a tree), it will be useful for me and other developers. So I created this. If this was useful for your situation, I'm glad.
1dBxqh6gpWqFv4h0M25ALp4bhcs6CoqhiIJ0fUeUjR57bAxIN2PqfkQns
In order to use this library, please install this as a library.
- Install FilesApp library.
- Library's project key is
1dBxqh6gpWqFv4h0M25ALp4bhcs6CoqhiIJ0fUeUjR57bAxIN2PqfkQns
.
- Library's project key is
- FilesApp uses Drive API. Please enable Drive API at API console.
- On the Script Editor
- Resources -> Cloud Platform Project
- Click the lower part of "This script is currently associated with project:"
- On "API APIs&services"
- In "Getting Started", Click "Enable APIs and get credentials like keys".
- Click Library at left side.
- At "Search APIs and services", Input "drive api", Click it.
- Enable "Google Drive API"
- On the Script Editor
Recently, when it enabled APIs, I had an experience that I was made to wait for several minutes for enabling APIs. So when you enabled APIs at API console, if the error related to APIs occurs, please run again after several minutes.
There are 4 methods in this library.
Method | Description |
---|---|
createTree(folderId, mimeType, fields) | Create a file and folder tree. Retrieve all folders of all level under folderId. All files and folders are included in an array with the level. |
getAllInFolder(folderId, mimeType, fields) | Retrieve all files and folders of all level under folderId. All files and folders are included in an array without the level. |
getAllFoldersInFolder(folderId) | Retrieve all folders of all level under folderId. |
getFilesAndFoldersInFolder(folderId, mimeType, fields) | Retrieve files and folders just under a folder with folderId. |
- folderId : Folder ID of top folder you want to retrieve. Please use as a string.
- If you want to retrieve the values under "root", please use "root" for this.
- mimeType : mimeType of files you want to retrieve. Please use as an array like
["application/vnd.google-apps.script", "image/png"]
- If you use
null
for this, all kinds of files are retrieved.
- If you use
- fields : fields of files you want to retrieve. Please use as a string. This value is the same to
fields
which is used at files.list of Drive API.- If you want to retrieve the filename and ID of files, you can use
"files(id,name)"
- If you want to retrieve the filename and ID of files, you can use
In order to explain the following sample script, it supposes that there is this structure in Google Drive. In this case, it doesn't need that "sampleFolder1" is root.
folderId
is the ID of "sampleFolder1". In this sample, the filenames of files with all mimeTypes are retrieved.
When "root" is used for "folderId", all files of Google Drive can be retrieved.
var res = FilesApp.createTree(folderId, null, "files(name)");
The index of the array of folderTreeById
and folderTreeByName
is corresponding to the level of folders.
{
"topFolderId": ["### FolderId of sampleFolder1 ###"],
"topFolderName": ["sampleFolder1"],
"totalFilesAndFolders": 11,
"totalFiles": 4,
"totalFolders": 7,
"files": [
{
"folderTreeById": ["### FolderId of sampleFolder1 ###"],
"folderTreeByName": ["sampleFolder1"],
"filesInFolder": [
{
"name": "Spreadsheet1",
"parents": ["### FolderId of sampleFolder1 ###"]
},
{
"name": "sampleFolder_2b",
"parents": ["### FolderId of sampleFolder1 ###"]
},
{
"name": "sampleFolder_2a",
"parents": ["### FolderId of sampleFolder1 ###"]
}
]
},
{
"folderTreeById": ["### FolderId of sampleFolder1 ###", "### FolderId of sampleFolder_2b ###"],
"folderTreeByName": ["sampleFolder1", "sampleFolder_2b"],
"filesInFolder": [
{
"name": "Spreadsheet4",
"parents": ["### FolderId of sampleFolder_2b ###"]
},
{
"name": "sampleFolder_2b_3b",
"parents": ["### FolderId of sampleFolder_2b ###"]
},
{
"name": "sampleFolder_2b_3a",
"parents": ["### FolderId of sampleFolder_2b ###"]
}
]
},
{
"folderTreeById": ["### FolderId of sampleFolder1 ###", "### FolderId of sampleFolder_2b ###", "### FolderId of sampleFolder_2b_3b ###"],
"folderTreeByName": ["sampleFolder1", "sampleFolder_2b", "sampleFolder_2b_3b"],
"filesInFolder": [
{
"name": "sampleFolder_2b_3b_4a",
"parents": ["### FolderId of sampleFolder_2b_3b ###"]
}
]
},
{
"folderTreeById": ["### FolderId of sampleFolder1 ###", "### FolderId of sampleFolder_2b ###", "### FolderId of sampleFolder_2b_3b ###", "### FolderId of sampleFolder_2b_3a ###"],
"folderTreeByName": ["sampleFolder1", "sampleFolder_2b", "sampleFolder_2b_3b", "sampleFolder_2b_3b_4a"],
"filesInFolder": [
{
"name": "Spreadsheet5",
"parents": ["### FolderId of sampleFolder_2b_3a ###"]
}
]
},
{
"folderTreeById": [ "### FolderId of sampleFolder1 ###", "### FolderId of sampleFolder_2b ###", "### FolderId of sampleFolder_2b_3a ###"],
"folderTreeByName": ["sampleFolder1", "sampleFolder_2b", "sampleFolder_2b_3a"],
"filesInFolder": [
{
"name": "Spreadsheet3",
"parents": ["### FolderId of sampleFolder_2b_3a ###"]
}
]
},
{
"folderTreeById": ["### FolderId of sampleFolder1 ###", "### FolderId of sampleFolder_2a ###"],
"folderTreeByName": ["sampleFolder1", "sampleFolder_2a"],
"filesInFolder": [
{
"name": "Spreadsheet2",
"parents": ["### FolderId of sampleFolder_2a ###"]
},
{
"name": "sampleFolder_2a_3a",
"parents": ["### FolderId of sampleFolder_2a ###"]
}
]
},
{
"folderTreeById": ["### FolderId of sampleFolder1 ###", "### FolderId of sampleFolder_2a ###", "### FolderId of sampleFolder_2a_3a ###"],
"folderTreeByName": ["sampleFolder1", "sampleFolder_2a", "sampleFolder_2a_3a"],
"filesInFolder": []
}
]
}
var res = FilesApp.getAllInFolder(folderId, null, "files(name)");
All files and folders in a folder you set are retrieved. But it is not as a tree. The speed is faster than createTree()
.
[
{"name":"Spreadsheet5"},
{"name":"Spreadsheet4"},
{"name":"Spreadsheet3"},
{"name":"Spreadsheet2"},
{"name":"Spreadsheet1"},
{"name":"sampleFolder_2b_3b_4a"},
{"name":"sampleFolder_2a_3a"},
{"name":"sampleFolder_2b"},
{"name":"sampleFolder_2a"},
{"name":"sampleFolder_2b_3b"},
{"name":"sampleFolder_2b_3a"}
]
var res = FilesApp.getAllFoldersInFolder(folderId);
All folders in a folder you set are retrieved as a tree.
{
"id":[
["### ID of sampleFolder1 ###"],
["### ID of sampleFolder1 ###","### ID of sampleFolder_2b ###"],
["### ID of sampleFolder1 ###","### ID of sampleFolder_2b ###","### ID of sampleFolder_2b_3b ###"],
["### ID of sampleFolder1 ###","### ID of sampleFolder_2b ###","### ID of sampleFolder_2b_3b ###","### ID of sampleFolder_2b_3b_4a ###"],
["### ID of sampleFolder1 ###","### ID of sampleFolder_2b ###","### ID of sampleFolder_2b_3a ###"],
["### ID of sampleFolder1 ###","### ID of sampleFolder_2a ###"],
["### ID of sampleFolder1 ###","### ID of sampleFolder_2a ###","### ID of sampleFolder_2a_3a ###"]
],
"name":[
["sampleFolder1"],
["sampleFolder1","sampleFolder_2b"],
["sampleFolder1","sampleFolder_2b","sampleFolder_2b_3b"],
["sampleFolder1","sampleFolder_2b","sampleFolder_2b_3b","sampleFolder_2b_3b_4a"],
["sampleFolder1","sampleFolder_2b","sampleFolder_2b_3a"],
["sampleFolder1","sampleFolder_2a"],
["sampleFolder1","sampleFolder_2a","sampleFolder_2a_3a"]
]
}
var res = FilesApp.getFilesAndFoldersInFolder(folderId, null, "files(name)");
All files and folders just under a folder you set are retrieved.
[
{"name":"Spreadsheet1"},
{"name":"sampleFolder_2b"},
{"name":"sampleFolder_2a"}
]
If you have any questions and commissions for me, feel free to tell me.
-
v1.0.0 (June 15, 2018)
- Initial release.
-
v1.0.1 (September 3, 2018)
- Removed a bug.
- When there are files and folders without the parents, an error occurred. In this version, this issue was removed.
- Removed a bug.