Share Files and Set Permissions in Google Drive

Apps Script lets you share files and folders with specific users, domains, or the general public using file.addEditor(), file.addViewer(), and file.setSharing(). This is useful for automatically sharing reports with clients, granting team members access to new folders, or revoking permissions when a deal closes.

Adding an Editor to a File

function shareWithEditor() { var file = DriveApp.getFileById("YOUR_FILE_ID"); file.addEditor("[email protected]"); Logger.log("Editor access granted to [email protected]"); }

Adding a Viewer to a File

function shareWithViewer() { var file = DriveApp.getFileById("YOUR_FILE_ID"); file.addViewer("[email protected]"); Logger.log("View access granted."); }

Sharing Reports with Customers from Sheet Data

This example reads each closed deal from the Sales Tracker sheet and shares the corresponding report file with the customer as a viewer:

function shareReportsWithCustomers() { var sheet = SpreadsheetApp .getActiveSpreadsheet() .getSheetByName("Sales Tracker"); var data = sheet.getRange(2, 1, sheet.getLastRow() - 1, 8).getValues(); // Columns: [Customer Name, Email, Product, Amount, Region, Sales Rep, Status, Date] var reportsFolder = DriveApp.getFolderById("YOUR_REPORTS_FOLDER_ID"); data.forEach(function(row) { var customerName = row[0]; var customerEmail = row[1]; var status = row[6]; if (status !== "Closed") return; // Only share with closed deals var files = reportsFolder.getFilesByName(customerName + " - Contract.pdf"); if (!files.hasNext()) return; var file = files.next(); file.addViewer(customerEmail); Logger.log("Shared with " + customerEmail + ": " + file.getName()); }); }

With the sample data, [email protected] and [email protected] receive view access (both are Closed), while [email protected] is skipped (In Progress).

Sharing a Folder with an Entire Team

function shareFolderWithSalesReps() { var folder = DriveApp.getFolderById("YOUR_FOLDER_ID"); var salesReps = [ "[email protected]", "[email protected]", "[email protected]" ]; salesReps.forEach(function(email) { folder.addEditor(email); Logger.log("Editor access granted to " + email); }); }

Making a File Publicly Viewable

function makePublic() { var file = DriveApp.getFileById("YOUR_FILE_ID"); file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW); Logger.log("File is now publicly viewable: " + file.getUrl()); }

Removing Access

function revokeAccess() { var file = DriveApp.getFileById("YOUR_FILE_ID"); file.removeViewer("[email protected]"); Logger.log("Access revoked."); }

Use removeEditor() for editors and removeViewer() for viewers. This is useful in offboarding workflows or when contracts expire.