ファイルそのものを移動するのではなく
ファイルのショートカットを作りたくて試しました
Code.gs
function doGet() {
return HtmlService.createHtmlOutputFromFile('index');
}
/**
* 指定されたフォルダにファイルのショートカットを作成します。
* @param {string[]} fileIds - ショートカットを作成するファイルのIDの配列。
* @param {string} folderId - ショートカットを作成する先のフォルダのID。
* @return {string} 処理結果のメッセージ。
*/
function createShortcuts(fileIds, folderId) {
const destinationFolder = DriveApp.getFolderById(folderId);
fileIds.forEach(fileId => {
// ショートカットを作成
destinationFolder.createShortcut(fileId);
});
return "すべてのファイルのショートカットを作成しました!";
}
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ショートカット作成ツール</title>
</head>
<body>
<h2>Googleドライブのファイルショートカット作成</h2>
<div>
<label for="fileUrls">ファイルURL(1行に1つずつ):</label><br>
<textarea id="fileUrls" rows="8" cols="80" placeholder="https://..."></textarea>
</div>
<div>
<label for="folderUrl">フォルダURL:</label><br>
<input type="text" id="folderUrl" style="width: 500px;" placeholder="https://...">
</div>
<br>
<button id="submit">ショートカット作成</button>
<script>
function extractId(url) {
const match = url.match(/[-\w]{25,}/);
return match ? match[0] : null;
}
document.getElementById("submit").onclick = function () {
const fileUrls = document.getElementById("fileUrls").value.trim();
const folderUrl = document.getElementById("folderUrl").value.trim();
const fileIds = fileUrls
.split('\n')
.map(line => extractId(line))
.filter(id => id !== null);
const folderId = extractId(folderUrl);
if (fileIds.length === 0 || !folderId) {
alert("ファイルURLとフォルダURLを正しく入力してください。");
return;
}
google.script.run
.withSuccessHandler(onSuccess)
.withFailureHandler(onFailure)
.createShortcuts(fileIds, folderId);
};
function onSuccess(message) {
alert(message);
}
function onFailure(error) {
alert("エラーが発生しました: " + error.message);
}
</script>
</body>
</html>
Reference
createShortcut(targetId)
関連記事