Created
October 18, 2016 17:16
-
-
Save Valtervieira/951c4d9b64e7ca8f4fd1a0fce69766b6 to your computer and use it in GitHub Desktop.
Gmail-Sheets
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
https://developers.google.com/apps-script/reference/gmail/gmail-message | |
https://ctrlq.org/code/20053-save-gmail-to-google-spreadsheet | |
var SEARCH_QUERY = "label:inbox"; | |
// Credit: https://gist.github.com/oshliaer/70e04a67f1f5fd96a708 | |
function getEmails_(q) { | |
var emails = []; | |
var threads = GmailApp.search(q); | |
for (var i in threads) { | |
var msgs = threads[i].getMessages(); | |
for (var j in msgs) { | |
emails.push([msgs[j].getFrom()+';'+msgs[j].getTo()+';'+msgs[j].getSubject()]); | |
} | |
} | |
return emails; | |
} | |
function appendData_(sheet, array2d) { | |
sheet.getRange(sheet.getLastRow() + 1, 1, array2d.length, array2d[0].length).setValues(array2d); | |
} | |
function saveEmails() { | |
var array2d = getEmails_(SEARCH_QUERY); | |
if (array2d) { | |
appendData_(SpreadsheetApp.getActiveSheet(), array2d); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment