我一直在尝试使用我在其中一个答案上看到的 Apps Script 片段来实现上述目标(顺便说一句,我不确定我是否应该在那个线程上问这个问题,或者是否可以打开一个新的,我是新的这里 :))。
它在应用程序中,它在文件上工作得很好,但对文件夹造成了破坏。
我的问题/问题是:1)在每次所有权更改时,驱动器都会通知用户,我不需要这个,知道如何关闭它吗?
2)文件夹是在新的所有者根文件夹中“创建”的,并且是扁平的,这意味着,如果我有一个包含 8 级子文件夹的树并且每个子文件夹中有 8 个文件夹,我将在根文件夹中获得 64 个文件夹新主人开车。这些只是“链接”,它们与适当的树结构一起出现。
任何想法,建议?
这就是我使用的代码,它可能有点粗糙,我完全是一个脚本新手:)
function main()
{
var rootFolder = DocsList.getFolder('TsT Fol1');
var subFiles = [];
//var subFolderz = [];
subFiles = getFilesInFolder(rootFolder,subFiles);
//subFolderz = GetSubFolders(rootFolder,subFolderz);
for (var i = 0; i < subFiles.length; i++)
{
filename = subFiles[i].getName();
Logger.log(filename);
Logger.log(subFiles[i].getId());
changeOwner
("DestUse@Company.com",subFiles[i].getId());
}
for (var i = 0; i < subFolderz.length; i++)
{
foldername = subFolderz[i].getName();
Logger.log(foldername);
Logger.log(subFolderz[i].getId());
changeFolderOwner
("DestUse@Company.com",subFolderz[i].getId());
}
}
function getFilesInFolder(rootFolder,subFiles) {
var subFolders = rootFolder.getFolders();
var foldersName;
subFiles = subFiles.concat(rootFolder.getFiles())
if (subFolders.length == 0) {return subFiles;}
for (var i = 0; i < subFolders.length; i++)
{
foldersName = subFolders[i];
subFiles = getFilesInFolder(foldersName, subFiles);
}
return subFiles;
};
function GetSubFolders(rootFolder,subFolderz) {
var subFolders = rootFolder.getFolders();
var foldersName;
subFolderz = subFolderz.concat(rootFolder.getFolders())
if (subFolders.length == 0) {return subFolderz;}
for (var i = 0; i < subFolders.length; i++)
{
foldersName = subFolders[i];
subFolderz = GetSubFolders(foldersName, subFolderz);
}
return subFolderz;
};
function changeOwner(newOwnerEmail, fileOrFolderId){
var file = DocsList.getFileById(fileOrFolderId);
var file = DocsList.getFolderById(fileOrFolderId)
var oldOwnerEmail = file.getOwner().getEmail();
if (oldOwnerEmail === newOwnerEmail) {
return;
}
file.removeEditor(newOwnerEmail);
var base = 'https://docs.google.com/feeds/';
var fetchArgs = googleOAuth_('docs', base);
fetchArgs.method = 'POST';
var rawXml = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007'>"
+"<category scheme='http://schemas.google.com/g/2005#kind' "
+"term='http://schemas.google.com/acl/2007#accessRule'/>"
+"<gAcl:role value='owner'/>"
+"<gAcl:scope type='user' value='"+newOwnerEmail+"'/>"
+"</entry>";
fetchArgs.payload = rawXml;
fetchArgs.contentType = 'application/atom+xml';
var url = base + encodeURIComponent(oldOwnerEmail) + '/private/full/'+fileOrFolderId+'/acl?v=3&alt=json';
var content = UrlFetchApp.fetch(url, fetchArgs).getContentText();
}
function changeFolderOwner(newOwnerEmail, fileOrFolderId){
var folder = DocsList.getFolderById(fileOrFolderId)
var oldOwnerEmail = folder.getOwner().getEmail();
if (oldOwnerEmail === newOwnerEmail) {
return;
}
folder.removeEditor(newOwnerEmail);
var base = 'https://docs.google.com/feeds/';
var fetchArgs = googleOAuth_('docs', base);
fetchArgs.method = 'POST';
var rawXml = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007'>"
+"<category scheme='http://schemas.google.com/g/2005#kind' "
+"term='http://schemas.google.com/acl/2007#accessRule'/>"
+"<gAcl:role value='owner'/>"
+"<gAcl:scope type='user' value='"+newOwnerEmail+"'/>"
+"</entry>";
fetchArgs.payload = rawXml;
fetchArgs.contentType = 'application/atom+xml';
var url = base + encodeURIComponent(oldOwnerEmail) + '/private/full/'+fileOrFolderId+'/acl?v=3&alt=json';
var content = UrlFetchApp.fetch(url, fetchArgs).getContentText();
}
function googleOAuth_(name,scope) {
var oAuthConfig = UrlFetchApp.addOAuthService(name);
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setConsumerKey("anonymous");
oAuthConfig.setConsumerSecret("anonymous");
return {oAuthServiceName:name, oAuthUseToken:"always"};
}