编辑:
请参阅issue 2756,注意管理员无法通过 Google Apps 脚本更改文件的所有权:
...这与您无法更改自己不属于您的文件的事实有关。任何非法 ACL 更改都会发生此错误,例如当您是查看器时尝试调用 addEditor()。
要更改不属于管理员的文件的所有权,他们必须使用Google Drive SDK,通过OAuth进行身份验证。
这当然是可能的,尽管仅适用于运行脚本的用户拥有的文件。
这是一个脚本,它将在文件夹中找到当前用户拥有的所有Science Shared
文件,并将所有权转移给 user science-shared
。它被设计为包含电子表格的脚本,可创建自定义菜单并使用电子表格浏览器 UI。将电子表格放入您的共享目录,任何老师都应该可以使用它来批发传输自己的文件。
管理员应该能够使用脚本来更改任何教师的文件 - 只需收集 idorigOwner
并将其传递给chownFilesInFolder
.
警告:它只处理文件,而不是子目录 - 如果需要,您可以扩展它。
/**
* Find all files owned by current user in given folder,
* and change their ownership to newOwner.
* Note: sub-folders are untouched
*/
function chownFilesInFolder(folderId,origOwner,newOwner) {
var folder = DriveApp.getFolderById(folderId);
var contents = folder.getFiles();
while(contents.hasNext()) {
var file = contents.next();
var name = file.getName();
var owner = file.getOwner().getEmail();
// Note: domain security policies may block access to user's emails
// If so, this will return a blank string - good enough for our purposes.
if (owner == origOwner) {
// Found a file owned by current user - change ownership
Logger.log(name);
//file.setOwner(newOwner);
}
}
};
/**
* Spreadsheet browser-based UI driver for chownFilesInFolder()
*/
function changeOwnership() {
var resp = Browser.msgBox("Transfer 'Science Shared' files",
"Are you sure you want to transfer ownership of all your shared files?",
Browser.Buttons.YES_NO);
if (resp == "yes") {
var folderName = "Science Shared";
// Assume there is just one "Science Shared" folder.
var folder = DriveApp.getFoldersByName(folderName);
if (!folder.hasNext()) {
throw new Error("Folder not found ("+folderName+")");
}
else {
var folderId = folder.next().getId();
var origOwner = Session.getActiveUser().getEmail(); // Operate on own files
var newOwner = "science_shared@example.com";
chownFilesInFolder(folderId,origOwner,newOwner);
Browser.msgBox("Operation completed", Browser.Buttons.OK);
}
}
else Browser.msgBox("Operation aborted", Browser.Buttons.OK);
}
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
*/
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Transfer 'Science Shared' files",
functionName : "changeOwnership"
}];
spreadsheet.addMenu("Science-Shared", entries);
};
我希望这可以帮助您入门。