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

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

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

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

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

โค้ดตัวอย่าง

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

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

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

ขั้นสูง/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);
  }
}

แสดงรายการประวัติการเข้าชมล่าสุด

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

ขั้นสูง/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);
  }
}