Skip to content

Instantly share code, notes, and snippets.

@sreejithpro
Last active March 15, 2023 15:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sreejithpro/314694bb68c67c26ad2a16bb4bff238c to your computer and use it in GitHub Desktop.
Save sreejithpro/314694bb68c67c26ad2a16bb4bff238c to your computer and use it in GitHub Desktop.
Find unread emails in gmail and copy sender and subject in a google doc
function downloadUnreadEmails() {
var sheet = SpreadsheetApp.create('Unread Emails');
sheet.appendRow(['Sender', 'Subject']);
var threads = GmailApp.search('is:unread');
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
var sender = message.getFrom();
var subject = message.getSubject();
sheet.appendRow([sender, subject]);
}
threads[i].markRead();
}
var url = sheet.getUrl();
var exportUrl = url.replace(/edit$/, '') + 'export?format=xlsx';
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(exportUrl, {
headers: {
'Authorization': 'Bearer ' + token
}
});
var blob = response.getBlob();
var file = DriveApp.createFile(blob);
file.setName('Unread Emails.xlsx');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment