LoginSignup
0
1

More than 1 year has passed since last update.

40 代おっさん GASのDriveAppクラスについて学ぶ

Last updated at Posted at 2022-02-24

本記事ついて

本記事は プログラミング初学者の私が学習していく中でわからない単語や概要をなるべくわかりやすい様にまとめたものです。
もし誤りなどありましたらコメントにてお知らせいただけるとありがたいです。

DriveAppクラス

DriveAppクラスとは
Driveサービスの最上位に位置するトップレベルオブジェクトです。

*DriveAppクラスの主なメンバーの図は、参考資料の本を見ていただくか、ネットで調べください。

ちなみにこちらで書いてありました。
https://excel-ubara.com/apps_script1/GAS036.html

フォルダIDとファイルID

ファルダ、ファイルが作成、アップロードされた時点でIDが付与される。

https://drive.google.com/drive/u/1/folders/{ID}

IDの部分がファルダIDになる。

https://drive.google.com/file/d/{ID]/view

IDの部分がファイルIDになる。

IDを使ってファルダ、ファイルを取得するには

getFolderByIdメソッド(ファルダ)

構文

DriveApp.getFolderById(ファルダID)

getFileByIdメソッド(ファイル)

構文

DriveApp.getFileById(ファイルID)

お試し

function tosiki() {
  const folderId = '×××××××××××××';
  const folder   = DriveApp.getFolderById(folderId);

  console.log(folder.getName());

  const fileId = '×××××××××××××××';
  const file   = DriveApp.getFileById(fileId);

  console.log(file.getName());
}

ルートフォルダの操作

ルートフォルダとは
マイドライブのこと

ルートフォルダを収得するメソッド
getRootFolderメソッド

構文

DriveApp.getRootFolder()

お試し

function tosiki() {
  const root = DriveApp.getRootFolder();

  console.log(root.getName());
}

本来、ファルダにファイル、フォルダを作成、削除する操作に関しては、Folderオブジェクトを収得してからそれに対して実行する。
ルートフォルダに関してはDriveAppクラスのメソッドで、ファルダの取得せず操作可能

フォルダを作成
createFolderメソッド

構文

DriveApp.createFolder(ファルダ名)

ファイルを作成
createFileメソッド

構文

DriveApp.createFile(ファイル名, 内容, [, MIMEタイプ])

内容は文字列を指定
MIMEタイプはファイル形式を指定

お試し

function tosiki() {
  const folderName = '作成したファルダ';
  DriveApp.createFolder(folderName);

  const fileName = '作成したファイル.txt';
  const content = '利樹かわいい~~~';
  DriveApp.createFile(fileName, content);
}

参考資料

https://www.amazon.co.jp/%E8%A9%B3%E8%A7%A3-Google-Apps-Script%E5%AE%8C%E5%85%A8%E5%85%A5%E9%96%80-%E7%AC%AC3%E7%89%88/dp/4798064742

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1