고급 Gmail 서비스

고급 Gmail 서비스를 사용하면 Gmail API를 Apps Script Apps Script의 기본 제공 Gmail 서비스처럼 이 API를 사용하면 스크립트가 대화목록, 메일, 라벨을 Gmail 편지함. 대부분의 경우, 내장된 서비스가 사용하기 더 쉽지만 고급 서비스는 몇 가지 추가 기능을 제공하며 Gmail 콘텐츠에 대한 정보입니다.

참조

이 서비스에 대한 자세한 내용은 Gmail API 참조 문서를 확인하세요. Apps Script의 모든 고급 서비스와 마찬가지로 고급 Gmail 서비스는 공개 API와 동일한 객체, 메서드, 매개변수가 포함됩니다. 자세한 내용은 메서드 서명 확인 방법을 참조하세요.

문제를 신고하고 기타 지원을 받으려면 다음을 참조하세요. Gmail 지원 가이드

샘플 코드

아래 샘플 코드는 API의 버전 1을 사용합니다.

라벨 정보 나열

다음 예에서는 사용자의 모든 라벨 정보를 나열하는 방법을 보여줍니다. 여기에는 라벨 이름, 유형, ID, 공개 상태 설정이 포함됩니다.

고급/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);
  }
}

받은편지함 스니펫 나열

다음 예는 사용자의 받은편지함에 있는 각 대화목록의 메일입니다. 페이지 토큰을 사용하여 결과의 전체 목록입니다.

고급/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를 메시지가 표시됩니다. 변경된 각 메시지는 기록 레코드에 있는 변경 이벤트의 수와 상관없이 한 번 이 페이지 토큰을 사용하여 전체 결과 목록에 액세스합니다.

고급/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);
  }
}