Last active
November 16, 2022 12:12
-
-
Save nmaruy27/a2ecbff21d0af03f91f6613f74b7069e to your computer and use it in GitHub Desktop.
フォルダを作成し、その配下にスプレッドシートを作成するGoogle Apps Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createSpreadSheetInSpecificFolder(): SpreadSheet { | |
// 作成するフォルダを格納するフォルダ(ややこしい…)のIDは事前に取得し、CURRENT_FOLDER_IDとして定義しておく | |
// 【フォルダIDの取得方法】 | |
// Googleドライブの対象フォルダをブラウザで開き、URLの最後のランダム文字列を取得する | |
// 例) https://drive.google.com/drive/folders/1G99xxx_J9a2QOWgspkDWPjxDdAxxxjyf | |
// このURLの場合「1G99xxx_J9a2QOWgspkDWPjxDdAxxxjyf」がIDになる | |
const currentFolder = DriveApp.getFolderById(CURRENT_FOLDER_ID); | |
// 作成したいフォルダが既に存在している場合は、ゴミ箱に移動する | |
const folders = currentFolder.getFoldersByName(DATA_FOLDER_NAME); | |
while (folders.hasNext()) { | |
let folder = folders.next(); | |
folder.setTrashed(true); | |
} | |
// フォルダを作成する | |
const dataFolder = currentFolder.createFolder(DATA_FOLDER_NAME); | |
// スプレッドシートを作成する | |
const file = Drive.Files.insert({ | |
title: name, | |
mimeType: MimeType.GOOGLE_SHEETS, | |
parents: [{ id: dataFolder.getId() }], | |
}); | |
return SpreadsheetApp.openById(file.id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment