GAS googleドライブの指定したフォルダのファイルを一覧で取得する
- 作成日 2022.10.13
- Google Apps Script
- Google Apps Script
GAS(Google Apps Script)で、googleドライブの指定したフォルダのファイルを一覧で取得する手順を記述してます。
環境
- OS windows11 home
- ブラウザ chrome 104.0.5112.101
指定したフォルダのファイルを一覧で取得する
指定したフォルダのファイルを一覧で取得するには、「DriveApp.getFiles()」を使用します。
DriveApp.getFiles()
// 戻り値は「FileIterator」です以下のフォルダ「hoge」から取得します。

実際に、取得してみます。
function myFunction() {
// idから取得
const folder = DriveApp.getFolderById('xxxxxxxxxxxxxxxxxx');
// ファイルを取得
const files = folder.getFiles();
// 表示処理
while (files.hasNext()) {
let file = files.next();
let fileName = file.getName();
console.log(fileName);
}
}ファイル名が取得されていることが確認できます。

MIMEタイプを指定
MIMEタイプを指定して取得することも可能です。
function myFunction() {
// idから取得
const folder = DriveApp.getFolderById('1wFZgGSjKoE5wX4Ws0b8xkUBjHov0A9IH');
// ファイルを取得
const files = folder.getFilesByType('application/vnd.google-apps.spreadsheet');
// 表示処理
while (files.hasNext()) {
let file = files.next();
let fileName = file.getName();
console.log(fileName);
}
}実行結果

指定できる「MIMEタイプ」は以下となります。
| 種類 | MIMEタイプ |
|---|---|
| Google Docs | application/vnd.google-apps.document |
| Google Forms | application/vnd.google-apps.form |
| Google My Maps | application/vnd.google-apps.map |
| Google Slides | application/vnd.google-apps.presentation |
| Google Apps Script | application/vnd.google-apps.script |
| Google Sites | application/vnd.google-apps.site |
| Google Sheets | application/vnd.google-apps.spreadsheet |
| xls | application/vnd.ms-excel |
| xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
| xml | text/xml |
| ods | application/vnd.oasis.opendocument.spreadsheet |
| csv | text/csv |
| tmpl | text/plain |
| application/pdf | |
| php | application/x-httpd-php |
| jpg | image/jpeg |
| png | image/png |
| gif | image/gif |
| bmp | image/bmp |
| txt | text/plain |
| doc | application/msword |
| js | text/js |
| swf | application/x-shockwave-flash |
| mp3 | audio/mpeg |
| zip | application/zip |
| rar | application/rar |
| tar | application/tar |
| arj | application/arj |
| cab | application/cab |
| html | text/html |
| htm | text/htm |
| default | application/octet-stream |
-
前の記事
Vagrant インストールしているプラグインを確認する 2022.10.13
-
次の記事
firefox 履歴とブックマークの管理画面を開くショートカットキー 2022.10.13
コメントを書く