Search in GDrive only the first 5 topics

56 Views Asked by At

is there a parameter in GDrive to start a search with "DriveApp.searchFiles". After the first e.g. 10 hits the command stop the searching.

Currently I can only manage to search for all hits, but this takes too long.

Greetings

I try this GScript:

function listAllFiles() {
  var MyAccount = Session.getActiveUser().getEmail();
  var files = DriveApp.searchFiles('title contains "GutenTag" and "' + MyAccount + '" in owners');
  
  while (files.hasNext()) {
    var file = files.next();
    Logger.log(file.getName());
  }
}
1

There are 1 best solutions below

7
SputnikDrunk2 On

Alternative Method [update]

If you would rather not use looping, as this can slow down the DriveApp.searchFiles function in your actual environment, you may want to try using the Google Drive API Advanced service list method and include a pageSize query parameter in the API call request. Here is a sample script:

[Updated]

function listAllFilesWithLimit() {
   var MyAccount = Session.getActiveUser().getEmail();

  const res = Drive.Files.list({
    "pageSize": 5,
    "q": `name contains 'GutenTag' and '${MyAccount}' in owners`
  });

  res.files.forEach(x => console.log(x.name))
}

Prerequisites:

Add the Drive API Advance Service in your Services section on the Apps Script Editor:

enter image description here

Demo

The API request only returned 5 results, rather than retrieving all of the matching files in the drive in a single operation.

enter image description here