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.