Skip to content

Instantly share code, notes, and snippets.

@soham2008xyz
Last active September 15, 2022 17:16
Show Gist options
  • Save soham2008xyz/0a29cdb68be216f3532a59f06ef52353 to your computer and use it in GitHub Desktop.
Save soham2008xyz/0a29cdb68be216f3532a59f06ef52353 to your computer and use it in GitHub Desktop.
Reset permissions for Google Drive documents in all subfolders

IMPORTANT! Before running this on any of your folders, make sure you test it on another account. Although the script seems to be working as intended, I cannot make any guarantee that it will not mess up your permissions. So go through the script carefully, check with the documentation for Google Apps Scripts, and test it thoroughly before running on any production data.

The script's UI is very rudimentary, so don't except anything fancy.

To install this script, go to your Google Drive and click New → Google Apps Script. Paste in the code, and Publish → Distribute as web-app. Set the script to Run as the logged-in user.

When invoking the app, you will be shown the folder hierarchy of your Google Drive. Clicking on a folder will display an ugly-looking popup, where you can choose to execute a permission reset script for the folder and its subfolders (the function doResetPermissions in the code).

This function iterates through the folder and its subfolders. For each file it finds, it executes file.setSharing with values from the parent folder. It also calls file.removeViewer for all viewers registered, and file.removeEditor for all registered editors.

Check the documentation for removeViewer and removeEditor to be sure they do what you want.

Note that executing the script may take some time (depending on the size of your folder structure), and no feedback is given while it's working, except in the script runner logs (View → Logs).

var selectedFolder;
var dialog;
function doGet() {
var app = UiApp.createApplication();
var heading = app.createHTML("<h1>Folders for</h1>");
app.add(heading);
var allFoldersIterator = DriveApp.getFolders();
var text = app.createTextArea().setName("text").setId("output");
app.add(text);
var tree = app.createTree();
var rootFolder = DriveApp.getRootFolder();
var treeRoot = app.createTreeItem(rootFolder.getName()).setId(rootFolder.getId());
var handler = app.createServerHandler("folderSelected");
tree.addItem(treeRoot);
tree.addSelectionHandler(handler);
treeRoot.setState(true);
createBranch(rootFolder, treeRoot, app);
app.add(tree);
var selectedFolderIdBox = app.createTextBox().setName("selectedFolderIdBox").setId("selectedFolderIdBox");
app.add(selectedFolderIdBox);
return app;
}
function createBranch(folder, branch, app) {
var children = folder.getFolders();
while (children.hasNext()) {
child = children.next();
Logger.log("Folder id: " + child.getId());
var subBranch = app.createTreeItem(child.getName()).setId(child.getId());
branch.addItem(subBranch);
createBranch(child, subBranch, app);
}
}
function folderSelected(eventInfo) {
var app = UiApp.getActiveApplication();
var parameter = eventInfo.parameter;
Logger.log("Selected " + eventInfo.parameter.selected);
selectedFolder = DriveApp.getFolderById(eventInfo.parameter.selected);
var selectedFolderIdBox = app.getElementById("selectedFolderIdBox");
selectedFolderIdBox.setValue(eventInfo.parameter.selected);
var okHandler = app.createServerHandler("okResetPermissions");
okHandler.addCallbackElement(selectedFolderIdBox);
dialog = app.createDialogBox(true, true);
var panel = app.createFlowPanel();
dialog.add(panel);
dialog.setId("confirmationDialog");
dialog.setPopupPosition(100, 100).setSize(500, 500);
panel.add(app.createLabel("Do you want to reset permissions for all files found in folder " + selectedFolder.getName() + " and its subfolders?\n (Click outside of the dialog box to cancel)"));
panel.add(app.createButton("Do it!", okHandler));
dialog.show();
return app;
}
function okResetPermissions(eventInfo) {
var selectedFolderId = eventInfo.parameter.selectedFolderIdBox;
var selectedFolder = DriveApp.getFolderById(selectedFolderId);
Logger.log("Resetting permissions on " + selectedFolder.getName());
var app = UiApp.getActiveApplication();
app.getElementById("confirmationDialog").hide();
Logger.log("Confirmation dialog hidden");
var progressIndicator = app.createDialogBox(false, true);
progressIndicator.setId("progressIndicator");
progressIndicator.setPopupPosition(100, 100).setSize(500, 500);
var label = app.createLabel("Working ...");
progressIndicator.add(label);
progressIndicator.show();
doResetPermissions(selectedFolder, function() {
var app = UiApp.getActiveApplication();
var progressIndicator = app.getElementById("progressIndicator");
progressIndicator.hide();
return app;
});
return app;
}
function doResetPermissions(selectedFolder, callback) {
Logger.log("Resetting permissions on folder " + selectedFolder.getName());
var sharingAccess = selectedFolder.getSharingAccess();
var sharingPermission = selectedFolder.getSharingPermission();
var files = selectedFolder.getFiles();
while (files.hasNext()) {
var file = files.next();
Logger.log(" Resetting permissions on file " + file.getName());
file.setSharing(sharingAccess, sharingPermission);
var viewers = file.getViewers();
for (var i = 0; i < viewers.length; i++) {
Logger.log(" Removing viewer " + viewers[i].getEmail());
file.removeViewer(viewers[i]);
}
var editors = file.getEditors();
for (var i = 0; i < editors.length; i++) {
Logger.log(" Removing editor " + editors[i].getEmail());
file.removeEditor(editors[i]);
}
}
var children = selectedFolder.getFolders();
while (children.hasNext()) {
var child = children.next();
doResetPermissions(child);
}
if (callback) {
callback();
}
}
Copy link

ghost commented Jun 14, 2022

r
Exception: UiApp has been deprecated. Please use HtmlService instead.
doGet @ Código.gs:5
Any Solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment