Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // EXECUTION is running forever: https://i.stack.imgur.com/yZjMw.png
- // Execution time (1.749 s) is without the "refreshThreads" command
- // FROM: https://stackoverflow.com/questions/65036762/script-to-search-for-multiple-unread-labels-in-gmail-threads-at-once?noredirect=1#comment114980378_65036762
- // As from: https://stackoverflow.com/questions/63907655/record-incoming-gmail-messages-in-google-sheets
- // AND as from: https://stackoverflow.com/questions/63990923/data-is-not-written-to-google-spreadsheet
- function mail2Sheets() {
- var ss = SpreadsheetApp.getActive();
- var sheet = ss.getSheetByName('newRec'); //gets the right sheet
- var freshLabel = GmailApp.getUserLabelByName("00-fresh"); // ++++ will add this label in the end
- const query = "label:unread (label:00-atest OR label:00-btest)";
- var foundThreads = GmailApp.search(query);
- var newReceipts = [];
- for (var i = 0; i < foundThreads.length; i++) {
- var messages = foundThreads[i].getMessages();
- for (var m = 0; m < messages.length; m++) {
- var from = messages[m].getFrom(); //from field
- var to = messages[m].getTo(); //to field
- var time = messages[m].getDate(); //date field
- var subject = messages[m].getSubject(); //subject field
- var body = messages[m].getPlainBody(); //body field
- var mId = messages[m].getId(); //id field to create the link later
- newReceipts.push([from,to,time,subject,body,'https://mail.google.com/mail/u/0/#inbox/'+mId]);
- }
- }
- if(!foundThreads.length) return; // if there are no unread ones, do nothing.
- sheet.getRange(SpreadsheetApp.getActiveSheet().getLastRow()+1,2,newReceipts.length,newReceipts[0].length).setValues(newReceipts); //writes to the spreadsheet
- GmailApp.markThreadsRead(foundThreads); // marks as read
- freshLabel.addToThreads(foundThreads); // ++++ add label "fresh" to found threads
- GmailApp.refreshThreads(foundThreads); // PROBLEM HERE
- sheet.sort(4,false);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement