使用Google Apps脚本在所有文件夹(和子文件夹)中查找文档/幻灯片/表格

时间:2018-08-31 15:24:29

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

从指向文件夹的链接开始,我试图获取该文件夹中所有文档,幻灯片/ Sheets,子文件夹(以及子文件夹的子文件夹)的信息。

通过一些在线搜索,我终于得到了一些可以找到所有文件的代码,但是我需要对其进行修改,以便仅找到文档,幻灯片和表格。最好的方法是什么?

这是我当前正在使用的代码:

function generateFolderTree(my_id) {
  var parentFolder = DriveApp.getFolderById(my_id);
  var f = parentFolder.getFiles();
  Logger.log(f.next().getName())

  getChildFolders(parentFolder);
}

function getChildFolders(parent) {
  var childFolders = parent.getFolders();
  while (childFolders.hasNext()) {
    var childFolder = childFolders.next();
    var files = childFolder.getFiles();
    while (files.hasNext()) { 
      Logger.log(files.next().getName()); // Print list of files inside the folder
    }
    getChildFolders(childFolder); // Recursive call for any sub-folders
  }
}

3 个答案:

答案 0 :(得分:2)

对此我有点怀疑,可以肯定,可以在代码中实现它;)

https://developers.google.com/apps-script/reference/base/mime-type

// Use MimeType enum to log the name of every Google Doc in the user's Drive.
var docs = DriveApp.getFilesByType(MimeType.GOOGLE_DOCS);
while (docs.hasNext()) {
var doc = docs.next();
Logger.log(doc.getName())
}

// Use plain string to log the size of every PNG in the user's Drive.
var pngs = DriveApp.getFilesByType('image/png');
while (pngs.hasNext()) {
var png = pngs.next();
Logger.log(png.getSize());
}

答案 1 :(得分:1)

这是我最后使用的代码,它似乎可以完成我想要/需要的一切:

function getFolders() {
  var my_link = Browser.inputBox('Enter Folder Link', 'Paste a folder link below...', Browser.Buttons.OK);
  var my_id = getIdFromUrl(my_link);
  fcount = 2;
  SpreadsheetApp.getActiveSpreadsheet().toast("Im looking for SLIDES.", "WORKING...",600);
  generateFolderTree(my_id,MimeType.GOOGLE_SLIDES);
  SpreadsheetApp.getActiveSpreadsheet().toast("Im looking for DOCS.", "WORKING...",600);
  generateFolderTree(my_id,MimeType.GOOGLE_DOCS);
  SpreadsheetApp.getActiveSpreadsheet().toast("Im looking for SHEETS.", "WORKING...",600);
  generateFolderTree(my_id,MimeType.GOOGLE_SHEETS);
  SpreadsheetApp.getActiveSpreadsheet().toast("Your search is complete!", "FINISHED!");
};

// -------------------------------------------------------------------------------

function generateFolderTree(my_id,type) {
  var parentFolder = DriveApp.getFolderById(my_id);
  var f = parentFolder.getFilesByType(type);
  while (f.hasNext()) {
    file = f.next();
    display(file);
  }
  getChildFolders(parentFolder,type);
}

function getChildFolders(parent,type) {
  var childFolders = parent.getFolders();
  while (childFolders.hasNext()) {
    var childFolder = childFolders.next();
    var files = childFolder.getFilesByType(type);
    while (files.hasNext()) {
      mfs = files.next();
      display(mfs);
    }
    getChildFolders(childFolder,type); // Recursive call for any sub-folders
  }
}

答案 2 :(得分:-1)



    function myFunction() 
    {
        var folders = DriveApp.getFoldersByName("Projects");
        var folder = folders.next();
        var sheets = getFilesInFolderByType(folder, MimeType.GOOGLE_SHEETS);
        var docs = getFilesInFolderByType(folder, MimeType.GOOGLE_DOCS);
        for each (var sheet in sheets)
        {
            console.log(sheet);
        }
        for each (var doc in docs)
        {
            console.log(doc);
        }
    }

    function getFilesInFolderByType(folder, type)
    {
        var files = []
        var filesInFolder = folder.getFilesByType(type);
        while (filesInFolder.hasNext())
        {
            files.push(filesInFolder.next());
        }
        // Use method to recurse through sub folders.
        var subFolders = folder.getFolders();
        while (subFolders.hasNext())
        { 
            var fileArray = getFilesInFolderByType(subFolders.next(), type);
            files.concat(fileArray);
        } 
        return files;
    }