2
2

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.

40 代おっさん GASのGmailサービスのスレッドについて学ぶ

Posted at

本記事ついて

本記事は プログラミング初学者の私が学習していく中でわからない単語や概要をなるべくわかりやすい様にまとめたものです。
もし誤りなどありましたらコメントにてお知らせいただけるとありがたいです。

システムラベルでスレッドを収得する

スレッドを収得する方法

・システムラベルからスレッドを取得する
・IDでスレッドを取得する
・検索をしてスレッドを取得する

システムラベルでスレッドを取得する

システムラベルは、受信トレイにある、重要、迷惑メールなどのラベルです。
GASではシステムラベルについてスレッドを収得するメソッドがある。

システムラベル 収得メソッド
受信トレイ getInboxThreads
スター付き getStarredThreads
下書き getDraftMessages
迷惑メール getSpamThreads
ゴミ箱 getTrashThreads

下書きは送信前につき、スレッドと言う概念がありません。したがって。収得できるのはメッセージの配列となります。

受信トレイからスレッドを配列として取得するgetInboxThreadsメソッドを見て見ます。

構文

GmailApp.getInboxThreads([開始位置, 最大取得数])

受信トレイは最新のものからインデックスが付与される。
引数の省略も可能だが、その場合はすべてのスレッドを収得する。

お試し

function tosiki() {
  const threads = GmailApp.getInboxThreads(0,3);

  for (const thread of threads) {
    console.log(thread.getFirstMessageSubject());
  }   
}

*一日当たり2万件ないしは5万件までと言う制限があるので注意

IDでスレッドを収得する

スレッドには一意に決まるIDが付与されています。スレッドIDがわかればスレッドを取得できる。
getThreadByIdメソッドで収得できる

構文

GmailApp.getThreadById(ID)

お試し

function tosiki() {
  const threads = GmailApp.getInboxThreads(0,1);
  const id = threads[0].getId();

  const thread = GmailApp.getThreadById(id);
  console.log(thread.getFirstMessageSubject());
}

スレッドを検索する

searchメソッドを使うことで検索機能を使うことが出来る。

構文

GmailApp.search(クエリ[, 開始位置, 最大収得数])

クエリはGmailの検索条件を指定する文字列で、検索対象となるキーワードのみで検索することも可能
検索演算子を使用できる。

*検索演算子の図は、参考資料の本を見ていただくか、ネットで調べください。

ちなみにこちらで書いてありました。
https://support.google.com/mail/answer/7190?hl=ja

お試し

function tosiki() {
  const query = 'is:read "お問い合わせ"';
  const threads = GmailApp.search(query, 0, 10);

  for (const thread of threads) {
    console.log(thread.getFirstMessageSubject());
  }
}

参考資料

https://www.amazon.co.jp/s?k=google+apps+script+%E5%AE%8C%E5%85%A8%E5%85%A5%E9%96%80&adgrpid=110264232688&gclid=CjwKCAiA9aKQBhBREiwAyGP5lSl7AJJLCvOEHb4wQgMlyqW1fll5X8GDTT_Rkd1_soUAyIPMXQr26hoClHEQAvD_BwE&hvadid=553833563682&hvdev=c&hvlocphy=1009076&hvnetw=g&hvqmt=b&hvrand=4378489642044417389&hvtargid=kwd-594191211348&hydadcr=4106_13159878&jp-ad-ap=0&tag=googhydr-22&ref=pd_sl_2x1owglv0s_b_p52

2
2
1

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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?