我在 Google Drive 上有一个文件夹结构,我正在尝试使用 Google Apps 脚本调用 Drive API v2 与外部用户共享。有时他们有 Google 帐户,但有时他们是“访客”,所以我需要使用 Drive.Permissions.insert 来为他们提供安全访问权限。
问题是我不想共享所有的子文件夹,只是一小部分。而且,当然,共享“根”文件夹是其他文件夹的递归。所以,我试图删除我授予的一些权限。如果不使用 API,我显然无法做到这一点(似乎 API 授予的权限必须通过 API 或使用 GUI 撤销,后者我没有这样做)。我只是找不到一种优雅的方法来获取我刚刚插入的权限的权限 ID。
这是我的代码:
function shareFolders() {
var email = "[email protected]"
var searchTerm = "FileNumber####";
var filenoFolders = DriveApp.searchFolders("title contains '"+searchTerm.replace("'","\'")+"' and trashed = false and hidden = false");
var filenoFolder = filenoFolders.next();
var internalDocsFolder = filenoFolder.getFoldersByName("Internal Documents").next();
var subfolders = internalDocsFolder.getFolders();
var incomingDocsFolder = internalDocsFolder.getFoldersByName("Disclosure").next().getFoldersByName("Our Client").next().getFoldersByName("Incoming Documents").next()
var resourceView = {
"role": "reader",
"type": "user",
"value": email
};
var resourceEdit = {
"role": "writer",
"type": "user",
"value": email
};
//Sharing
try {
Drive.Permissions.insert(resourceView, filenoFolder.getId());
Utilities.sleep(3000);
while (subfolders.hasNext()){
var subfolder = subfolders.next()
if (subfolder.getName() == "Disclosure" || subfolder.getName() == "Court Documents" || subfolder.getName() == "Searches") {
console.log(subfolder.getName() + " - No Viewership Change Required");
}
else {
console.log(subfolder.getName() + " - Removing Viewer");
Drive.Permissions.delete(subfolder.getId(), resourceView); //This Doesn't Work
}
};
Utilities.sleep(3000);
Drive.Permissions.insert(resourceEdit, incomingDocsFolder.getId());
}
catch(e){
console.log("Error Caught: " + e)
};
};
注意:我们不为您编写代码,但您可以查看下面的示例调整脚本作为参考,以指导您进行实际项目。我在脚本上添加了评论以获得更多上下文。
为了能够使用权限ID删除权限,您可以通过插入权限调用返回的响应检索此ID(参见下面的
insertPermission
函数)。该响应中的 id
属性实际上是 Permission ID。
据我了解,根据您的评论和脚本,这是您想要的流程:
resourceView
权限插入Google Drive文件夹Internal Documents
.resourceView
文件夹的子文件夹/子文件夹(添加到noChange
变量)中删除继承的Internal Documents
权限。resourceEdit
权限插入incomingFolders
变量中添加的文件夹。var stat = 0; //Checks if a subfolder contains subfolder inside. 0 means that the subfolder doesn't contain any subfolders, 1 means there are sub folders within a sub folder (multiple nested folder).
var permissionID;
var email = "[email protected]"
var resourceView = {
"role": "reader",
"type": "user",
"value": email
};
var resourceEdit = {
"role": "writer",
"type": "user",
"value": email
};
//Remove the inherited 'resourceView' permission from these folders.
var noChange = [
'Disclosure',
'Court Documents',
'Searches'
];
//Add 'resourceEdit' permission into these folders.
var incomingFolders = [
'Our Client',
'Incoming Documents'
];
function shareFolders() {
var searchTerm = "FileNumber####";
var filenoFolders = DriveApp.searchFolders("title contains '" + searchTerm.replace("'", "\'") + "' and trashed = false and hidden = false");
var folderData = Drive.Children.list(filenoFolders.next().getId())
var folders = folderData.items;
//Loop through the contents of the folder & check each sub folders / child folders.
for (var i = 0; i < folders.length; i++) {
var id = folders[i].id;
var folder = Drive.Files.get(id);
//Capture a folder named "Internal Documents", insert a permission & check it's sub folders.
if (folder.mimeType == 'application/vnd.google-apps.folder' & folder.title == 'Internal Documents') {
insertPermission(resourceView, folder); //Inserts permission from "resourceView" into the 'Internal Documents' folder
viewSubFolders(folder);
}
}
}
/**
* Insert permission
*/
function insertPermission(resource, folder) {
var res = Drive.Permissions.insert(resource, folder.getId());
permissionID = res.id // store the permission ID to the permsionID variable to be used
console.log(`Inserted permission to folder "${folder.title}" \nPermission ID: ${permissionID}`);
}
/**
* Delete permission
*/
function deletePermission(folder, permissionID) {
Drive.Permissions.remove(folder.id, permissionID);
console.log(`Removed permission in "${folder.title}"`)
}
/**
* View the contents of the folder & specifically check if a sub/child folder exists.
*/
function viewSubFolders(folder) {
console.log(`> Found \"${folder.title}\" folder...`)
var folderData = Drive.Children.list(folder.id)
var files = folderData.items;
//Find a sub folder
for (var i = 0; i < files.length; i++) {
var id = files[i].id;
var folder = Drive.Files.get(id);
//Capture the subfolder & run the script called "Peek"
if (folder.mimeType == 'application/vnd.google-apps.folder') {
peek(folder) > 0 ? peek(folder) : null
}
}
}
/**
* Review the child folder the
* */
function peek(folder) {
console.log(`Sub-folder \"${folder.title}\" found`);
//Removes "resourceView" permission in 'noChange' variable folders.
noChange.join('').match(folder.title) ? deletePermission(folder, permissionID) : null;
//Inserts "resourceEdit" permission to the 'incomingFolders' variable.
incomingFolders.join(' ').match(folder.title) ? insertPermission(resourceEdit, folder) : null;
//Captures if current subfolder contains abother subfolder. This will only show in the logs for review in case you want to review (nested sub folders).
var folderData = Drive.Children.list(folder.id)
var files = folderData.items;
for (var i = 0; i < files.length; i++) {
var id = files[i].id;
var folder = Drive.Files.get(id);
if (folder.mimeType == 'application/vnd.google-apps.folder') {
viewSubFolders(folder)
stat += 1
} else {
stat = 0
}
}
return stat;
}
测试内部文件文件夹及其子文件夹/文件:
运行脚本后。
内部文档具有
resourceView
共享权限:
子文件夹/子文件夹(在
noChange
变量中添加)没有从父文件夹继承resourceView
共享权限(Internal Documents文件夹):
在incomingFolders
中添加的其他子/子文件夹具有
resourceEdit
权限:
Internal Documents文件夹的其余内容将继承
resourceView
权限。
示例执行日志:
好吧,我已经浏览了代码,可以说这段代码使用 Google Apps Script 和 Drive API v2 与外部用户共享 Google Drive 上的特定文件夹。该脚本根据特定的文件编号搜索根文件夹,然后识别需要与外部用户共享的子文件夹。然后该脚本使用 Drive.Permissions.insert 为外部用户添加权限以根据需要查看或编辑文件夹。
但是,脚本在尝试删除之前授予的权限时遇到了问题。该代码尝试使用 Drive.Permissions.remove 和 Drive.Permissions.delete 分别从根文件夹和子文件夹中删除权限,但这些功能似乎没有按预期工作。
看来这个问题可能与之前授予的权限没有正确的权限ID有关。如果没有正确的权限 ID,则无法使用 API 移除或删除权限。
要解决此问题,脚本可能需要先检索文件夹的现有权限,然后在尝试移除或删除权限时使用返回的权限 ID。或者,可以使用 Drive API 撤销给定用户对特定文件夹或子文件夹的所有权限,而不是尝试删除单个权限。