Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Dottenpixel/a28730bebdaa292c0fff1dc940fb8dd2 to your computer and use it in GitHub Desktop.
Save Dottenpixel/a28730bebdaa292c0fff1dc940fb8dd2 to your computer and use it in GitHub Desktop.
Extract Gmail content to a spreadsheet #gas #gmail #sheet
var SHEET_ID = YOUR_SPREADSHEET_ID;
var SHEET_NAME = YOUR_SHEET_NAME;
function getEmails_(q){
var emails = [];
var thds = GmailApp.search(q);
for(var i in thds){
var msgs = thds[i].getMessages();
for(var j in msgs){
emails.push([msgs[j].getBody().replace(/<.*?>/g, '\n').replace(/^\s*\n/gm, '').replace(/^\s*/gm, '').replace(/\s*\n/gm, '\n')]);
}
}
return emails;
}
function appendData_(sheet, array2d){
sheet.getRange(sheet.getLastRow() + 1, 1, array2d.length, array2d[0].length).setValues(array2d);
}
function run(){
//Gmail Advanced search https://support.google.com/mail/answer/7190
var array2d = getEmails_("the");
if(array2d) {
var ss = SpreadsheetApp.openById(SHEET_ID);
var sheet = ss.getSheetByName(SHEET_NAME);
if(!sheet) sheet = ss.insertSheet(SHEET_NAME);
appendData_(sheet, array2d);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment