路径

时间:2018-06-06 01:38:28

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

我有一个看起来像这样的字符串:

2017\S\Smith, John\ProjectID23869

2017文件夹位于父文件夹中,其ID为。

var parentFolderId = DriveApp.getFolderById("xxxxxx12345");

我需要获取ProjectID23869的folderId,我认为我需要按文件夹向下钻取文件,而不是简单地在根目录中找到名称。我们有数以万计的文件夹,这需要花费太多时间。

我尝试了各种方法,但似乎无法得到它。

再次,非常感谢帮助。你们都很棒。

1 个答案:

答案 0 :(得分:0)

根据他们对云端硬盘的使用方式,以下是两种方式:

方式#1

DriveApp.getFoldersByName("your folder").next().getId();

通过这种方式,您可以在所有文件中找到您的文件夹。

方式#2

DriveApp.getFolderById("id for folder0")
  .getFoldersByName("folder1").next()
  .getFoldersByName("folder2").next()
  //...
  .getId()

此处,代码可能会运行得更慢,具体取决于您的文件数量和Google的服务器数量。我建议您尝试两者并检查性能是否有问题。即使projectID字符串不唯一,方式#2也始终只返回1个结果。但是,您的路径字符串不得包含任何错误。

使用Way#2的当前代码,可能的错误是您将拥有两个具有相同名称的文件夹,而Way#2将不会默认在右侧。

双向代码

function onOpen() { // this is so you can run it from a Spreadsheet menu

  SpreadsheetApp.getUi()
  .createMenu("Deepest file")
  .addItem("easyNameFinder", "easyNameFinder") // Way #1
  .addItem("pathFinder", "pathFinder") // Way #2
  .addToUi();

}

function easyNameFinder() { // Way #1

  var name = SpreadsheetApp.getCurrentCell().getValue();
  var folderList = []
  var folders = DriveApp.getFoldersByName(name);
  while (folders.hasNext()) {// in case name is not unique
    var folder = folders.next();
    folderList.push(folder.getId() + " in " + folder.getParents().next().getName());
  }
  var id = "See below the " + folderList.length + " folder match(es) for " + name + "\n" + folderList.join("\n");
  Logger.log(id); // view logs with Ctrl + Enter
  SpreadsheetApp.getUi().alert(id); // an alert will appear on the active sheet
}

function pathIterator(query, folder) { // The loop for Way #2

  var folders = folder.getFoldersByName(query)
  if (folders.hasNext()) { // unfortunately, there could be more than one child folder with a given name. If that is the case, you will run into problems.
    var folder = folders.next();
    var name = folder.getName();
    Logger.log(name) // view logs with Ctrl + Enter in the Script editor
    if (folder) {
      Logger.log("we just found " + name + "!\n")
      return folder
    }; 
  } else return null;
}

function pathFinder() { // Way #2: main function

  var string = SpreadsheetApp.getCurrentCell().getValue();
  var array = string.split("\\")
  Logger.log(array);
  var mother = 'YOUR-PARENT-FILE-ID-HERE';
  var mainFolder = DriveApp.getFolderById(mother); 
  var folder = mainFolder;
  var error;

  array.forEach(function(value) {
    var recursion = pathIterator(value, folder)
    if (recursion) {folder = recursion}
    else {
      error = "Could not find folder \"" + value + "\" in " + folder
      Logger.log(error)
      SpreadsheetApp.getUi().alert(error);}
  })

  if (error) {
    return; // skipping the code below if some folder went unfound
  }

  var displayString = folder.getId() + "\nis the Id for the folder " + '"' + folder.getName() + '"'
  Logger.log(displayString); // view logs with Ctrl + Enter
  SpreadsheetApp.getUi().alert(displayString); // an alert will appear on the active sheet
}

通过我自己的100个文件夹的表现:

  • Way#1(easyNameFinder):0.495秒
  • 方式#2(路径查找器):1.091秒