简体   繁体   English

在 FolderIterator 和 FileIterator 上循环 - Google Apps 脚本

[英]Looping on FolderIterator and FileIterator - Google Apps Script

I'm making a script to loop in a Google Drive folder and noticed I don't know what's the best way to loop over iterators (specifically FolderIterator and FileIterator).我正在制作一个脚本来循环在 Google Drive 文件夹中,并注意到我不知道循环迭代器(特别是 FolderIterator 和 FileIterator)的最佳方法是什么。 My first approach我的第一种方法

const drive_id  = '__________';
let base_folder = DriveApp.getFolderById(drive_id),
    sub_folders = base_folder.getFolders();
Logger.log(base_folder.getFolders());
for(let sub_folder of sub_folders) 
{
    Logger.log(sub_folder);
}

outputs Error TypeError: sub_folders is not iterable .输出Error TypeError: sub_folders is not iterable

Secondly I tried with while loops and checks on hasNext(), but I didn't find them beautifyl enough.其次,我尝试使用 while 循环并检查 hasNext(),但我发现它们不够美。 I'm currently set with this for loop, that works and is clean enough but still looks a bit hacky.我目前设置了这个 for 循环,它可以工作并且足够干净,但仍然看起来有点 hacky。

/*...*/
for(let sub_folders = base_folder.getFolders(); sub_folders.hasNext();)
{
  let sub_folder = sub_folders.next();
  Logger.log(sub_folder);
}

Also notice how I couldn't declare sub_folder within the loop itself.另请注意,我无法在循环本身中声明sub_folder So, what's the proper way of doing it?那么,正确的做法是什么?

Also, what if I wanted to use a map or filter on the sub_folders instead?另外,如果我想使用mapfilter sub_folders怎么办?

Note: this question is probably of broader scope than google-script, but I couldn't find the right terminology to ask the question differently so I preferred to stick to the particular case I found.注意:这个问题可能比谷歌脚本更广泛的 scope,但我找不到正确的术语来提出不同的问题,所以我更愿意坚持我发现的特定情况。

What's the proper way of looping a FolderIterator or FileIterator?循环 FolderIterator 或 FileIterator 的正确方法是什么?

  • The proper way of doing it by using a while loop.使用while循环的正确方法。

  • The reason for that has to do with the hasNext() function which returns a Boolean ( true or false ) and the only loop that natively works with booleans is the while loop.其原因与hasNext() function 有关,它返回Booleantruefalse ),并且唯一可以与布尔值一起使用的循环是while循环。

Also, what if I wanted to use a map or filter on the sub_folders instead?另外,如果我想使用 map 或过滤 sub_folders 怎么办?

  • In your code sub_folder is an object of type Folder .在您的代码sub_folder是一个 object 类型的Folder Unfortunately, you can only retrieve the folders one by one within the while loop by using next() so you can't directly apply map or filter on the FolderIterator .不幸的是,您只能在while循环中使用next()逐个检索文件夹,因此您不能直接应用mapfilter FolderIterator

  • Instead, you can create an array of folders by pushing each folder to an array (within the while loop) and then use map .相反,您可以通过将每个文件夹推送到一个数组(在while循环内)来创建一个文件夹数组,然后使用map

Example:例子:

This script finds all the folders and store them in an array and then uses map to get the name of each sub folder:此脚本查找所有文件夹并将它们存储在一个数组中,然后使用map 获取每个子文件夹的名称

function myFunction() {
  const base_folder  = DriveApp.getFolderById('folder_id');
  const sub_folders  = base_folder.getFolders();
  const folders = [];

  while (sub_folders.hasNext()){
    let folder = sub_folders.next()
    folders.push(folder);
  }
  
  const folder_names = folders.map(f=>f.getName());
  console.log(folder_names);
} 

Here is a list of all the available methods you can apply to each element of the folders array: Class Folder以下是您可以应用于folders数组的每个元素的所有可用方法的列表: Class 文件夹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM