Reputation: 33
I am a big amateur in Google App Script.
Have this script runing ok but need to implement one more fuction. Now i need this: The emails read by the script I need to be marked as read
I have tried to integrate several options but without positive result.
Could anyone help me to implement it in the script itself
function processInboxToSheet() {
//var threads = GmailApp.getInboxThreads();
// Have to get data separate to avoid google app script limit!
var start = 0;
var threads = GmailApp.search("newer_than:1d AND is:unread AND label:eur OR label:desc",0,100);
var sheet = SpreadsheetApp.getActiveSheet();
var result = [];
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
var content = messages[0].getPlainBody();
// implement your own parsing rule inside
if (content) {
var tmp;
tmp = content.match(/\b([A-B\d][A-B\d]{4})\b/);
var cod = (tmp && tmp[1]) ? tmp[1].trim() : 'Error';
tmp = content.match(/\b(\d+[R])/);
var prom = (tmp && tmp[1]) ? tmp[1].trim() : 'Error';
tmp = content.match(/\b(\d{2}\.\d{2}\)\b/);
var exp = (tmp && tmp[1]) ? tmp[1].trim() : 'Error';
sheet.appendRow([cod, prom, exp]);
Utilities.sleep(500);
}
}
};
Upvotes: 2
Views: 3173
Reputation: 19309
If I understand you correctly, you want to mark as read the first message in each of the threads. To do that, you have to use markRead().
Now, this is a method that corresponds to the GmailMessage class. Hence, you need to call this method from an instance of this class. Right now, in your code, you have the variable messages
which is an array of the messages in a thread, not a message. To access the individual messages, you have to specify an index of the array. The first message in the thread corresponds to message[0]
, so that's where you have to use markRead
.
So the only thing you would need to do is adding the following line of code anywhere inside the for
loop and after you have defined messages
:
messages[0].markRead();
(If you only want to mark the message as read if the message has body content, you would have to add the line above inside the if
block.)
Also, if you wanted to mark the thread as read instead (a GmailThread also has a markRead method, you could at this line of code at the beginning of the for
loop:
threads[i].markRead();
I hope this is of any help.
Upvotes: 4