listFolderSharing(folderId, {recursive}) walks a Drive folder and returns rows ready for setValues: Name, URL, Owner, Editors, Viewers, Sharing access, Sharing permission. Use it to audit who can see every file in a folder.
When to use
- A quarterly access review of a client or team folder
- Checking for files left as "anyone with the link"
- A one-time dump before tightening sharing
How to use this snippet
/**
* List sharing on every file in a Drive folder (and, if recursive, subfolders).
* Each data row is [name, url, owner, editors, viewers, sharingAccess, sharingPermission].
* The first row is a header, so you can write the result with one setValues.
*
* getOwner, getEditors, getViewers, getSharingAccess, and getSharingPermission
* can throw on files in a shared drive. Those fields are filled with
* "(unavailable: ...)" instead of aborting the listing.
*
* @param {string} folderId
* @param {{recursive?: boolean}} [opts]
* @return {Array<Array<string>>}
*/
function listFolderSharing(folderId, opts) {
opts = opts || {};
var folder = DriveApp.getFolderById(folderId);
var rows = [['Name', 'URL', 'Owner', 'Editors', 'Viewers', 'Sharing access', 'Sharing permission']];
function emails(users) {
return users.map(function (u) { return u.getEmail(); }).join(', ');
}
function tryGet(fn) {
try { return fn(); } catch (e) { return '(unavailable: ' + e.message + ')'; }
}
function describe(file) {
rows.push([
file.getName(),
file.getUrl(),
tryGet(function () { var o = file.getOwner(); return o ? o.getEmail() : ''; }),
tryGet(function () { return emails(file.getEditors()); }),
tryGet(function () { return emails(file.getViewers()); }),
tryGet(function () { return String(file.getSharingAccess()); }),
tryGet(function () { return String(file.getSharingPermission()); })
]);
}
function walk(f) {
var files = f.getFiles();
while (files.hasNext()) describe(files.next());
if (opts.recursive) {
var subs = f.getFolders();
while (subs.hasNext()) walk(subs.next());
}
}
walk(folder);
return rows;
}
Example
function auditClientFolder() {
var rows = listFolderSharing('FOLDER_ID', { recursive: true });
var tab = SpreadsheetApp.getActive().getSheetByName('Audit');
tab.clearContents();
tab.getRange(1, 1, rows.length, rows[0].length).setValues(rows);
}
Notes
- Sharing access comes from
getSharingAccess()(PRIVATE,DOMAIN,DOMAIN_WITH_LINK,ANYONE_WITH_LINK,ANYONE). Sharing permission isgetSharingPermission()(VIEW,COMMENT,EDIT, orNONE). - My Drive files usually return owner and user lists. Files in a shared drive often throw on those methods because the drive, not a person, owns the file. The row is still written; the failed fields say why.
- Only files are listed, not the folder itself. Shortcuts appear as files. A large tree can hit the six-minute limit — start with
{recursive: false}or a subfolder.
Happy Coding!
