Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

browse files in google team drive

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...