Apps Scriptリファレンス: Apps Script Reference |障害・課題追跡: IssueTracker |Google Workspace: Status Dashboard - Summary

2025年6月8日日曜日

Googleドライブの複数ファイルを指定フォルダに移動したい


複数ファイルを一気に移動したくて作ったアプリです



移動するファイルを1行ずつ「File URLs」テキストエリアに入れて

移動先のフォルダのURLを「Folder URL」に入れて

Submitをクリックする




Code.gs
function doGet() {
return HtmlService.createHtmlOutputFromFile('index');
}

function moveFiles(fileIds, folderId) {
const destinationFolder = DriveApp.getFolderById(folderId);
fileIds.forEach(fileId => {
const file = DriveApp.getFileById(fileId);
file.moveTo(destinationFolder);
});
return "All files moved successfully!";
}


index.html
<!DOCTYPE html>
<html>
<body>
<a target="_blank" href="https://script.google.com/home/projects/SCRIPT_ID/edit">SCRIPT</a>
<div>
<label for="fileUrls">File URLs (one per line):</label>
<br>
<textarea id="fileUrls" rows="5" cols="50"></textarea>
</div>
<div>
<label for="folderUrl">Folder URL:</label>
<br>
<input type="text" id="folderUrl">
</div>
<button id="submit">Submit</button>

<script>
document.getElementById("submit").onclick = function() {
const fileUrls = document.getElementById("fileUrls").value;
const folderUrl = document.getElementById("folderUrl").value;

// 改行で区切ってfileIdsを取得
const fileIds = fileUrls.split('\n').map(url => url.trim().split("/d/")[1].split("/")[0]);
const folderId = folderUrl.split("/folders/")[1];

google.script.run
.withFailureHandler(onFailure)
.withSuccessHandler(onSuccess)
.moveFiles(fileIds, folderId);
};

function onSuccess(result) {
alert(result);
}

function onFailure(e) {
alert([e.message, e.stack]);
}
</script>
</body>
</html>




Tips


アプリ内からスクリプトエディタを開けるようにAppsScriptプロジェクトのリンクを配置しています

<a target="_blank" href="https://script.google.com/home/projects/SCRIPT_ID/edit">SCRIPT</a>



関連記事





Latest post

スプレッドシートの空白セルを直前の値で埋めたい

A列の空白セルに直前の値を入れたくて書いたコードです スプレッドシートに以下のようなBeforeの表があるとき (A列に空白セルがある) Before 1 A B 2 エリア 都市 3 東京 新宿 4 渋谷 5 池袋 6 神奈川 横浜 7 川崎 8 相模原 9 千葉 千葉 10 ...