拡張 Gmail サービス

高度な Gmail サービスを使用すると、Apps Script で Gmail API を使用できます。Apps Script の組み込みの Gmail サービスと同様に、この API を使用すると、スクリプトは Gmail メールボックス内のスレッド、メッセージ、ラベルを検索して変更できます。ほとんどの場合、組み込みサービスの方が使いやすいですが、この高度なサービスでは、いくつかの追加機能が提供され、Gmail コンテンツに関する詳細情報にアクセスできます。

リファレンス

このサービスの詳細については、Gmail API のリファレンス ドキュメントをご覧ください。Apps Script のすべての高度なサービスと同様に、高度な Gmail サービスでも公開 API と同じオブジェクト、メソッド、パラメータを使用します。詳細については、メソッド シグネチャの決定方法をご覧ください。

問題を報告したり、その他のサポートを探したりするには、Gmail サポートガイドをご覧ください。

サンプルコード

以下のサンプルコードでは、バージョン 1 の API を使用しています。

ラベル情報のリスト

次の例は、ユーザーのすべてのラベル情報を一覧表示する方法を示しています。これには、ラベル名、タイプ、ID、公開設定が含まれます。

advanced/gmail.gs
/**
 * Lists the user's labels, including name, type,
 * ID and visibility information.
 */
function listLabelInfo() {
  try {
    const response =
      Gmail.Users.Labels.list('me');
    for (let i = 0; i < response.labels.length; i++) {
      const label = response.labels[i];
      console.log(JSON.stringify(label));
    }
  } catch (err) {
    console.log(err);
  }
}

受信トレイのスニペットを一覧表示する

次の例は、ユーザーの受信トレイの各スレッドに関連付けられているテキスト スニペットを一覧表示する方法を示しています。結果の全リストにアクセスするためにページトークンを使用していることに注意してください。

advanced/gmail.gs
/**
 * Lists, for each thread in the user's Inbox, a
 * snippet associated with that thread.
 */
function listInboxSnippets() {
  try {
    let pageToken;
    do {
      const threadList = Gmail.Users.Threads.list('me', {
        q: 'label:inbox',
        pageToken: pageToken
      });
      if (threadList.threads && threadList.threads.length > 0) {
        threadList.threads.forEach(function(thread) {
          console.log('Snippet: %s', thread.snippet);
        });
      }
      pageToken = threadList.nextPageToken;
    } while (pageToken);
  } catch (err) {
    console.log(err);
  }
}

最近の履歴を一覧表示する

次の例は、最近のアクティビティ履歴をログに記録する方法を示しています。具体的には、この例では、ユーザーが最後に送信したメッセージに関連付けられた履歴レコード ID を復元し、その時間以降に変更されたすべてのメールのメッセージ ID をログに記録します。履歴レコードに変更イベントの数に関係なく、変更された各メッセージは 1 回だけ記録されます。結果の全リストにアクセスするためにページトークンを使用していることに注意してください。

advanced/gmail.gs
/**
 * Gets a history record ID associated with the most
 * recently sent message, then logs all the message IDs
 * that have changed since that message was sent.
 */
function logRecentHistory() {
  try {
    // Get the history ID associated with the most recent
    // sent message.
    const sent = Gmail.Users.Threads.list('me', {
      q: 'label:sent',
      maxResults: 1
    });
    if (!sent.threads || !sent.threads[0]) {
      console.log('No sent threads found.');
      return;
    }
    const historyId = sent.threads[0].historyId;

    // Log the ID of each message changed since the most
    // recent message was sent.
    let pageToken;
    const changed = [];
    do {
      const recordList = Gmail.Users.History.list('me', {
        startHistoryId: historyId,
        pageToken: pageToken
      });
      const history = recordList.history;
      if (history && history.length > 0) {
        history.forEach(function(record) {
          record.messages.forEach(function(message) {
            if (changed.indexOf(message.id) === -1) {
              changed.push(message.id);
            }
          });
        });
      }
      pageToken = recordList.nextPageToken;
    } while (pageToken);

    changed.forEach(function(id) {
      console.log('Message Changed: %s', id);
    });
  } catch (err) {
    console.log(err);
  }
}