-
-
Save d-neri/65d6646504cb6cf49ea829b35b73dd11 to your computer and use it in GitHub Desktop.
// Execute the "run" function below after setting up the appropriate APIs | |
// Original credit: https://stackoverflow.com/a/59222719/501042 | |
function sender_list_paged(token) { | |
var dt = new Date().getTime(); | |
var ss = SpreadsheetApp.create('Gmail count emails by sender ' + dt); | |
var sh = ss.getActiveSheet() | |
sh.clear(); | |
sh.appendRow(['Email Address', 'Count']); | |
var token = token || null; | |
var query = "in:inbox is:unread"; | |
var sender_array = []; | |
var uA = [] | |
var cObj = {}; | |
do { | |
var result = Gmail.Users.Messages.list("<EMAIL>", { 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 | |
if (token) { | |
PropertiesService.getUserProperties().setProperty("lastpagetoken", token); | |
} | |
} while (token); | |
sender_array.forEach(function (r) { | |
r.splice(1, 0, cObj[r[0]]); | |
}); | |
sh.getRange(2, 1, sender_array.length, 2).setValues(sender_array).sort({ column: 2, ascending: false }); | |
} | |
function getLastPageToken() { | |
return PropertiesService.getUserProperties().getProperty("lastpagetoken") | |
} | |
function run() { | |
PropertiesService.getUserProperties().setProperty("lastpagetoken", ''); | |
sender_list_paged(getLastPageToken()); | |
} | |
Nice one
Beautiful script!
Well, beside permissions, the user's actual email must be substituted for "" in line 15.
Otherwise,
Error
GoogleJsonResponseException: API call to gmail.users.messages.list failed with error: Invalid user id specified in request/Delegation denied
sender_list_paged @ sender_list.gs:15
Wonderful script! Thanks @d-neri
I seem unable to approve permissions, it lets me click on my gmail account after clikcing 'Review Permissions' but then comes back with "This App is Blocked as it tried to access sensitive information in your Gmail account"
Is there something else I need to complete
i received this error: "Exception: Too many simultaneous invocations". Would be grateful for any advice to solve this. thanks
The script worked nicely.
Thanks.
Thank you very much for the wonderful script. I am getting an "Exceeded maximum execution time." error. Can anyone suggest a fix for this?
Any help would be greatly appreciated. Thank you.
You can use this script instead- https://gist.github.com/fabianem/5012c0b9b44d0bd2c167ab7c81a82937
Brilliant @d-neri. Simply, brilliant.