Learn or Die

生涯勉強。Macです。

GASでDrive APIを利用する方法

特定のフォルダにスプレッドシートを作成したいときに使用しました。
APIを使わないとコードが冗長になってしまいます。

Google Drive APIの概要▼ developers.google.com

手順

  1. プロジェクトのリソース>Googleの拡張サービス>Drive APIをONにする
  2. Googleデベロッパーコンソールのライブラリにて、「Google Drive API」 を検索して有効にする

コード

//createSpreadsheet(フォルダID, シート名)
function createSpreadsheet(id, name) {
  var file = Drive.Files.insert({
    'title': name,
    'mimeType': 'application/vnd.google-apps.spreadsheet',
    'parents': [{'id': id}]
  });
  
  return SpreadsheetApp.openById(file.getId());
}