GAS googleドライブの指定したidのフォルダを取得する

GAS googleドライブの指定したidのフォルダを取得する

GAS(Google Apps Script)で、googleドライブの指定したidのフォルダを取得する手順を記述してます。ここでは実際に「id」でフォルダを取得してフォルダ名を確認します。

環境

  • OS windows11 home
  • ブラウザ chrome 108.0.5359.125

指定したidのフォルダを取得

指定したidのフォルダを取得するには、「DriveApp.getFolderById()」を使用します。

DriveApp.getFolderById(id)

フォルダidは、フォルダを開いたリンクから確認可能です。

実際に、id名を指定して、そこからフォルダ名を取得してみます。

function myFunction() {

  // idから取得
  const folder = DriveApp.getFolderById('1wFZgGSjKoE5wX4Wsxxxxxxxxxxx');

  // フォルダ名を取得
  console.log(folder.getName());

}

フォルダ名が取得されていることが確認できます。

存在しないidを指定

存在しないidを指定すると、エラーが発生します。

function myFunction() {

  // idから取得
  const folder = DriveApp.getFolderById('xxxxxxxxxxxxxxxxxxxxx');

  // フォルダ名を取得
  console.log(folder.getName());

}