Last active
May 26, 2024 07:05
-
-
Save d-neri/65d6646504cb6cf49ea829b35b73dd11 to your computer and use it in GitHub Desktop.
Google Apps Script - Gmail count emails by sender
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); | |
} | |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wonderful script! Thanks @d-neri