Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

javascript - Google scripts fetching from GmailApp problem

I really need your help with a simple function that is not working for me, I am trying to fetch all the emails in my inbox under a specific label, As I am doing it, It is fetching every mail about 10 times as duplications, (I have 120 emails in all Gmail, It fetched 800+ mails)

I checked it multiple times and I don't get the problem.

Hopefully, you will see it Thank you in advance!

function getGmailEmails(){
  var label = GmailApp.getUserLabelByName("ToBeProcessedByGAS");
  var threads = label.getThreads();

  for(var i = threads.length - 1; i>=0; i--){
    var messages = threads[i].getMessages();

    for(var j = 0; j <messages.length; j++){
      var message = messages[j];
      extractDetails(message);
      GmailApp.markMessageRead(message);
    }
    threads[i].removeLabel(label);

  }
}

function extractDetails(message){
  var dateTime = message.getDate();
  var subjectText = message.getSubject();
  var senderDetails = message.getFrom();
  var bodyContents = message.getPlainBody();

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Mails")
  //var activesheets = SpreadsheetApp.getActiveSpreadsheet().sheet;
  sheet.appendRow([dateTime,senderDetails,subjectText,bodyContents]);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What if you try to rename the label instead of removing it?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...