This is a Javascript library to retrieve the file list with the folder tree from the specific folder (publicly shared folders and own folders) of own Google Drive and shared Drives.
The library for retrieving the file list with the folder tree from the specific folder of Google Drive has already been published for Google Apps Script, golang, node.js and python as GetFileList. Ref Here, this GetFileList was released as the library of Javascript.
-
Retrieve the file list in the shared folder and own folder using the API and the access token with gapi.
-
When an application of the side bar and dialog of Google Docs is created, there is the case that the file list is required to be retrieved. Of course, this can be done using Google Apps Script. But, the side bar and dialog can use Javascript. And this means that v8 engine can be used. If the file list can be retrieved by Javascript, the process cost will become lower than that of the method for using Google Apps Script. This library can be also used such situation.
- This module retrieves all files from a folder in Google Drive.
- All files include the folder structure in Google Drive.
- Only folder tree can be also retrieved.
<script src="getfilelist_js.min.js"></script>
Or, using jsdelivr cdn
<script src="https://cdn.jsdelivr.net/gh/tanaikech/GetFileList_js@master/getfilelist_js.min.js"></script>
Method | Explanation |
---|---|
GetFolderTree(object) | Retrieve only folder structure from a folder |
GetFileList(object) | Retrieve file list with folder structure from a folder |
There are 3 patterns for using this library.
This is a sample script using API key. When you want to retrieve the API key, please do the following flow.
- Login to Google.
- Access to https://console.cloud.google.com/?hl=en.
- Click select project at the right side of "Google Cloud Platform" of upper left of window.
- Click "NEW PROJECT"
- Input "Project Name".
- Click "CREATE".
- Open the created project.
- Click "Enable APIs and get credentials like keys".
- Click "Library" at left side.
- Input "Drive API" in "Search for APIs & Services".
- Click "Google Drive API".
- Click "ENABLE".
- Back to https://console.cloud.google.com/?hl=en.
- Click "Enable APIs and get credentials like keys".
- Click "Credentials" at left side.
- Click "Create credentials" and select API key.
- Copy the API key. You can use this API key.
When the API key is used, the values can be retrieved from only the publicly shared folder. Please be careful this. If you want to retrieve the folder which is not shared in your Google Drive, please use the access token.
<script src="getfilelist_js.min.js"></script>
<script>
const folderId = "###"; // In this case, this is the folder ID of the publicly shared folder.
const resource = {
apiKey: "###",
id: folderId,
fields: "files(id,name)",
};
const fl = new GetFileList();
fl.getFileList(resource)
// or fl.getFolderTree(resource)
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
</script>
- When you want to retrieve the file list from the folder using API key, the folder is required to be shared.
- You can modify the property of
fields
. When this is not used, the default fields are used.
Document of OAuth2 is here.
In this sample script, it supposes that the access token is retrieved by Google APIs Client Library for browser JavaScript, aka gapi.
<script src="getfilelist_js.min.js"></script>
<script>
const folderId = "###"; // In this case, this is the folder ID of the publicly shared folder.
const resource = {
accessToken: gapi.auth.getToken().access_token,
id: folderId,
fields: "files(id,name)",
};
const fl = new GetFileList();
fl.getFileList(resource)
// or fl.getFolderTree(resource)
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
</script>
In this sample script, it supposes that the access token is retrieved by HTML Service of Google Apps Script. Such situation can be used for the dialog and side bar of Google Docs.
<script src="https://cdn.jsdelivr.net/gh/tanaikech/GetFileList_js@master/getfilelist_js.min.js"></script>
<input type="button" value="Run script" onClick="run()" />
<script>
function run() {
google.script.run
.withSuccessHandler((accessToken) => runGetFileList(accessToken))
.getAuth();
}
function runGetFileList(accessToken) {
const folderId = "###"; // In this case, this is the folder ID of the publicly shared folder.
const resource = {
accessToken: accessToken,
id: folderId,
fields: "files(id,name)",
};
const fl = new GetFileList();
fl.getFileList(resource)
// or fl.getFolderTree(resource)
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
}
</script>
function getAuth() {
return ScriptApp.getOAuthToken();
}
function doGet() {
return HtmlService.createHtmlOutputFromFile("index");
}
// DriveApp.getFiles(); // This is used for the scope.
- If you want to include the script of library at Google Apps Script project, you can also load the library using
<?!= include('getfilelist'); ?>
.
- You can modify the property of
fields
. When this is not used, the default fields are used.
As a sample, when the values are retrieved from above structure, the results of GetFolderTree()
becomes as follows.
const fl = new GetFileList();
fl.getFolderTree(resource)
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
{
"id": [
["folderIdOfsampleFolder1"],
["folderIdOfsampleFolder1", "folderIdOfsampleFolder_2a"],
["folderIdOfsampleFolder1", "folderIdOfsampleFolder_2b"],
[
"folderIdOfsampleFolder1",
"folderIdOfsampleFolder_2a",
"folderIdOfsampleFolder_2a_3a"
],
[
"folderIdOfsampleFolder1",
"folderIdOfsampleFolder_2b",
"folderIdOfsampleFolder_2b_3a"
],
[
"folderIdOfsampleFolder1",
"folderIdOfsampleFolder_2b",
"folderIdOfsampleFolder_2b_3b"
],
[
"folderIdOfsampleFolder1",
"folderIdOfsampleFolder_2b",
"folderIdOfsampleFolder_2b_3b",
"folderIdOfsampleFolder_2b_3b_4a"
]
],
"names": [
"sampleFolder1",
"sampleFolder_2a",
"sampleFolder_2b",
"sampleFolder_2a_3a",
"sampleFolder_2b_3a",
"sampleFolder_2b_3b",
"sampleFolder_2b_3b_4a"
],
"folders": [
"folderIdOfsampleFolder1",
"folderIdOfsampleFolder_2a",
"folderIdOfsampleFolder_2b",
"folderIdOfsampleFolder_2a_3a",
"folderIdOfsampleFolder_2b_3a",
"folderIdOfsampleFolder_2b_3b",
"folderIdOfsampleFolder_2b_3b_4a"
]
}
const fl = new GetFileList();
fl.getFileList(resource)
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
{
"searchedFolder": {
"id": "###",
"name": "sampleFolder1",
"mimeType": "application/vnd.google-apps.folder",
"parents": ["###"],
"createdTime": "2000-01-01T01:23:45.000Z",
"modifiedTime": "2000-01-01T01:23:45.000Z",
"webViewLink": "https://drive.google.com/drive/folders/###",
"owners": [
{ "displayName": "###", "permissionId": "###", "emailAddress": "###" }
],
"shared": true
},
"folderTree": {
"id": [
["folderIdOfsampleFolder1"],
["folderIdOfsampleFolder1", "folderIdOfsampleFolder_2a"],
["folderIdOfsampleFolder1", "folderIdOfsampleFolder_2b"],
[
"folderIdOfsampleFolder1",
"folderIdOfsampleFolder_2a",
"folderIdOfsampleFolder_2a_3a"
],
[
"folderIdOfsampleFolder1",
"folderIdOfsampleFolder_2b",
"folderIdOfsampleFolder_2b_3a"
],
[
"folderIdOfsampleFolder1",
"folderIdOfsampleFolder_2b",
"folderIdOfsampleFolder_2b_3b"
],
[
"folderIdOfsampleFolder1",
"folderIdOfsampleFolder_2b",
"folderIdOfsampleFolder_2b_3b",
"folderIdOfsampleFolder_2b_3b_4a"
]
],
"names": [
"sampleFolder1",
"sampleFolder_2a",
"sampleFolder_2b",
"sampleFolder_2a_3a",
"sampleFolder_2b_3a",
"sampleFolder_2b_3b",
"sampleFolder_2b_3b_4a"
],
"folders": [
"folderIdOfsampleFolder1",
"folderIdOfsampleFolder_2a",
"folderIdOfsampleFolder_2b",
"folderIdOfsampleFolder_2a_3a",
"folderIdOfsampleFolder_2b_3a",
"folderIdOfsampleFolder_2b_3b",
"folderIdOfsampleFolder_2b_3b_4a"
]
},
"fileList": [
{
"folderTree": ["folderIdOfsampleFolder1"],
"files": [
{
"name": "Spreadsheet1",
"mimeType": "application/vnd.google-apps.spreadsheet"
}
]
},
{
"folderTree": ["folderIdOfsampleFolder1", "folderIdOfsampleFolder_2a"],
"files": [
{
"name": "Spreadsheet2",
"mimeType": "application/vnd.google-apps.spreadsheet"
}
]
},
{
"folderTree": ["folderIdOfsampleFolder1", "folderIdOfsampleFolder_2b"],
"files": [
{
"name": "Spreadsheet4",
"mimeType": "application/vnd.google-apps.spreadsheet"
}
]
},
{
"folderTree": [
"folderIdOfsampleFolder1",
"folderIdOfsampleFolder_2a",
"folderIdOfsampleFolder_2a_3a"
],
"files": null
},
{
"folderTree": [
"folderIdOfsampleFolder1",
"folderIdOfsampleFolder_2b",
"folderIdOfsampleFolder_2b_3a"
],
"files": [
{
"name": "Spreadsheet3",
"mimeType": "application/vnd.google-apps.spreadsheet"
}
]
},
{
"folderTree": [
"folderIdOfsampleFolder1",
"folderIdOfsampleFolder_2b",
"folderIdOfsampleFolder_2b_3b"
],
"files": null
},
{
"folderTree": [
"folderIdOfsampleFolder1",
"folderIdOfsampleFolder_2b",
"folderIdOfsampleFolder_2b_3b",
"folderIdOfsampleFolder_2b_3b_4a"
],
"files": [
{
"name": "Document1",
"mimeType": "application/vnd.google-apps.document"
},
{
"name": "image1.png",
"mimeType": "image/png"
},
{
"name": "Slides1",
"mimeType": "application/vnd.google-apps.presentation"
},
{
"name": "Spreadsheet5",
"mimeType": "application/vnd.google-apps.spreadsheet"
},
{
"name": "StandaloneProject1",
"mimeType": "application/vnd.google-apps.script"
},
{
"name": "Test1.txt",
"mimeType": "text/plain"
}
]
}
],
"totalNumberOfFiles": 10,
"totalNumberOfFolders": 7
}
At Google Drive, the files and folders are managed by IDs. For example, one file and one folder can have multiple parent folders. But in this library, the 1st parent folder is retrieved. I thought that at the most cases, one file and folder will have one parent folder. So I used this. If in your environment, one file and folder has multiple parent folder, only 1st parent folder is retrieved. And also, the files and folders which have no parent folders cannot be retrieved. Please be careful this.
As the libraries "GetFileList" for other languages, there are following libraries.
- Golang: https://github.com/tanaikech/go-getfilelist
- Google Apps Script: https://github.com/tanaikech/FilesApp
- Javascript: https://github.com/tanaikech/GetFileList_js
- Node.js: https://github.com/tanaikech/node-getfilelist
- Python: https://github.com/tanaikech/getfilelistpy
If you have any questions and commissions for me, feel free to tell me.
-
v1.0.0 (September 12, 2019)
- Initial release.
-
v1.0.1 (September 30, 2019)
- A bug related to the scope of variables was removed.
-
v1.0.2 (May 15, 2020)
-
Shared drive got to be able to be used. The file list can be retrieved from both your Google Drive and the shared drive.
- For example, when the folder ID in the shared Drive is used
id
ofresource
, you can retrieve the file list from the folder in the shared Drive.
- For example, when the folder ID in the shared Drive is used
-
-
v1.0.3 (June 1, 2020)
- When the file is retrieved from the shared drive, the parameter was not completed. This bug was removed.