I've written a script that imports Gmail messages to a spreadsheet. An inbox rule moves it to a "awaiting import" Gmail label, and the script processes the message then removes this label and adds it to the "imported" label.
The problem is that, when an email is received into this "awaiting import" label, the label seems to be applied to the whole thread. As the message has the same subject as previously imported messages, it therefore imports all other messages in the thread (these have already been imported).
Here's my script:
var importedCount = 0;
var label = GmailApp.getUserLabelByName('Monitor/AwaitingImport');
var threads = label.getThreads(0,50); // An array of threads (limit set to 50, this could be increased)
if (threads.length != 0) { // If a thread exists
for (var i = 0; i < threads.length; i++) { // for each thread in the label
var threadsubject = threads[i].getFirstMessageSubject();
if (threadsubject.match(/(Check In)/)) { // if the subject contains "Check In"
var messages = threads[i].getMessages(); // get ALL the messages
for (var j = 0; j < messages.length; j++) { // for each message
var body = messages[j].getPlainBody();
// Get key:value pairs from email into an array
var array = body.trim().split("\n");
var newArray = [];
for (k = 0; k < array.length; k++) {
newArray = newArray.concat(array[k].toString().split("="));
}
// Push other data to array that isn't in body of email
newArray.push("GmailMessageID", messages[j].getId());
newArray.push("MessageDate", messages[j].getDate());
// Create object
var newObj = {};
for (var l =0 ; l< newArray.length ; l++) {
if (l%2 == 0){
newObj[newArray[l]] = newArray[l+1];
}
}
// Now insert object as record into the spreadsheet
insertRecordWithAnyFieldsIntoAnySheet('Incoming Logs', newObj, true);
}
// Finally remove the label from this message, mark it as read, and update the importedCount (for logging)
threads[i].removeLabel(GmailApp.getUserLabelByName('Monitor/AwaitingImport'));
threads[i].addLabel(GmailApp.getUserLabelByName('Monitor/Imported'));
importedCount++;
}
}
}
SpreadsheetApp.getActiveSpreadsheet().toast(importedCount + ' messages imported');
How can I prevent ALL messages in the thread from being imported, and just import the one that was just received?
I don't want to check the incoming message against messages already imported using the Gmail Message ID, as this would involve a resource-intensive search of thousands of messages already imported into the sheet.
I don't want to import only the last message in the thread, because this potentially misses out other messages that haven't been imported
Finally I tried to import only unread messages, by adding another condition to the script (i.e.
if (messages[j].isRead())
), on the basis that my script would mark the message as read (not shown in above snippet) but this still seemed to import all messages in the thread