Created
January 17, 2020 16:16
-
-
Save rusco/1d9b05eaf5dc4657f307d6734d9915f7 to your computer and use it in GitHub Desktop.
Reads Gmail into a Google Sheet
This file contains hidden or 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
// | |
// date: 17.01.2020, jr | |
// | |
var SEARCH_QUERY = "label:inbox is:unread to:me"; | |
var SEARCH_QUERY_ALL = "label:inbox to:me"; | |
function getEmails_(q) { | |
var email = []; | |
var threads = GmailApp.search(q); | |
for (var i in threads) { | |
var msgs = threads[i].getMessages(); | |
for (var j in msgs) { | |
var mail = msgs[j]; | |
email.push([mail.getSubject(), mail.getFrom(), mail.getDate(), mail.isUnread(), mail.getPlainBody().substring(0, 100)]); | |
} | |
} | |
return email; | |
} | |
function appendDataTest_(arr) { | |
var mailSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Mail"); | |
var len = arr.length + 1; | |
mailSheet.getRange("Mail!A2:E" + len ).setValues(arr); | |
} | |
function saveEmails() { | |
var array2d = getEmails_(SEARCH_QUERY); | |
if (array2d) { | |
appendDataTest_(array2d); | |
} | |
} | |
function onOpen() { | |
var spreadsheet = SpreadsheetApp.getActive(); | |
var menuItems = [ | |
{ name: "Read my Mail", functionName: "saveEmails" } | |
]; | |
spreadsheet.addMenu("Admin", menuItems); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment