我创建了一个Google Apps脚本网络应用程序,该应用程序应该与您想要的接近。
重要!在任何文件夹上运行此文件之前,请确保在另一个帐户上对其进行测试。尽管脚本似乎按预期运行,但我不能保证它不会破坏您的权限。因此,请仔细阅读该脚本,查看Google Apps脚本的文档,并在对任何生产数据运行之前对其进行彻底的测试。
脚本的用户界面非常初级,因此不要花哨。
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();
}
}
要安装此脚本,请转到您的Google云端硬盘,然后点击新建→Google Apps脚本。粘贴代码,然后将发布→发布为web-app。
将脚本设置为以登录用户 身份运行(或类似的方式,我的Google云端硬盘不是英语)。
调用该应用程序时,将显示Google云端硬盘的文件夹层次结构。单击一个文件夹将显示一个难看的弹出窗口,您可以在其中选择对该文件夹及其子文件夹(代码中的功能)执行权限重置脚本doResetPermissions
。
此功能遍历文件夹及其子文件夹。对于找到的每个文件,它将file.setSharing
使用父文件夹中的值执行。它还要求file.removeViewer
所有已注册的观看者和file.removeEditor
所有已注册的编辑者。
检查removeViewer和removeEditor的文档,以确保它们可以执行所需的操作。
请注意,执行脚本可能要花费一些时间(取决于文件夹结构的大小),并且在工作期间不会给出任何反馈,除非脚本运行程序日志(查看→日志)中给出任何反馈。