这个for循环确实存在准确的问题(我遇到了问题?)。
- 使用GmailApp.getInboxThreads(k,max)[k];但这跳过了其他所有线程。 (你能告诉我为什么吗?)
- 这只有在k = 0且max = 100时才有效。如果我尝试做k = 100和max = 100,我会得到一组空白数据。执行日志显示,当k = 100且max = 100时,完全跳过for循环。
提前感谢您的帮助!!!
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();
};
答案 0 :(得分:0)
请注意,for循环严格等于while循环:
//for (/*Init*/; /*Cond*/; /*Step*/) { /*Action*/ }
//stricty equal to :
/*Init*/;
while (/*Cond*/) {
/*Action*/;
/*Step*/;
}
因为k = max = 100,那么k < max
返回false,所以你不要进入循环。
如果你想保持k = max = 100,那么你应该使用k <= max
(小于或等于)或do-loop。
答案 1 :(得分:0)
我认为您在使用Gmail API或如何运行for循环时遇到问题。
关于第一个问题,请查看Gmail API doc
您可以使用:
getInboxThreads()
//OR
getInboxThreads(start, max)
// Where start if the index of the first of thread and max is the maximum number of thread retrieved
所以当你这样做时: GmailApp.getInboxThreads(K,最大)[k]的;
您将范围K的 ONE 元素K检索到最大
获取所有帖子:
var threads = GmailApp.getInboxThreads();
//OR
var threads = GmailApp.getInboxThreads(k, max);
//Where K is starting email and max maximum number of thread
您的循环必须从零开始,因为您正在使用上下文GmailApp中的数组。所以:
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
}