Skip to content

Instantly share code, notes, and snippets.

@Gabelbombe
Created April 3, 2017 15:34
Show Gist options
  • Select an option

  • Save Gabelbombe/e336e8507bcd729383d35e04bcffcc0e to your computer and use it in GitHub Desktop.

Select an option

Save Gabelbombe/e336e8507bcd729383d35e04bcffcc0e to your computer and use it in GitHub Desktop.
GoogleApps script using simple js to perform regexp searches.
function GmailAppRegexpSearch ()
{
var sheet = SpreadsheetApp.getActiveSheet();
var row = 2;
// Clear existing search results
sheet.getRange(2, 1, sheet.getMaxRows() - 1, 4).clearContent();
// Which Gmail Label should be searched?
var label = sheet.getRange("F3").getValue();
// Get the Regular Expression Search Pattern
var pattern = sheet.getRange("F4").getValue();
// Retrieve all threads of the specified label
var threads = GmailApp.search("in:" + label);
for (var i = 0 ; i < threads.length ; i++)
{
var messages = threads[i].getMessages();
for (var m = 0 ; m < messages.length ; m++)
{
var msg = messages[m].getBody();
// Does the message content match the search pattern?
if (msg.search(pattern) !== -1)
{
// Format and print the date of the matching message
sheet.getRange(row,1).setValue(
Utilities.formatDate(messages[m].getDate(),"GMT","yyyy-MM-dd")
);
// Print the sender's name and email address
sheet.getRange(row,2).setValue(messages[m].getFrom());
// Print the message subject
sheet.getRange(row,3).setValue(messages[m].getSubject());
// Print the unique URL of the Gmail message
var id = "https://mail.google.com/mail/u/0/#all/" + messages[m].getId();
sheet.getRange(row,4).setFormula('=hyperlink("' + id + '", "View")');
// Move to the next row
row++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment