0

Hi im trying to export the Emailadresses from the senders in a label in Gmail Called "Suarez". But it's just running and never completing, it should be about 372 emails. Is it to much to print to the logger?

Here is what im trying:

function getEmailsadresses(){

var threads = GmailApp.search("in:suarez");

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

  var messages = threads[i].getMessages();

  for (var m = 0; m < messages.length; m++) {
    var msg = messages[m].getFrom();
  }
}

    Logger.log(msg()); // log from address of the message

}
3
  • It's not too much... You should put a breakpoint inside the 'for' loop and check if 'messages' contain an array with objects of your emails.
    – Ido Green
    Sep 2, 2014 at 12:15
  • I think i kind of get you, do you know how to put that?
    – John Smith
    Sep 2, 2014 at 12:30
  • I changed the first line to this: var threads = GmailApp.getUserLabelByName('suarez').getThreads(1,10); And now it seems to find the adresses but i get an error: TypeError: Henric Smith <[email protected]> is not a function, it's a string. (row: 29, file: Kod)
    – John Smith
    Sep 2, 2014 at 12:48

1 Answer 1

0

I use a script embedded in an spreadsheet the reads every messages in every threads in a Label that could probably be a good starting point for you.

My specific use case was to log SMS messages that my android phone sends me as emails each time I send or receive a text message.

So it will work for you by just changing the label name and maybe remove the data filtering I added on the message. Since I have a lot of data I set up a time trigger task manager to works in small bunches to avoid exceeding time limit.

Usage is simple : run the "startSMSLog" and go have a couple of coffees until the cell A1 in the spreadsheet stops being RED... that's it.

Code below : the start function

function startSMSLog(){
  var triggerID = ScriptApp.newTrigger('countSMSLogEmails').timeBased().everyMinutes(5).create().getUniqueId();
  var thisTrigger = PropertiesService.getScriptProperties().setProperty('thisTriggerSMS',triggerID);
  PropertiesService.getScriptProperties().setProperty('current thread SMS',0);
  var sh = SpreadsheetApp.getActive().getSheetByName('SMS'); // change to the name of your sheet
  sh.getRange('A1').setBackground('red');
  countSMSLogEmails(true);
}

and the "working" code :

function countSMSLogEmails(clearSheet){
  var sh = SpreadsheetApp.getActive().getSheetByName('SMS');
  var start = Number(PropertiesService.getScriptProperties().getProperty('current thread SMS'));
  var CallLogThreads = GmailApp.getUserLabelByName('SMS').getThreads(start,10);
  if(CallLogThreads.length==0){
    sh.sort(2,false);
    sh.getRange('A1').setBackground('#FFC');
    var triggers = ScriptApp.getProjectTriggers();
    var thisTrigger = PropertiesService.getScriptProperties().getProperty('thisTriggerSMS');
    for(var n in triggers){
      if(triggers[n].getUniqueId()==thisTrigger){ScriptApp.deleteTrigger(triggers[n])};
    }
    sh.getRange('A1').setValue('Subject  (Log on '+Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "dd-MMM-yy  HH:mm")+')');
    return;
  } else {
    PropertiesService.getScriptProperties().setProperty('current thread SMS',start+CallLogThreads.length);
  }
  var data = [];
  for(var n=0;n<CallLogThreads.length;n++){ 
    var thread = CallLogThreads[n];
    Logger.log('thread message count = '+thread.getMessageCount());
    var msg = thread.getMessages();
    var msgData = [];
    for(var m in msg){
      var msgDate = msg[m].getDate();
      var msgSubject = msg[m].getSubject(); 
      var msgBody = msg[m].getBody().replace(/[\n\r]/g,' ').replace(/&#39;/g,"'").replace(/&nbsp;/g,"!").replace(/&amp;/g,"&").replace(/&quot;/g,'"')
      .replace(/&gt;/g,'>').replace(/&lt;/g,'<').replace(/<br \/>/g,'\n').replace(/<br>/g,'\n');
      msgData.push([msgSubject,msgDate,msgBody]);
    }
    data.push(msgData);
  }
  var dataTotal = [];
  if (clearSheet==true) {
    dataTotal.push(['subject', 'date', 'message']);
  }
  for(var n in data){
    dataTotal = dataTotal.concat(data[n]);
  };
  Logger.log(JSON.stringify(dataTotal));
  if (clearSheet==true) {
    sh.clearContents();
    sh.getRange(1,1,dataTotal.length,dataTotal[0].length).setValues(dataTotal);
  }else{
    sh.getRange(sh.getLastRow()+1,1,dataTotal.length,dataTotal[0].length).setValues(dataTotal);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.