GAS googleドライブで指定したフォルダをゴミ箱に移動する

GAS googleドライブで指定したフォルダをゴミ箱に移動する

GAS(Google Apps Script)で、googleドライブで指定したフォルダをゴミ箱に移動する手順を記述してます。「setTrashed」に「true」を指定することで可能です。

環境

  • OS windows11 home
  • ブラウザ chrome 108.0.5359.125

フォルダをゴミ箱に移動

フォルダをゴミ箱に移動するには、「setTrashed」を使用します。

setTrashed(true);

※trueを指定することでゴミ箱に移動することが可能

実際に、移動させるコードは以下となります。
※フォルダを開いていてもゴミ箱移動されます。

function myFunction() {

  // idで取得
  const folder = DriveApp.getFolderById("1J3RS7MpnMvvY9tmVI4xxxxxx");

  // trueでゴミ箱に移動
  folder.setTrashed(true);

}

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

フォルダ名を指定

フォルダ名を指定して移動させる場合は「getFoldersByName」を使用します。
※googleドライブは、同一のフォルダ名が使用できるのでループ処理になります。

function myFunction() {

  // idで取得
  const folders = DriveApp.getFoldersByName("foo");

  while (folders.hasNext()) {
    folders.next().setTrashed(true);
  }

}