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

บริการ Gmail ขั้นสูงช่วยให้คุณใช้ Gmail API ใน Google 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) {
        for (const thread of threadList.threads) {
          console.log(`Snippet: ${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) {
        for (const record of history) {
          for (const message of record.messages) {
            if (changed.indexOf(message.id) === -1) {
              changed.push(message.id);
            }
          }
        }
      }
      pageToken = recordList.nextPageToken;
    } while (pageToken);

    for (const id of changed) {
      console.log("Message Changed: %s", id);
    }
  } catch (err) {
    console.log(err);
  }
}

แสดงข้อความ

ตัวอย่างต่อไปนี้แสดงวิธีแสดงรายการข้อความที่ยังไม่ได้อ่านของผู้ใช้ Gmail

advanced/gmail.gs
/**
 * Lists unread messages in the user's inbox using the advanced Gmail service.
 */
function listMessages() {
  // The special value 'me' indicates the authenticated user.
  const userId = "me";

  // Define optional parameters for the request.
  const options = {
    maxResults: 10, // Limit the number of messages returned.
    q: "is:unread", // Search for unread messages.
  };

  try {
    // Call the Gmail.Users.Messages.list method.
    const response = Gmail.Users.Messages.list(userId, options);
    const messages = response.messages;
    console.log("Unread Messages:");

    for (const message of messages) {
      console.log(`- Message ID: ${message.id}`);
    }
  } catch (err) {
    // Log any errors to the Apps Script execution log.
    console.log(`Failed with error: ${err.message}`);
  }
}