Google Apps Script to Extract Emails

If you have a generic email that is being sent to you regularly and you happen to be a gmail user, you can easily extract that data into a spreadsheet where you can further parse the data into a database if that particular email follows a template.

Zapier recently introduced an email parser but it was too much of a hassle to actually use (although it seemed really cool and easy at first). Plus the integration with a database was so cumbersome that I just gave up and found my own solution.

I receive notifications of my app sales from multiple vendors and I create some formulas in a basic spreadsheet to exract the data I need.

Install Google Addon

We created a Google Addon with easy to use filters so you don’t have to code your own script.

Below is a script that extracts the body of an email that has been pre-labled “your custom label”. When you enter into Google Docs, create a spreadsheet, click the tools tab and select script editor. Once in the editor, add the following code into the window:

var sheet = SpreadsheetApp.getActiveSheet();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();

function getEmails() {
  var label = GmailApp.getUserLabelByName("your custom label");
  var threads = label.getThreads();
  var row = 2;
  for (var i = 0; i < threads.length; i++) {
    var messages=threads[i].getMessages();
    for (var m=0; m < messages.length; m++) {   

function onOpen() {
  var menuEntries = [ {name: "Load Emails", functionName: "getEmails"} ];
  spreadsheet.addMenu("Email", menuEntries);

Save the file and reload your empty spreadsheet. You will notice that there is now a new tab in your main menu that is labeled “Email”. Select “Load Email” and the script will fetch the body of your emails and add them to the second row.

If you want to add subject lines and sender info you can use the getSubject() or the getFrom() methods and append it to you setValue method. A list of Gmail class methods can be found in the Google Apps Documentation

Also, if you happen to be planning on extracting more than a hundred or so emails, you may run into a limit restriction that Google sets on fetching emails.

It’s recommended that you add the following to your loops:


It didn’t work for me, so I split up my function into separate parts and that got the job done.

Install Google Addon

We created a Google Addon with easy to use filters so you don’t have to code your own script.

12 responses to “Google Apps Script to Extract Emails”
  1. Don't interrupt me! Avatar
    Don’t interrupt me!

    Thanks for this great starter. What I’m trying to elaborate now is a script that appends extracted values in the same spreadsheet if run regularly. Right now it fetches values to the spreadsheet again and again. I tried to duct tape it myself with a little help from Google Developers documentation but I won’t get it running. Would you help me out? 馃檪

    1. dchun Avatar

      A bit late but if you still need help, pm me

      1. jay Avatar

        Hey Dchun I am actually looking for a bit of help with something similar, are you available to help at all?


        1. David Chun Avatar

          Hi Jay, send over details about what you’re trying to accomplish and your budget here: and I will get back to you.

  2. upasana sharma Avatar
    upasana sharma

    hello sir.
    when I run this code in spreadsheet.the email link appears but after that when I click on load email it shows cannot call method “getThreads” of null. how can I remove this error.

  3. upasana sharma Avatar
    upasana sharma

    hello sir.
    after load email. it show TypeError: Cannot call method “getThreads” of null.

    1. David Chun Avatar

      That means that you don’t have any emails that have been labeled by the string you set to the label variable in the line before calling the getThread function.

  4. Joel Avatar

    I’m trying to copy just the table that my customer send me on the email, is not an attachment is a table on the message. I created a macro in excel that separates the data on the table as I need it to create different report but I have to copy from email to excel and than run the macro, I’m trying to get all the tables they send me in one file so I can copy it from there or try to do a macro in google sheets to do the report for me.

  5. jay Avatar

    Whoops, dunno if that one worked, but I am looking for assistance with something very similar to this. Interested?

  6. upasana sharma Avatar

    var threads = GmailApp.getInboxThreads(401, 600);
    when i fetch emails it gives error.(Argument max cannot exceed 500).
    Please help me how can i fetch emails above 500 limit.

  7. Thom C Avatar
    Thom C

    Thanks for this, it turned out top be just what I needed.
    For those, like me, who haven’t written script before the ‘append it to you setValue method’ looks like this:
    for (var m=0; m < messages.length; m++) {
    (I'm used to adding delimited parameters in the setValue but learned I needed to set the column in the sheet.)

    If anyone figured out how to use nested labels or labels with special characters feel free to let us know.

  8. Lachezar Georgiev Avatar
    Lachezar Georgiev


    I am looking for something similar, but for some reason I can’t even make the example script above work. I will be grateful if you have some time to respond. Thank you!