概要
Google Apps Script(GAS)で、Googleドライブの指定したフォルダ内にある、すべてのファイルを取得する方法です。
使用するメソッド
構文
Folderオブジェクト.getFiles()
引数
なし
戻り値
FileIterator
サンプルプログラム
function sample() {
// フォルダID
const FOLDER_ID = PropertiesService.getScriptProperties().getProperty("FOLDER_ID");
// フォルダ
const targetFolder = DriveApp.getFolderById(FOLDER_ID);
// フォルダ内のファイル一覧を取得
const files = targetFolder.getFiles();
// ファイルの件数分繰り返す
while(files.hasNext()) {
// ファイル
let file = files.next();
// ログに出力(ファイル名)
Logger.log(file.getName());
}
}
参考
![Class Folder | Apps Script | Google Developers](https://www.gstatic.com/devrel-devsite/prod/ve53a52d1b45000b9df3bcdfe3060bb6d50689f5a6c2b8f03cdaa24fff649fba3/developers/images/opengraph/white.png)
Class Folder | Apps Script | Google Developers
Google ドライブ内のフォルダ。フォルダへのアクセスや作成には DriveApp を使用します。