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.
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 own Google Drive and shared Drives.
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.
1dBxqh6gpWqFv4h0M25ALp4bhcs6CoqhiIJ0fUeUjR57bAxIN2PqfkQns
.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, accessToken) | 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, accessToken) | Retrieve all files and folders of all level under folderId. All files and folders are included in an array without the level. |
getAllFoldersInFolder(folderId, accessToken) | Retrieve all folders of all level under folderId. |
getFilesAndFoldersInFolder(folderId, mimeType, fields, accessToken) | Retrieve files and folders just under a folder with folderId. |
["application/vnd.google-apps.script", "image/png"]
null
for this, all kinds of files are retrieved.fields
which is used at files.list of Drive API.
"files(id,name)"
ScriptApp.getOAuthToken()
, please use this. When this is not used, ScriptApp.getOAuthToken()
is used as the default access token. For example, when this is used, the file list in Drive of the service account can be retrieved.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" }
]
As the libraries "GetFileList" for other languages, there are following libraries.
If you have any questions and commissions for me, feel free to tell me.
v1.0.0 (June 15, 2018)
v1.0.1 (September 3, 2018)
v1.1.0 (May 14, 2020)
###
of const res = FilesApp.createTree("###")
, the folder tree of the shared Drive is retrieved, if you have the shared Drive.v1.1.1 (May 14, 2020)
options
was removed. Because in this library, the file list is retrieved under the specific folder, and this library doesn't directly search the specific files.v1.1.2 (May 29, 2020)
v1.1.3 (December 12, 2020)
ScriptApp.getOAuthToken()
is used as the default access token. By this, the service account can be used.