2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【GAS】Gmailの未読のメール既読にしたいな

Posted at

はじめに

こんにちは。

Gmailで、件名見て、「あぁ、それね。」で未読でほったらかしにすることが多い筆者です。

特にメールマガジンとか、マネーフォワードMEの出金のお知らせとか。。

気づいたら 8000件 も溜まってました。

今更読む気なんて無いので、全て既読にしようと思います(もしかしたらいるメールもあるかもなので、今回は削除しません)。

作戦

  1. GASで未読のメール(スレッド)を取得する。
    2. 最大500件しか取得できないので、取得できなくなるまでループする(8000件あるので)。
  2. GASで1のメールをスレッド単位で既読する。

実装

これで行けました:thumbsup:

code.gs
const markRead = () => {
  let offset = 0
  const limit = 500
  while (true) {
    // 作戦: 1
    var threads = GmailApp.search('is:unread is:inbox', offset, limit)
    offset += limit

    // 作戦: 2
    for (thread in threads) {
      threads[thread].markRead()
    }

    if (threads.length < limit) break
  }
}

おわりに

今回は受信トレイにある未読のメールを対象に実行しました。
GASは360秒の実行時間の上限で一度に8000件のメールを既読にできませんでしたが、数回繰り返して全てのメールを既読できました :smiley:

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?