Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created November 29, 2022 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/5be73060a7941d73ce7179c6fa77206a to your computer and use it in GitHub Desktop.
Save tanaikech/5be73060a7941d73ce7179c6fa77206a to your computer and use it in GitHub Desktop.
Retrieving Specific Folders from Google Drive using Google Apps Script

Retrieving Specific Folders from Google Drive using Google Apps Script

These are sample scripts for retrieving specific folders from Google Drive using Google Drive service (DriveApp) with Google Apps Script.

Retrieving folders in own Google Drive

const folders = DriveApp.searchFolders(
  `'${Session.getActiveUser().getEmail()}' in owners and trashed=false`
);
const res = [];
while (folders.hasNext()) {
  const folder = folders.next();
  res.push(folder.getName());
}
console.log(res);

Retrieving folders in shared Drives

const folders = DriveApp.searchFolders(
  `not '${Session.getActiveUser().getEmail()}' in owners and trashed=false`
);
const res = [];
while (folders.hasNext()) {
  const folder = folders.next();
  const owner = folder.getOwner();
  if (owner === null) {
    res.push(folder.getName());
  }
}
console.log(res);

Retrieving folders of sharedWithMe

const folders = DriveApp.searchFolders(`sharedWithMe`);
const res = [];
while (folders.hasNext()) {
  const folder = folders.next();
  res.push(folder.getName());
}
console.log(res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment