What query can extract a Gmail thread’s final email details including date range, subject, body, sender, and receiver? Below is a sample code:
function initializeMenu() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Mail Tools').addItem('Retrieve Latest', 'retrieveLatestEmail').addToUi();
}
function retrieveLatestEmail() {
const threads = GmailApp.getInboxThreads();
for (let thread of threads) {
const allMessages = thread.getMessages();
const latestMsg = allMessages[allMessages.length - 1];
const msgInfo = {
date: latestMsg.getDate(),
subject: latestMsg.getSubject(),
body: latestMsg.getPlainBody(),
from: latestMsg.getFrom(),
to: latestMsg.getTo()
};
Logger.log(msgInfo);
}
}