如何在具有设置名称的文件夹中列出谷歌驱动器中的所有文件?

时间:2016-03-29 03:35:09

标签: google-apps-script google-sheets google-drive-api

我的驱动器中有一个文件夹,其中包含多个“客户端”文件夹。每个客户端文件夹都具有相同的结构,其中包含一个包含已完成发票的文件夹(称为“已完成的发票”)。

folder structure

我需要一种方法来遍历所有名为“已完成的发票”的文件夹,并在Google工作表中列出这些文件夹中的电子表格ID,以便稍后我可以循环浏览这些文件并从中提取数据。

我发现多个代码源可以工作但只在第一级 - 即它会列出代理/客户端文件夹中的文件,但它不会进入子文件夹。例如。见下面的代码

function listFilesInFolder() {

   var sheet = SpreadsheetApp.getActiveSheet();
   sheet.appendRow(["Name", "Date", "Size", "URL", "Download", "Description", "Type"]);

    var folder = DriveApp.getFoldersByName("FOLDER ID GOES HERE");
    //Logger.log(folder);
    var contents = folder.getFiles();

    var cnt = 0;
    var file;

    while (contents.hasNext()) {
        var file = contents.next();
        cnt++;

        Logger.log(file);
        Logger.log(cnt);

            data = [
                file.getName(),
                file.getDateCreated(),
                file.getSize(),
                file.getUrl(),
,
            ];

            sheet.appendRow(data);



    };
};

3 个答案:

答案 0 :(得分:5)

感谢大家的回答,但经过一番搜索,我找到了答案。对于那些正在寻找类似解决方案的人 - 我找到了一个优秀的脚本here,它提供了一个非常强大的输出,我想这可以用于各种不同的场景:

/** 
 * Google Apps Script - List all files & folders in a Google Drive folder, & write into a speadsheet.
 *    - Main function 1: List all folders
 *    - Main function 2: List all files & folders
 * 
 * Hint: Set your folder ID first! You may copy the folder ID from the browser's address field. 
 *       The folder ID is everything after the 'folders/' portion of the URL.
 * 
 * @version 1.0
 * @see     https://github.com/mesgarpour
 */

// TODO: Set folder ID
var folderId = 'My folder ID';

// Main function 1: List all folders, & write into the current sheet.
function listFolers(){
  getFolderTree(folderId, false);
};

// Main function 2: List all files & folders, & write into the current sheet.
function listAll(){
  getFolderTree(folderId, true); 
};

// =================
// Get Folder Tree
function getFolderTree(folderId, listAll) {
  try {
    // Get folder by id
    var parentFolder = DriveApp.getFolderById(folderId);

    // Initialise the sheet
    var file, data, sheet = SpreadsheetApp.getActiveSheet();
    sheet.clear();
    sheet.appendRow(["Full Path", "Name", "Date", "URL", "Last Updated", "Description", "Size"]);

    // Get files and folders
    getChildFolders(parentFolder.getName(), parentFolder, data, sheet, listAll);

  } catch (e) {
    Logger.log(e.toString());
  }
};

// Get the list of files and folders and their metadata in recursive mode
function getChildFolders(parentName, parent, data, sheet, listAll) {
  var childFolders = parent.getFolders();

  // List folders inside the folder
  while (childFolders.hasNext()) {
    var childFolder = childFolders.next();
    // Logger.log("Folder Name: " + childFolder.getName());
    data = [ 
      parentName + "/" + childFolder.getName(),
      childFolder.getName(),
      childFolder.getDateCreated(),
      childFolder.getUrl(),
      childFolder.getLastUpdated(),
      childFolder.getDescription(),
      childFolder.getSize()
    ];
    // Write
    sheet.appendRow(data);

    // List files inside the folder
    var files = childFolder.getFiles();
    while (listAll & files.hasNext()) {
      var childFile = files.next();
      // Logger.log("File Name: " + childFile.getName());
      data = [ 
        parentName + "/" + childFolder.getName() + "/" + childFile.getName(),
        childFile.getName(),
        childFile.getDateCreated(),
        childFile.getUrl(),
        childFile.getLastUpdated(),
        childFile.getDescription(),
        childFile.getSize()
      ];
      // Write
      sheet.appendRow(data);
    }

    // Recursive call of the subfolder
    getChildFolders(parentName + "/" + childFolder.getName(), childFolder, data, sheet, listAll);  
  }
};

答案 1 :(得分:0)

创建一个名为'已完成发票的新文件夹'并将发票添加到该文件夹​​。然后,您只需要在一个文件夹中搜索。这是可能的,因为Drive中的文件夹只是标签,文件可以有多个父文件夹。

答案 2 :(得分:0)

上面的代码可能会被删除(对我来说没有用)。您可以尝试此操作,然后在执行日志中查看输出(通过热键CTRL + ENTER访问)

function generateFolderTree() {

  try {

    // If you want a tree of any sub folder
    //var parent = DriveApp.getFoldersByName("FOLDER_NAME").next();

    // If you want to search from the top (root) folder
    var parentFolder = DriveApp.getRootFolder();

    getChildFolders(parentFolder);

  } catch (e) {

    Logger.log(e.toString());

  }

}


function getChildFolders(parent) {

  var childFolders = parent.getFolders();

  while (childFolders.hasNext()) {

    var childFolder = childFolders.next();

    Logger.log("Folder Name: " + childFolder.getName());
    Logger.log("Folder URL:  " + childFolder.getUrl());

    var files = childFolder.getFiles();

    while (files.hasNext()) {
      var file = files.next();
      // Print list of files inside the folder
      Logger.log(file.getName() + ', ' + file.getUrl());
    }
    // Recursive call for any sub-folders
    getChildFolders(childFolder);

  }

}