Skip to content

Instantly share code, notes, and snippets.

@pablocastelo
Last active May 5, 2017 19:01
Show Gist options
  • Save pablocastelo/8fe24cde57554cfaa214d4d3892f0fab to your computer and use it in GitHub Desktop.
Save pablocastelo/8fe24cde57554cfaa214d4d3892f0fab to your computer and use it in GitHub Desktop.
function getMails() {
var sheetId = ''; // use your Sheets ID here
var ss = SpreadsheetApp.openById(sheetId);
var s = ss.getSheetByName('logger');
var threads = GmailApp.search('-in:trash');
var a=[];
for (var i = 0; i < threads.length; i++) {
var messages = GmailApp.getMessagesForThread(threads[i]);
for (var j = 0; j < messages.length; j++) {
a[j]= [messages[j].getDate(), messages[j].getFrom(), messages[j].getSubject(), messages[j].getPlainBody()];
}
}
Logger.log(a);
var nextRow=s.getLastRow() + 1;
Logger.log("Next Row to write: " + nextRow);
var numRows=a.length;
Logger.log("Number of mails fetched: " + numRows);
if (numRows > 0) {
var numCols=a[0].length;
Logger.log("Number of rows per mail: " + numCols);
s.getRange(nextRow,1,numRows,numCols).setValues(a);
GmailApp.moveThreadsToTrash(threads);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment