Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of Gmail emails per sender

I'm trying to do a mass cleanout of my Gmail because search has gotten so slow. I would like to tally how many emails i've received from each sender. The closest solution I've found is this: https://webapps.stackexchange.com/questions/102162/can-i-get-a-list-of-the-senders-of-all-of-the-messages-in-my-inbox however that only tallies emails in Inbox, not in All Mail. I even tried moving all my 100k emails from All Mail to Inbox so that I could use it, but Gmail doesn't appear to let you move more than 3,500 messages in.

How do I adjust the Google Apps script to analyze All Mail instead of just Inbox? Or any other way of doing this welcomed too

function sender_list() {
  var inbox_threads = GmailApp.getInboxThreads();

  var sender_array = [];

  for (var i = 0; i < inbox_threads.length; i++) {
    var message = inbox_threads[i].getMessages();
    for (var x = 0; x < message.length; x++) {
      var sender = message[x].getFrom();
      sender_array.push([sender]);      
    }
  }
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getSheetByName('sheet1');
  sheet.clear();
  sheet.appendRow(['Email Address']);
  sender_array.sort();
  sheet.getRange(sheet.getLastRow()+1, 1, sender_array.length,1).setValues(sender_array);
}
like image 640
KHMan Avatar asked Dec 06 '19 16:12

KHMan


People also ask

How do I see the number of emails in my Gmail app?

What to Know. In your Gmail inbox screen, select Settings (gear icon), select See all settings, and then select the Advanced tab. In the Unread message icon section, select Enable, and then select Save Changes. Now, your Gmail Chrome tab will display the number of unread messages you currently have.

How do I show all emails from one sender in Gmail?

To find emails from a particular person type "from:" into the Gmail search bar. Then type the name of the sender. For example, if you're looking for an email from Jane Smith, you would type the following: "from: jane smith." Only emails from Jane Smith will appear as a search result.


1 Answers

This will get you a unique sender list

function sender_list() {
  var inbox_threads=GmailApp.search('in:anywhere');
  var sender_array=[];
  var uA=[];
  for(var i=0;i<inbox_threads.length;i++) {
    var message=inbox_threads[i].getMessages();
    for(var x=0;x<message.length; x++) {
      var sender=message[x].getFrom();  
      //prevent duplicates
      if(uA.indexOf(sender)==-1) {
        uA.push(sender);
        sender_array.push([sender]);      
      }
    }
  }
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet()
  sh.clear();
  sh.appendRow(['Email Address']);
  sh.getRange(2, 1,sender_array.length,1).setValues(sender_array).sort({column:1,ascending:true});

}

and this version adds the number of emails per sender:

function sender_list() {
  var inbox_threads=GmailApp.search('in:anywhere');
  var sender_array=[];
  var uA=[];
  var cObj={};
  for(var i=0;i<inbox_threads.length;i++) {
    var message=inbox_threads[i].getMessages();
    for(var x=0;x<message.length; x++) {
      var sender=message[x].getFrom();  
      if(uA.indexOf(sender)==-1) {
        uA.push(sender);
        sender_array.push([sender]);
        cObj[sender]=1;
      }else{
        cObj[sender]+=1;
      }
    }
  }
  sender_array.forEach(function(r){
    r.splice(1,0,cObj[r[0]]);
  });
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet()
  sh.clear();
  sh.appendRow(['Email Address','Count']);
  sh.getRange(2, 1,sender_array.length,2).setValues(sender_array).sort({column:1,ascending:true});

}

I have about 500 emails total. I don't store a lot of emails and I discard a lot of unwanted emails when they arrive. It took about 20 seconds to run. So I would imagine 100K emails will require you to run this in batches.

Batch Operations

The following code requires that that Gmail API be enabled.

To do a batch you could run this function to start with:

function sender_list_paged(token) {
  var token=token||null;
  var query="in:anywhere"; 
  var sender_array=[];
  var uA=[]
  var cObj={};
  do{
    var result=Gmail.Users.Messages.list("your gmail address", {maxResults:100,pageToken:token,q:query});
    var list=result;
    Logger.log(list);
    for(var i=0;i<list.messages.length;i++) {
      var sender=GmailApp.getMessageById(list.messages[i].id).getFrom();
      if(uA.indexOf(sender)==-1) {
        uA.push(sender);
        sender_array.push([sender]);
        cObj[sender]=1;
      }else{
        cObj[sender]+=1;
      }
    }
    token=list.nextPageToken
    PropertiesService.getUserProperties().setProperty("lastpagetoken", token);
  }while(token);
  sender_array.forEach(function(r){
    r.splice(1,0,cObj[r[0]]);
  });
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet()
  sh.clear();
  sh.appendRow(['Email Address','Count']);
  sh.getRange(2, 1,sender_array.length,2).setValues(sender_array).sort({column:1,ascending:true});
}

And then run the function again like this:

sender_list_paged(getLastPageToken());

function getLastPageToken() {
  return PropertiesService.getUserProperties().getProperty("lastpagetoken")
}

And I think that will work. But you may have to play with it as I haven't ever had to do that.

like image 186
Cooper Avatar answered Sep 26 '22 14:09

Cooper