บริการ Gmail ขั้นสูง

บริการ Gmail ขั้นสูงช่วยให้คุณใช้ Gmail API ใน Apps Script ได้ API นี้ช่วยให้สคริปต์ค้นหาและแก้ไขชุดข้อความ ข้อความ และป้ายกำกับในกล่องจดหมาย Gmail ได้เช่นเดียวกับบริการ Gmail ในตัวของ Apps Script ในกรณีส่วนใหญ่ บริการในตัวจะใช้งานง่ายกว่า แต่บริการขั้นสูงนี้มีฟีเจอร์เพิ่มเติมเล็กน้อยและการเข้าถึงข้อมูลที่ละเอียดขึ้นเกี่ยวกับเนื้อหา Gmail

ข้อมูลอ้างอิง

สำหรับข้อมูลโดยละเอียดเกี่ยวกับบริการนี้ โปรดดูเอกสารอ้างอิงสำหรับ Gmail API บริการขั้นสูงของ Gmail จะใช้ออบเจ็กต์ เมธอด และพารามิเตอร์เดียวกันกับ API สาธารณะ เช่นเดียวกับบริการขั้นสูงทั้งหมดใน Apps Script โปรดดูข้อมูลเพิ่มเติมที่หัวข้อวิธีกำหนดลายเซ็นของเมธอด

หากต้องการรายงานปัญหาและค้นหาการสนับสนุนอื่นๆ โปรดดูคู่มือการสนับสนุน Gmail

รหัสตัวอย่าง

โค้ดตัวอย่างด้านล่างใช้ API เวอร์ชัน 1

แสดงรายการข้อมูลป้ายกำกับ

ตัวอย่างต่อไปนี้แสดงวิธีแสดงข้อมูลป้ายกำกับทั้งหมดของผู้ใช้ ซึ่งรวมถึงชื่อ ประเภท รหัส และการตั้งค่าระดับการเข้าถึงของป้ายกำกับ

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);
  }
}

แสดงรายการประวัติล่าสุด

ตัวอย่างต่อไปนี้แสดงวิธีบันทึกประวัติกิจกรรมล่าสุด กล่าวโดยเจาะจงก็คือ ตัวอย่างนี้จะกู้คืนรหัสบันทึกประวัติที่เชื่อมโยงกับข้อความที่ส่งล่าสุดของผู้ใช้ จากนั้นจะบันทึกรหัสข้อความของทุกข้อความที่เปลี่ยนแปลงนับจากเวลานั้น ข้อความที่เปลี่ยนแปลงแต่ละรายการจะได้รับการบันทึกเพียงครั้งเดียว ไม่ว่าจะมีเหตุการณ์การเปลี่ยนแปลงกี่ครั้งในบันทึกประวัติ โปรดสังเกตการใช้โทเค็นของหน้าเว็บเพื่อเข้าถึงรายการผลการค้นหาทั้งหมด

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);
  }
}