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

40 代おっさん GASのメッセージを取得する

Last updated at Posted at 2022-02-23

本記事ついて

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

スレッドからメッセージを収得する

受信したメールの情報や内容を蓄積するには、メッセージを取得する必要がある

メッセージを取得する方法

・スレッドからメッセージを取得する
・IDでメッセージを取得する
・スレッドの配列からメッセージを取得する

スレッドからメッセージを取得する

GmailThreadクラスの
getMessagesメソッドを使用

構文

GmailThreadオブジェクト.getMessages()

お試し

function tosiki() {
  const threads = GmailApp.getInboxThreads(0, 1);
  const messages = threads[0].getMessages();
  console.log(messages[0].getSubject());
}

IDでメッセージを取得する

メッセージにも一意で決まるIDが付与されている。
メッセージIDがわかれば
getMessageByIdメソッドで直接収得できる。

構文

GmailAPP.getMessageById(id)

お試し

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

  const message = GmailApp.getMessageById(id);
  console.log(message.getSubject());
}

スレッドの配列からメッセージを取得する

スレッドの配列から直接メッセージを取得できる
getMesssagesForThreadsメソッド

構文

GmailAPP.getMesssagesForThreads(スレッドの配列)

お試し

function tosiki() {
  const threads = GmailApp.getInboxThreads(0, 2);
  const messagesForThreads = GmailApp.getMessagesForThreads(threads);

  for (const [i, thread] of messagesForThreads.entries()) {
    for (const [j, message] of thread.entries()) {
      console.log(`[${i}] [${j}]: ${message.getSubject}`);
    }
  }
}

参考資料

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

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