LoginSignup
0
0

More than 1 year has passed since last update.

Google Apps ScriptでGmailを自動掃除

Last updated at Posted at 2021-07-17

Google Apps Script でGmailに溜まったメールを掃除する方法

■用途

  • メルマガに登録している場合、指定期間だけ残してからゴミ箱に移動

■設定の流れ

  1. URLにアクセスを行い、「新しいプロジェクト」を押下
    キャプチャ.JPG
  2. ファイルを追加(Gmail.gs)&リネーム(main.gs)を行います
    キャプチャ.JPG
  3. 掃除を行うためのScriptを記述して保存します
  4. main.gsを選択し、試しに記述した処理を実行します
  5. 問題がなければ、トリガー画面に遷移します
    キャプチャ.JPG
  6. トリガー設定を保存します
    キャプチャ.JPG

■「3」のコードについて

ゴミ箱に入れるだけなら「Gmail.gs」は特に触る必要は割りません。
「main.gs」を自身の環境に合わせて修正する必要があります。

// Gmail.gs
/**
 * Gmail関連
 */
class Gmail {
  /**
   * 指定ラベルのデータが指定日過ぎた場合にゴミ箱移動
   *
   * @param {string} label 対象のラベル名
   * @param {int}    day   経過日数
   */
  cleanupByLabelAndDay(label, day) {
    const threads = GmailApp.search('older_than:' + day + 'd -is:starred label:' + label);
    for (let i = 0; i < threads.length; i++) {
      threads[i].moveToTrash();
    }
  }
}
// main.gs
/**
 * Gmailのクリーンアップ
 */
function cleanupGmail() {
  const obj = new Gmail();
  // 「ラベル名」が365日経過したらゴミ箱に移動
  obj.cleanupByLabelAndDay('ラベル名', 365);
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0