I am listed as a manager on a shared drive in our organisation. I can view, open and edit the files and folders in Google Drive using the browser. The folder I am interested in contains 2 folders and a number of various filetypes.
I am using Apps Script in Google Sheets to get a list of files and folders that meet certain conditions. When I tried to get a list of the files and folders from my target folder using Google Drive API (V2), the script could see 0 files and 0 folders. When I used the built-in DriveApp service, it timed out for what I was trying to achieve; so thought Google Drive API should hopefully be quicker. I can confirm that DriveApp could see the folder titles - I used that for testing in the Logger log.
This test code is supposed to return the number of folders in a given folder (and confirm the account used). Can anyone advise why I am getting the number of folders return as 0 even though I have organizer-level permissions, the Drive Service active and the correct Folder ID ? There are definitely 2 folders in my test target that I have permissions for and am not getting any error messages.
function countFolderFolders() {
// Create a Drive API client
var folderId = 'ABCD123'; // AMEND to suit
var driveApi;
try {
driveApi = Drive.Files.list({
q: "'" + folderId + "' in parents and trashed = false and mimeType = 'application/vnd.google-apps.folder'", // Query - count folders in the specified folder
maxResults: 1000
});
} catch (e) {
Logger.log('Drive API Error: ' + e.toString());
return;
}
if (driveApi && driveApi.items) {
// Count the number of folders
var folderCount = 0;
for (var i = 0; i < driveApi.items.length; i++) {
var item = driveApi.items[i];
if (item.mimeType === "application/vnd.google-apps.folder") {
folderCount++;
}
}
Logger.log('The number of folders in the folder with the ID "' + folderId + '" is: ' + folderCount);
} else {
Logger.log('No folders found or error occurred.');
}
testAccount();
}
function testAccount() {
var email = Session.getActiveUser().getEmail();
Logger.log('The script is being run under the account: ' + email);
}
In your showing script, how about the following modification?
driveApi = Drive.Files.list({
q: "'" + folderId + "' in parents and trashed = false and mimeType = 'application/vnd.google-apps.folder'", // Query - count folders in the specified folder
maxResults: 1000
});
driveApi = Drive.Files.list({
q: "'" + folderId + "' in parents and trashed = false and mimeType = 'application/vnd.google-apps.folder'", // Query - count folders in the specified folder
maxResults: 1000,
corpora: "allDrives",
includeItemsFromAllDrives: true,
supportsAllDrives: true
});
In this case, it supposes that you have permission to read the files and folders from the shared drive. Please be careful about this.
By the way, from your search query of "'" + folderId + "' in parents and trashed = false and mimeType = 'application/vnd.google-apps.folder'"
, in this case, only the folders are retrieved. So, I thought that the following modification might be able to be used.
From
if (driveApi && driveApi.items) {
// Count the number of folders
var folderCount = 0;
for (var i = 0; i < driveApi.items.length; i++) {
var item = driveApi.items[i];
if (item.mimeType === "application/vnd.google-apps.folder") {
folderCount++;
}
}
Logger.log('The number of folders in the folder with the ID "' + folderId + '" is: ' + folderCount);
} else {
Logger.log('No folders found or error occurred.');
}
To
if (driveApi && driveApi.items) {
// Count the number of folders
var folderCount = driveApi.items.length;
Logger.log('The number of folders in the folder with the ID "' + folderId + '" is: ' + folderCount);
} else {
Logger.log('No folders found or error occurred.');
}