我正在尝试编写一个简单的Google Apps脚本来列出Google Team Drive中的文件,但我没有取得多大成功。
以下是代码:
function start()
{
Logger.log("Starting application...");
var startingFolders = DriveApp.getFoldersByName("Temp");
if (startingFolders.hasNext())
{
// Assuming only one folder with that name
accessFiles(startingFolders.next());
}
else
Logger.log("Folder not found");
}
function accessFiles(folder)
{
Logger.log("Folder: %s", folder.getName());
// Print some file properties
var files = folder.getFiles();
while (files.hasNext())
{
var file = files.next();
Logger.log("Working on file %s. Current access: %s.", file.getName(), file.getSharingAccess());
// some work here...
}
// Explore subfolders
while (folder.hasNext())
{
var subfolder = folder.next();
accessFiles(subfolder);
}
}
问题是日志始终打印“找不到文件夹”消息。 “Temp”是我拥有完全访问权限的团队驱动器中的文件夹。请注意,我正在尝试使用Google Apps脚本,而不是REST API。
我不确定我做错了什么,或者是否支持Team Drives ......
任何帮助将不胜感激!感谢。
答案 0 :(得分:1)
使用Google应用脚本服务(DriveApp)无法在Team Drive上搜索文件和文件夹,但我们可以使用Drive Advanced Service,但首先应首先启用它。说明在Enabling Advanced Services。
以下脚本将通过其ID列出指定Team Drive中名为Temp的文件夹内的所有文件。
function listFiles(){
var teamDriveId = 'put_here_the_teamdrive_id';
var pageToken;
var folders = Drive.Files.list({
corpora: 'teamDrive',
supportsTeamDrives: true,
teamDriveId: teamDriveId,
includeTeamDriveItems: true,
q: 'title = "Temp"'
});
if(folders.items.length !== 1) {
Logger.log('There is a problem.');
return;
}
var query = 'trashed = false and ' + //to exclude trashed files
'not mimeType = "application/vnd.google-apps.folder"'; // To exclude folders
var files, pageToken;
do {
files = Drive.Files.list({
q: query,
maxResults: 100,
pageToken: pageToken,
// required for team drive queries
corpora: 'teamDrive',
supportsTeamDrives: true,
teamDriveId: teamDriveId,
includeTeamDriveItems: true
});
if (files.items && files.items.length > 0) {
for (var i = 0; i < files.items.length; i++) {
var file = files.items[i];
Logger.log('%s (ID: %s)', file.title, file.id);
}
} else {
Logger.log('No files found.');
}
pageToken = files.nextPageToken;
} while (pageToken);
}