Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save renatomattos2912/cb264cb3232658406c018b8442b57b27 to your computer and use it in GitHub Desktop.
Save renatomattos2912/cb264cb3232658406c018b8442b57b27 to your computer and use it in GitHub Desktop.
function extractEmailAddresses() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var search = "in:spam is:all ";
var threads = GmailApp.search(search);
var row, messages, from, email;
try {
for (var x=0; x<threads.length; x++) {
from = threads[x].getMessages()[0].getFrom();
from = from.match(/\S+@\S+\.\S+/g);
if ( from.length ) {
email = from[0];
email = email.replace(">", "");
email = email.replace("<", "");
row = sheet.getLastRow() + 1;
sheet.getRange(row,1).setValue(email);
}
}
} catch (e) {
Logger.log(e.toString());
Utilities.sleep(5000);
}
}
// Remove Duplicate Email addresses
function cleanList() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getRange(4, 1, sheet.getLastRow()).getValues();
var newData = new Array();
for(i in data){
var row = data[i];
var duplicate = false;
for(j in newData){
if(row[0] == newData[j][0]){
duplicate = true;
}
}
if(!duplicate){
newData.push(row);
}
}
sheet.getRange(4, 2, newData.length, newData[0].length).setValues(newData);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment