1

我一直在尝试使用我在其中一个答案上看到的 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"};
    }
4

1 回答 1

2

抱歉,这个答案来晚了,但我认为无论哪种方式,答案都会有所帮助。

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

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

举个简单的例子,如果你的“root”有两个子文件夹“dir1”和“dir2”,那么新所有者将拥有所有三个文件夹的所有权,并且它们都将显示在用户的云端硬盘列表中,但如果他们要点击进入'root' 他们会看到 'dir1' 和 'dir2' 仍然在那里。

于 2013-08-21T17:45:34.133 回答