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