Google云端硬盘 - 更改文件夹,子文件夹和内容的所有者,同时保存树状结构

时间:2013-06-10 18:41:02

标签: google-apps-script

我一直在尝试使用我在其中一个答案上看到的Apps脚本片段来实现上述目标(顺便说一句,我不确定我是否应该在该线程上问这个,或者可以打开一个新的,我是新来的:))。

它在应用程序中,它在文件上运行得非常好,但会对文件夹造成严重破坏。

我的问题/疑问是: 1)对每个所有权变更,驱动器通知用户,我不需要这个,任何想法如何关闭它?

2)文件夹已在新的所有者根文件夹中“创建”,并且是平的,这意味着,如果我有一个8级子文件夹的树,每个子文件夹中有8个文件夹,我将获得64个文件夹新所有者驱动器的根。这些只是“链接”,它们与适当的树结构一起出现。

任何想法,建议?

这是我使用的代码,它可能有点粗糙,我完全是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 个答案:

答案 0 :(得分:2)

对不起,这个答案很晚,但我认为答案无论如何都会有所帮助。

1)Apps脚本无法禁止通知电子邮件,但Google最新版本的Drive SDK确实允许这样做。您只需将“?sendNotificationEmails = false”附加到您发送请求的网址即可。您可以在链接页面上运行测试,以准确查看查询字符串的外观。

2)文件夹实际上保持其结构。只有用户明确获得每个文件夹的所有权的后果。由于权限一次被赋予一个文件夹,因此用户一次访问一个文件夹,以便它们单独显示在用户的Drive文档中。尽管如此,Drive文件/文件夹的共享与其父文件夹元数据分开存储,因此只要用户有权访问该文件夹及其父文件夹,就应保留该文件。

举一个简单的例子,如果你有一个带有两个子文件夹'dir1'和dir2'的'root',新的所有者将拥有所有三个文件夹的所有权,它们将显示在用户的驱动器列表中,但如果它们是点击'root',他们会看到'dir1'和'dir2'仍在那里。