概要
Google Apps Script(GAS)で、Googleドライブのファイルを指定したフォルダへ移動する方法です。
使用するメソッド
構文
Fileオブジェクト.moveTo(destination)
引数
パラメーター | 必須 | 型 | 説明 |
---|---|---|---|
destination | ○ | Folder | 移動先のフォルダ |
戻り値
File
サンプルプログラム
function sample() {
// 移動するファイルID(プロパティに設定しておく)
const FILE_ID = PropertiesService.getScriptProperties().getProperty("FILE_ID");
// ファイルの移動先のフォルダID(プロパティに設定しておく)
const FOLDER_ID = PropertiesService.getScriptProperties().getProperty("FOLDER_ID");
// -----
// 移動先のフォルダを取得
const destination = DriveApp.getFolderById(FOLDER_ID);
// 移動するファイルを取得
const file = DriveApp.getFileById(FILE_ID);
// スプレッドシートを移動
const moveFile = file.moveTo(destination);
// 移動したフォルダ名をコンソールに出力
console.log(moveFile.getParents().next().getName());
}