简体   繁体   中英

How do I gather data from email thread in Google Apps Scripts? The same thread is repeated for the length of my inbox

I'm building a program in Google Apps Scripts that gathers the from email, label and the ID of the inbox threads. Currently, I have 2 for loops iterating through the messages in order to gather the information from the threads and place them into a 2D array. My end goal is to take the labels in the email threads gather the emails with a specific label, and any email that is not labeled, the program will label using the thread ID. The issue I'm having is that only the first thread is outputted about 45 times which is the length of my inbox. Im not sure whether I'm using an incorrect method, or there is something else wrong with my code that is preventing each thread from being read once. If you run the program the i in the first for loop iterates only through the first element and I'm not sure why.

 function TheaThreads(){
  var SS =SpreadsheetApp.getActiveSpreadsheet();
  var ThreadSheet = SS.getSheetByName("Threads");
  var threads = GmailApp.getInboxThreads();

   for (var i=0; i < threads.length; i++) {
    for (var j = 0; j < threads[i].getMessages().length; j++){
    Logger.log(i);
    var message = threads[i].getMessages()[j],
    label = threads[i].getLabels(),
    //subject = message.getSubject(),
    //content = message.getPlainBody(),
    ident = message.getId(),
    emailfrom = message.getFrom();
   if(label==null|| label== undefined|| label.length==0){
    label="No Label";
    }
   }

   var csNames = new Array();
   for(i=0; i<threads.length; i++){
     csNames[i] = new Array();
     csNames[i][0]= ident;
     csNames[i][1]= label;
     csNames[i][2]=emailfrom;
     Logger.log( ident);
 }

Remove

   var csNames = new Array();
   for(i=0; i<threads.length; i++){
     csNames[i] = new Array();
     csNames[i][0]= ident;
     csNames[i][1]= label;
     csNames[i][2]=emailfrom;
     Logger.log( ident);

and, before

for (var j = 0; j < threads[i].getMessages().length; j++){

add

var csNames = new Array();

and, after

 if(label==null|| label== undefined|| label.length==0){
label="No Label";

add

 csNames[j] = new Array();
 csNames[j][0]= ident;
 csNames[j][1]= label;
 csNames[j][2]=emailfrom;
 Logger.log( ident);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM