【问题标题】:Javascript, In Google add-on, I try to get all folders and subfolders in specific folderJavascript,在 Google 插件中,我尝试获取特定文件夹中的所有文件夹和子文件夹
【发布时间】:2017-09-09 22:59:52
【问题描述】:

我是舞台上的学生,我在这个问题上遇到了问题。我在电子表格的附加组件中尝试了多种解决方案但没有成功,例如:

var childFolders = DriveApp.getFolderById('ID').getFolders(); 

var childFolders = DriveApp.getFolderById('ID').searchFolders("mimeType = 'application/vnd.google-apps.folder'");   

我只得到其中的文件夹,而不是它们的子文件夹。

当您可以使用此命令拥有驱动器的所有文件夹时,这很奇怪:

var childFolders = DriveApp.getFolders();

我想要文件夹和所有子文件夹。详细信息我将不得不使用触发器,因为我超过了 6 分钟的时间限制,驱动器中有超过 7000 个文件夹。示例:根目录下的一个文件夹可以有500个文件夹的树形文件夹。

我尽量避免使用递归函数。递归组合和触发器是......!我的脚本必须得到一个文件夹,所有的都是子文件夹。 我得到了第一个文件夹的启动键。

如果你有想法,欢迎你。

【问题讨论】:

    标签: javascript google-apps-script google-drive-api directory


    【解决方案1】:

    我只是给你一些开始,只是为了表明我能够从根目录开始获取文件夹中的子文件夹。

    root > mainFolder > subFolder (the tree can go on)
    

    我使用了Class FolderClass FolderIterator 作为参考。

    function getSubFolders() {
    
      var folderArray=[];
      //displays all the folders assigns it to 'rootFolder' variable
      var rootFolder = DriveApp.getFolderById('root').getFolders();
      //iterate over the root folders
      while (rootFolder.hasNext()) {
    
        var mainFolder = rootFolder.next();
        //assign the IDs of the root folders to an array
        folderArray.push(mainFolder.getId());
        Logger.log(mainFolder.getName());
      }
        //go inside one of the folders of your choice using the ID inside the array so we can use it to access the sub-folders
        var newFolder = DriveApp.getFolderById(folderArray[4]).getFolders();
      //loop over the sub-folders inside if it has any
      while (newFolder.hasNext()) {
       //I had sub-folders inside so I accessed it and displayed their names
        var subFolders = newFolder.next();
        Logger.log(subFolders.getName());
      }
    
    }
    

    果然我得到了子文件夹

    [17-04-14 15:28:45:256 HKT] Subfolder3
    [17-04-14 15:28:45:257 HKT] Subfolder2
    [17-04-14 15:28:45:259 HKT] Subfolder1
    

    【讨论】:

    • 谢谢,我会将我的研究分成一张纸用于每个文件夹。我喜欢获得根的方式。这很简单,我正在获取驱动器的 ID。它也有效,但你的方式更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多