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

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

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

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

หากต้องการรายงานปัญหาและรับการสนับสนุนอื่นๆ โปรดดู คู่มือการสนับสนุนของ 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);
  }
}

แสดงรายการข้อความ

ตัวอย่างต่อไปนี้แสดงวิธีแสดงข้อความที่ยังไม่อ่านของผู้ใช้ 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}`);
  }
}