browse files in google team drive

风格不统一 提交于 2019-11-26 22:25:56

问题


I am trying to write a simple Google Apps Script to list the files in a Google Team Drive and I am not having much success.

Here is the code:

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);
  }
}

The problem is that the log always prints the "Folder not found" message. "Temp" is a folder in a Team Drive that I have Full access to. Note that I am trying to use the Google Apps Script and not the REST API.

I am not sure what I am doing wrong, or if Team Drives are not supported yet...

Any help would be appreciated! Thanks.


回答1:


It's not possible to search file and folders on a Team Drive by using the Google apps Script Service (DriveApp) but we could use the Drive Advanced Service but first we should enabled it first. The instructions are on Enabling Advanced Services.

The following script will list all the files inside a folder named Temp from an specified Team Drive by its id.

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);
}


来源:https://stackoverflow.com/questions/48293530/browse-files-in-google-team-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!