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

2024年5月18日土曜日

Googleドライブのファイルを指定フォルダに移動したい - Move a file to a specific folder in Google Drive via Apps Script


Googleドライブのファイル移動を簡単にしたくて書いたコードです。
I wrote this code to make it easier to move files in Google Drive.


移動したいファイルURLと移動先のフォルダURLを入力して移動するWebアプリです。
Deploy a web app to move a file using the file URL and the destination folder URL.

実行手順 - Execution procedure:
  1. file URL に移動したいファイルのURLを入力する - Set the "file URL"
  2. folder URLには移動先のフォルダのURLを入力する- Set the "folder URL"
  3. submitボタンをクリックする- Click the "submit" button



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

function moveFile(fileId, folderId) {
const file = DriveApp.getFileById(fileId);
const destinationFolder = DriveApp.getFolderById(folderId);
file.moveTo(destinationFolder);
return "Success!!";
}


index.html
<!DOCTYPE html>
<html>
<body>
file URL:<input type="text" id="fileUrl">
folder URL:<input type="text" id="folderUrl">
<button id="submit">submit</button>

<script>
elem("submit").onclick = submitClicked;

function submitClicked() {
const fileId = elem("fileUrl").value.split("/d/")[1].split("/")[0];
const folderId = elem("folderUrl").value.split("/folders/")[1];
google.script.run
.withFailureHandler(onFailure)
.withSuccessHandler(onSuccess)
.moveFile(fileId, folderId);
}

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

function onFailure(e) {
alert([e.message, e.stack]);
}

function elem(id) {
return document.getElementById(id);
}

</script>
</body>
</html>


Reference

GoogleドライブのファイルやフォルダをmoveTo(destination)で移動したい

Latest post

Google Apps Scriptの障害時はIssueTrackerを見てみる - Incidents for Apps Script are reported on Issue Tracker

IssueTracker > Apps Script issues https://issuetracker.google.com/savedsearches/566234 Google Apps Scriptの障害時は IssueTracker に課題が上がっていることが...