Problem: Every time I run the script, the same previous emails are loaded into the spreadsheet. Given that I need to run this script weekly, I would like to ignore emails already imported from prior runs.
I believe an edit needs to be made in the for loop or perhaps add an if statement before the append
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var label = GmailApp.getUserLabelByName("Test Script");
var threads = label.getThreads();
for (var i=0; i<threads.length; i++)
{
var messages = threads[i].getMessages();
for (var j=0; j<messages.length; j++)
{
var msg = messages[j].getPlainBody();
var sub = messages[j].getSubject();
var dat = messages[j].getDate();
ss.appendRow([msg, sub, dat])
}
}
Output is currently: Email 1 Email 2 Email 3 Email 1 Email 2 Email 3 Email 4 Email 1 Email 2 Email 3 Email 4 Email 5
But I would like it to just add new emails coming in: Email 1 Email 2 Email 3 +Email 4 +Email 5