Skip to main content
typo
Source Link
varblob
  • 81
  • 1
  • 2

Based off of @Jacob Jan Tuinstra's@jacob-jan-tuinstra's answer. Made some minor modifications to suit my needs perhaps others would find it useful.

function getEmails() { // set spreadsheet and retrieve labels var query = 'YOUR GMAIL SEARCH QUERY GOES HERE', ss = SpreadsheetApp.getActiveSpreadsheet(), sh = ss.getSheetByName(query) || ss.insertSheet(query, ss.getSheets().length), uniqueEmails = {}, errors = [], // max chunk size chunkSize = 500, currentChunk = 0, threads, messages, i, j, k, msg, tos; sh.clear(); while (currentChunk === 0 || threads.length === chunkSize) { try { // grab threads that match the query one chunk at a time threads = GmailApp.search(query, chunkSize * currentChunk, chunkSize); // grab corresponding messages from the threads messages = GmailApp.getMessagesForThreads(threads); for (i = 0; i < messages.length; i++) { msg = messages[i]; for (j = 0; j < msg.length; j++) { // get the from uniqueEmails[msg[j].getFrom()] = true; // get the reply to uniqueEmails[msg[j].getReplyTo()] = true; // grab from the to field alas well // this has a bug for people with commas in their names // tos = msg[j].getTo().split(',');   // for (k = 0; k < tos.length; k++) { // uniqueEmails[tos[k]] = true;   // } } } currentChunk += 1; } catch (e) { errors.push(e); } } // create 2D-array var aUnique = []; for (k in uniqueEmails) { aUnique.push([k]); } // add data to corresponding sheet sh.getRange(1, 1, aUnique.length, 1).setValues(aUnique); } 

Based off of @Jacob Jan Tuinstra's answer. Made some minor modifications to suit my needs perhaps others would find it useful.

function getEmails() { // set spreadsheet and retrieve labels var query = 'YOUR GMAIL SEARCH QUERY GOES HERE', ss = SpreadsheetApp.getActiveSpreadsheet(), sh = ss.getSheetByName(query) || ss.insertSheet(query, ss.getSheets().length), uniqueEmails = {}, errors = [], // max chunk size chunkSize = 500, currentChunk = 0, threads, messages, i, j, k, msg, tos; sh.clear(); while (currentChunk === 0 || threads.length === chunkSize) { try { // grab threads that match the query one chunk at a time threads = GmailApp.search(query, chunkSize * currentChunk, chunkSize); // grab corresponding messages from the threads messages = GmailApp.getMessagesForThreads(threads); for (i = 0; i < messages.length; i++) { msg = messages[i]; for (j = 0; j < msg.length; j++) { // get the from uniqueEmails[msg[j].getFrom()] = true; // get the reply to uniqueEmails[msg[j].getReplyTo()] = true; // grab from the to field al well tos = msg[j].getTo().split(','); for (k = 0; k < tos.length; k++) { uniqueEmails[tos[k]] = true; } } } currentChunk += 1; } catch (e) { errors.push(e); } } // create 2D-array var aUnique = []; for (k in uniqueEmails) { aUnique.push([k]); } // add data to corresponding sheet sh.getRange(1, 1, aUnique.length, 1).setValues(aUnique); } 

Based off of @jacob-jan-tuinstra's answer. Made some minor modifications to suit my needs perhaps others would find it useful.

function getEmails() { // set spreadsheet and retrieve labels var query = 'YOUR GMAIL SEARCH QUERY GOES HERE', ss = SpreadsheetApp.getActiveSpreadsheet(), sh = ss.getSheetByName(query) || ss.insertSheet(query, ss.getSheets().length), uniqueEmails = {}, errors = [], // max chunk size chunkSize = 500, currentChunk = 0, threads, messages, i, j, k, msg, tos; sh.clear(); while (currentChunk === 0 || threads.length === chunkSize) { try { // grab threads that match the query one chunk at a time threads = GmailApp.search(query, chunkSize * currentChunk, chunkSize); // grab corresponding messages from the threads messages = GmailApp.getMessagesForThreads(threads); for (i = 0; i < messages.length; i++) { msg = messages[i]; for (j = 0; j < msg.length; j++) { // get the from uniqueEmails[msg[j].getFrom()] = true; // get the reply to uniqueEmails[msg[j].getReplyTo()] = true; // grab from the to field as well // this has a bug for people with commas in their names // tos = msg[j].getTo().split(',');   // for (k = 0; k < tos.length; k++) { // uniqueEmails[tos[k]] = true;   // } } } currentChunk += 1; } catch (e) { errors.push(e); } } // create 2D-array var aUnique = []; for (k in uniqueEmails) { aUnique.push([k]); } // add data to corresponding sheet sh.getRange(1, 1, aUnique.length, 1).setValues(aUnique); } 
added in to field grabber and also moved the query to a var
Source Link
varblob
  • 81
  • 1
  • 2
function getEmails() { // set spreadsheet and retrieve labels var query = 'YOUR GMAIL SEARCH QUERY GOES HERE', ss = SpreadsheetApp.getActiveSpreadsheet(), sh = ss.getSheetByName('emails'query) || ss.insertSheet('emails'query, ss.getSheets().length), uniqueEmails = {}, errors = [], // maximummax chunk size chunkSize = 500, currentChunk = 0, threads, messages;messages, // clear thei,  sheet j, k, msg, tos; sh.clear(); while (currentChunk === 0 || threads.length === chunkSize) { try { // getgrab threads forthat searchmatch the query one chunk at a time threads = GmailApp.search('YOUR GMAIL SEARCH QUERY GOES HERE'query, chunkSize * currentChunk, chunkSize); // get allgrab thecorresponding messages forfrom the retrieved threads messages = GmailApp.getMessagesForThreads(threads); for (var i = 0; i < messages.length; i++) { var msg = messages[i]; //for process(j all= the0; messagesj grabbing< frommsg.length; andj++) reply{  to and put them in uniqueEmails  // get the from  for (var j = 0; j < msg.length; j++ uniqueEmails[msg[j].getFrom()] {= true; uniqueEmails[msg[j].getFrom()]// =get true;the reply to uniqueEmails[msg[j].getReplyTo()] = true; }  // grab from the to field al }well currentChunk += 1; tos = msg[j].getTo().split(','); } catch for (ek = 0; k < tos.length; k++) { // I don't actually do anything withuniqueEmails[tos[k]] these= buttrue;  there stored for now should probably print them }   }  } currentChunk += 1; } catch (e) { errors.push(e); } } // create 2D-array var aUnique = []; for (var k in uniqueEmails) { aUnique.push([k]); } // add data to corresponding sheet sh.getRange(1, 1, aUnique.length, 1).setValues(aUnique); } 
function getEmails() { // set spreadsheet and retrieve labels var ss = SpreadsheetApp.getActiveSpreadsheet(), sh = ss.getSheetByName('emails') || ss.insertSheet('emails', ss.getSheets().length), uniqueEmails = {}, errors = [], // maximum chunk size chunkSize = 500, currentChunk = 0, threads, messages; // clear the sheet sh.clear(); while (currentChunk === 0 || threads.length === chunkSize) { try { // get threads for search threads = GmailApp.search('YOUR GMAIL SEARCH QUERY GOES HERE', chunkSize * currentChunk, chunkSize); // get all the messages for the retrieved threads messages = GmailApp.getMessagesForThreads(threads); for (var i = 0; i < messages.length; i++) { var msg = messages[i]; // process all the messages grabbing from and reply to and put them in uniqueEmails  for (var j = 0; j < msg.length; j++) { uniqueEmails[msg[j].getFrom()] = true; uniqueEmails[msg[j].getReplyTo()] = true; }  } currentChunk += 1; } catch (e) { // I don't actually do anything with these but there stored for now should probably print them errors.push(e); } } // create 2D-array var aUnique = []; for (var k in uniqueEmails) { aUnique.push([k]); } // add data to sheet sh.getRange(1, 1, aUnique.length, 1).setValues(aUnique); } 
function getEmails() { // set spreadsheet and retrieve labels var query = 'YOUR GMAIL SEARCH QUERY GOES HERE', ss = SpreadsheetApp.getActiveSpreadsheet(), sh = ss.getSheetByName(query) || ss.insertSheet(query, ss.getSheets().length), uniqueEmails = {}, errors = [], // max chunk size chunkSize = 500, currentChunk = 0, threads, messages, i,   j, k, msg, tos; sh.clear(); while (currentChunk === 0 || threads.length === chunkSize) { try { // grab threads that match the query one chunk at a time threads = GmailApp.search(query, chunkSize * currentChunk, chunkSize); // grab corresponding messages from the threads messages = GmailApp.getMessagesForThreads(threads); for (i = 0; i < messages.length; i++) { msg = messages[i]; for (j = 0; j < msg.length; j++) {  // get the from   uniqueEmails[msg[j].getFrom()] = true; // get the reply to uniqueEmails[msg[j].getReplyTo()] = true;  // grab from the to field al well  tos = msg[j].getTo().split(',');  for (k = 0; k < tos.length; k++) { uniqueEmails[tos[k]] = true;   }   }  } currentChunk += 1; } catch (e) { errors.push(e); } } // create 2D-array var aUnique = []; for (k in uniqueEmails) { aUnique.push([k]); } // add data to corresponding sheet sh.getRange(1, 1, aUnique.length, 1).setValues(aUnique); } 
more formatting adjustments
Source Link
varblob
  • 81
  • 1
  • 2

Uses search, doesn't break up by labels and also does chunking for large inboxes.

 function getEmails() { // set spreadsheet and retrieve labels var ss = SpreadsheetApp.getActiveSpreadsheet(), sh = ss.getSheetByName('emails') || ss.insertSheet('emails', ss.getSheets().length), uniqueEmails = {}, errors = [], // maximum chunk size chunkSize = 500, currentChunk = 0, threads, messages; // clear the sheet sh.clear(); while (currentChunk === 0 || threads.length === chunkSize) { try { // get threads for search threads = GmailApp.search('YOUR GMAIL SEARCH QUERY GOES HERE', chunkSize * currentChunk, chunkSize); // get all the messages for the retrieved threads messages = GmailApp.getMessagesForThreads(threads); for (var i = 0; i function getEmails() { // set spreadsheet and retrieve labels var ss = SpreadsheetApp.getActiveSpreadsheet(), sh = ss.getSheetByName('emails') || ss.insertSheet('emails', ss.getSheets().length), uniqueEmails = {}, errors = [], // maximum chunk size chunkSize = 500, currentChunk = 0, threads, messages; // clear the sheet sh.clear(); while (currentChunk === 0 || threads.length === chunkSize) { try { // get threads for search threads = GmailApp.search('YOUR GMAIL SEARCH QUERY GOES HERE', chunkSize * currentChunk, chunkSize); // get all the messages for the retrieved threads messages = GmailApp.getMessagesForThreads(threads); for (var i = 0; i < messages.length; i++) { var msg = messages[i]; // process all the messages grabbing from and reply to and put them in uniqueEmails for (var j = 0; j < msg.length; j++) { uniqueEmails[msg[j].getFrom()] = true; uniqueEmails[msg[j].getReplyTo()] = true; } } currentChunk += 1; } catch (e) { // I don't actually do anything with these but there stored for now should probably print them errors.push(e); } } // create 2D-array var aUnique = []; for (var k in uniqueEmails) { aUnique.push([k]); } // add data to sheet sh.getRange(1, 1, aUnique.length, 1).setValues(aUnique); } 

Uses search, doesn't break up by labels and also does chunking.

 function getEmails() { // set spreadsheet and retrieve labels var ss = SpreadsheetApp.getActiveSpreadsheet(), sh = ss.getSheetByName('emails') || ss.insertSheet('emails', ss.getSheets().length), uniqueEmails = {}, errors = [], // maximum chunk size chunkSize = 500, currentChunk = 0, threads, messages; // clear the sheet sh.clear(); while (currentChunk === 0 || threads.length === chunkSize) { try { // get threads for search threads = GmailApp.search('YOUR GMAIL SEARCH QUERY GOES HERE', chunkSize * currentChunk, chunkSize); // get all the messages for the retrieved threads messages = GmailApp.getMessagesForThreads(threads); for (var i = 0; i 

Uses search, doesn't break up by labels and also does chunking for large inboxes.

function getEmails() { // set spreadsheet and retrieve labels var ss = SpreadsheetApp.getActiveSpreadsheet(), sh = ss.getSheetByName('emails') || ss.insertSheet('emails', ss.getSheets().length), uniqueEmails = {}, errors = [], // maximum chunk size chunkSize = 500, currentChunk = 0, threads, messages; // clear the sheet sh.clear(); while (currentChunk === 0 || threads.length === chunkSize) { try { // get threads for search threads = GmailApp.search('YOUR GMAIL SEARCH QUERY GOES HERE', chunkSize * currentChunk, chunkSize); // get all the messages for the retrieved threads messages = GmailApp.getMessagesForThreads(threads); for (var i = 0; i < messages.length; i++) { var msg = messages[i]; // process all the messages grabbing from and reply to and put them in uniqueEmails for (var j = 0; j < msg.length; j++) { uniqueEmails[msg[j].getFrom()] = true; uniqueEmails[msg[j].getReplyTo()] = true; } } currentChunk += 1; } catch (e) { // I don't actually do anything with these but there stored for now should probably print them errors.push(e); } } // create 2D-array var aUnique = []; for (var k in uniqueEmails) { aUnique.push([k]); } // add data to sheet sh.getRange(1, 1, aUnique.length, 1).setValues(aUnique); } 
Source Link
varblob
  • 81
  • 1
  • 2
Loading