I currently have a Google Apps Script running a spreadsheet that pulls the correct Email I am querying for. However, I can only get the original email body contents, when I am actually trying to pull a thread reply in the actual email conversation.
Does anyone know if this is possible or how to go about doing so?
My current script only pulls the initial email from the search query, but this is not the intention.
function myFunction() {
// Use sheet
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Gmail query
var query = "subject:Attendance Shed";
// Search in Gmail, bind to array
var threads = GmailApp.search(query);
// Loop through query results
for (var i = 0; i < threads.length; i++)
{
// Get messages in thread, add to array
var messages = threads[i].getMessages();
// Used to find max index in array
var max = messages[0];
var maxIndex = 0;
// Loop through array to find maxIndexD = most recent mail
for (var j = 0; j < messages.length; j++) {
if (messages[j] > max) {
maxIndex = j;
max = messages[j];
}
}
// Find data
var mId = messages[maxIndex].getId() // ID used to create mail link
var from = messages[maxIndex].getFrom();
var cc = messages[maxIndex].getCc();
var time = threads[i].getLastMessageDate()
var sub = messages[maxIndex].getSubject();
// Write data to sheet
ss.appendRow([from, cc, time, sub, 'https://mail.google.com/mail/u/0/#inbox/'+mId])
}
}
I expect it to be able to retrieve a specific thread using the search query given and output it to a Spreadsheet. Currently it only pulls the initial email.
Try this code after the var messages = threads[i].getMessages();
line:
…
for (var j = 0; j < messages.length; j++) {
// Find data
var mId = messages[j].getId() // ID used to create mail link
var from = messages[j].getFrom();
var cc = messages[j].getCc();
var time = threads[i].getLastMessageDate()
var sub = messages[j].getSubject();
// Write data to sheet
ss.appendRow([from, cc, time, sub, 'https://mail.google.com/mail/u/0/#inbox/' + mId])
}
ss.appendRow();
This will get each message's information, and then append an empty row to separate between each thread.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.