
GASで新規ファイルを作成する いちいちマイドライブを表示することが面倒
スプレッドシートで作業している際に、新しいファイルを作る場合、マイドライブを表示することが面倒です。
ということで、カスタムメニューから新規のドキュメント、スプレッドシート、スライドを作成してしまうコードです。
新規ファイルはマイドライブに保存されます。
コードは下にあります。




function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('新規ファイル作成')
.addItem('新規Googleドキュメント', 'createGoogleDoc')
.addItem('新規Googleスプレッドシート', 'createGoogleSheet')
.addItem('新規Googleスライド', 'createGoogleSlide')
.addToUi();
}
function createGoogleDoc() {
const doc = DocumentApp.create('新規ドキュメント');
const url = doc.getUrl();
const ui = SpreadsheetApp.getUi();
ui.alert('新規Googleドキュメントを作成しました。');
SpreadsheetApp.getActiveSpreadsheet().toast('新規Googleドキュメントを作成しました', '通知');
openInNewWindow(url);
}
function createGoogleSheet() {
const sheet = SpreadsheetApp.create('新規スプレッドシート');
const url = sheet.getUrl();
const ui = SpreadsheetApp.getUi();
ui.alert('新規Googleスプレッドシートを作成しました。');
SpreadsheetApp.getActiveSpreadsheet().toast('新規Googleスプレッドシートを作成しました', '通知');
openInNewWindow(url);
}
function createGoogleSlide() {
const slide = SlidesApp.create('新規スライド');
const url = slide.getUrl();
const ui = SpreadsheetApp.getUi();
ui.alert('新規Googleスライドを作成しました。');
SpreadsheetApp.getActiveSpreadsheet().toast('新規Googleスライドを作成しました', '通知');
openInNewWindow(url);
}
function openInNewWindow(url) {
const htmlOutput = HtmlService.createHtmlOutput(`<script>window.open("${url}");google.script.host.close();</script>`);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'ファイルを開いています');
}