Google Drive - Change owner on folder, sub-folders

2019-06-05 11:43发布

I've been trying to achieve the above using an Apps Script snippet I saw on one of the answers (BTW, I am not sure if I should ask this on that thread, or is it ok to open a new one, I am new here :)).

It's in apps, and it working wonderfully on files, but raise havoc with folders.

My problem/questions are: 1) on every ownership change made, the drive is notifying the user, I don't need this, any idea how to shut it down?

2) Folders are been "created" in the new owner root folder, and flat, meaning, if I have a tree with 8 level of sub folders and each has 8 folders in it, I'll get 64 folders in the root of the new owner drive. and those are just "link" they appear along with the appropriate tree structure.

Any idea, suggestions?

Thats the code I used, it may be a bit crude, I am totally a newbie to scriting :)

     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"};
    }

1条回答
时光不老,我们不散
2楼-- · 2019-06-05 12:38

Sorry this answer is late but I figure the answer would be helpful either way.

1) Apps Script doesn't have the ability to suppress the notification email but Google's most recent version of the Drive SDK does allow for this. You just need to append "?sendNotificationEmails=false" to the url that you're sending your request to. You can run a test on the linked page to see exactly what the query string looks like.

2) The folders actually do maintain their structure. There is just the consequence of a user getting ownership of each of those folders explicitly. Since the rights are given one folder at a time, the user gets access one folder at a time so they show up individually in the user's Drive documents. Still, a Drive file/folder's sharing is stored separately from its parent folder metadata so that should be retained as long as the user has access to the folder and its parent.

For a simple example, if you have a 'root' with two subfolders 'dir1' and dir2', the new owner will have ownership of all three folders and they will each show up in the user's Drive list but if they were to click in 'root' they would see that 'dir1' and 'dir2' are still in there.

查看更多
登录 后发表回答