0

This for loop is really having problems being accurate (I am having problems?).

-Was using GmailApp.getInboxThreads(k,max)[k]; but this skipped every other thread. (can you tell me why?)

-Now this only works if k=0 and max =100. If I try to do k=100 and max =100 I get a blank set of data. Execution log shows that with k=100 and max =100 the for loop is completely skipped.

Thank you for your help in advance!!!

function newEmailAddressList(){

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName("NewEmails");
 var range = sheet.getRange("A3:Q");
 var varRange = sheet.getRange("A2:G2");
 var addrA = [];



 range.clearContent(); //May need to change this to my delete function to speed things up.

 var contact = ContactsApp.getContacts();       //var contact = ContactsApp.getContacts();

 //////////

  for(var i = 0; i < contact.length; i++){      //for(var i = 0;i < contact.length;i++){

   //var addrA = [];                             //var addrA = []; This can't be here. It is not global or accessable if it is in this loop and the other global version doesn't access it apparently

   var addresses = contact[i].getEmails();     // var addresses = contact[i].getEmails();  

   for(var j = 0;j < addresses.length; j++){    //for(var j = 0;j < addresses.length;j++) {                           

      var address = addresses[j].getAddress();                //   s += addresses[j].getAddress() + br;

     addrA.push(address);                       //   
    }; 


}; /////////////////////AT THIS POINT CONTACTS ARRAY IS COMPLETE/////////////////////////

 var joinAddr = addrA.join();
 //var lowerAddr = joinAddr.toLowerCase();

 var startingEmail = sheet.getRange("C2").getValue();
 var numEmails = sheet.getRange("E2").getValue();

 var max = numEmails;



 for (var k = startingEmail; k<max; ++k){

 var threads = GmailApp.getInboxThreads()[k]; //get max threads starting at most recent thread
 var messages = threads.getMessages()[0];

       var sndr; 
       var rcpnt;
       var srAry = [];



       var sndr = messages.getFrom().replace(/^.+<([^>]+)>$/, "$1"); //http://stackoverflow.com/questions/26242591/is-there-a-way-to-get-the-specific-email-address-from-a-gmail-message-object-in      
       var sndrLower = sndr.toLowerCase;

       var rcpnt = messages.getTo().replace(/^.+<([^>]+)>$/, "$1");
       var rcpntLower = rcpnt.toLowerCase;

       var cc = messages.getCc().replace(/^.+<([^>]+)>$/, "$1");
       var ccLower = cc.toLowerCase;

       //srAry.push(sndr);
       //srAry.push(rcpnt);
       //srAry.push(cc);



    var isIn = joinAddr.search(sndr || rcpnt);

     if(isIn == -1){

      var instance = k;
      var dat = messages.getDate();
      //var sndr = messages.getFrom();
      //var rcpnt = messages.getTo();
      var sub = messages.getSubject();
      var msg = messages.getPlainBody();


      var info = [instance,dat,sndr,rcpnt,cc,sub,msg];

       sheet.appendRow(info); //appendRow only works with sheet class not range class



};//else{ break;  };

};

//Browser.msgBox(("Here are your New Contacts from Emails "k" through "k + max"!"));

getContactGroups();

};
Clayten
  • 53
  • 1
  • 9

2 Answers2

0

Notice that for loop is strictly equal to a while loop :

//for (/*Init*/; /*Cond*/; /*Step*/) { /*Action*/ }
//stricty equal to :
/*Init*/;
while (/*Cond*/) {
    /*Action*/;
    /*Step*/;
}

So because k = max = 100, then k < max returns false, so you don't enter in the loop. If you want to keep k = max = 100, then you should use k <= max (smaller or equal than) or a do-loop.

Julien Vernay
  • 295
  • 3
  • 13
  • Thank you Julien. Yes that was the problem. I was treating the range in the for loop as the same thing as the start and max points in the getInboxThreads(). Any thoughts on this problem? http://stackoverflow.com/questions/42615392/transient-errors-skipping-blank-cells-with-if-clause – Clayten Mar 05 '17 at 23:46
0

I think you are having problem with how to use the Gmail API or how to run the for loop.

For your first question, check Gmail API doc

You can use :

getInboxThreads()
//OR 
getInboxThreads(start, max)
// Where start if the index of the first of thread and max is the maximum number of thread retrieved

So when you do : GmailApp.getInboxThreads(k,max)[k];

You retrieve ONE element K of the range K to max

To get All threads :

var threads = GmailApp.getInboxThreads();
//OR
var threads = GmailApp.getInboxThreads(k, max);
//Where K is starting email and max maximum number of thread

Your loop must start from zero because you are working on an array out of the context GmailApp. So :

var threads = GmailApp.getInboxThreads(k, max);
//threads is an array of threads range from 0 to max?
//Don't use k again!
for(var i=0; i<threads.length;i++){
 var currentThread = threads[i];
 //do what you need with the thread
}
Yooz
  • 2,506
  • 21
  • 31
  • Thank you!!! Keeping k=0 solved my problem. I've kept the code like this: does it work better to have "gmailApp.GetInboxThreads before or after the loop? var joinAddr = addrA.join(); //var lowerAddr = joinAddr.toLowerCase(); var startingEmail = sheet.getRange("C2").getDisplayValue(); var numEmails = sheet.getRange("E2").getDisplayValue(); var max = numEmails; for (var k = 0; k – Clayten Mar 05 '17 at 23:42
  • Before, it has to be before because you loop on the results of the call – Yooz Mar 05 '17 at 23:44
  • Any thoughts on this problem? http://stackoverflow.com/questions/42615392/transient-errors-skipping-blank-cells-with-if-clause – Clayten Mar 05 '17 at 23:46