Gmail Connector for MS Excel How to Get List of Messages for a Specific User

Introduction

In this article we will delve deeper into Gmail and MS Excel integration, and will learn how to get list of messages for a specific user. We are continuing from where we left off. By this time, you must have installed ODBC PowerPack, created ODBC Data Source, and configured authentication settings in your Gmail account .

So, let's not waste time and begin.

Use Query Builder to generate SQL query

  1. The first thing you have to do is open Query Builder:

    ZappySys API Driver - Gmail
    Read / search Gmail messages, download attachments, send mail and more using Gmail REST API.
    GmailDSN
    Open Query Builder in API ODBC Driver to read and write data to REST API
  2. Then simply select the Get List of Messages for a Specific User endpoint (action).

  3. Continue by configuring the Required parameters. You can also set optional parameters too.

  4. Move on by hitting Preview Data button to preview the results.

  5. If you see the results you need, simply copy the generated query:

    Get List of Messages for a Specific User
    Optional Parameters
    ApiVersion v1
    UserId
    Search Expression
    IncludeSpamTrash
    LabelIds
    MaxResults 100
    SELECT * FROM Messages
    Query Builder
  6. That's it! You can use this query in MS Excel.

Let's not stop here and explore SQL query examples, including how to use them in Stored Procedures and Views (virtual tables) in the next steps.

SQL query examples

Use these SQL queries in your MS Excel data source:

How to Get all messages

SELECT * FROM Messages

How to Get all messages for a specified user

Get all messages for a specified user id (mailbox). Thi is very slow row by row operation. For each message one API call is invoked so do not try this way unless you have to.

SELECT * FROM Messages
LIMIT 10 --This will only fetch first 10 messages from the result (Fetching all messages can be very slow (Rowbecuase its Row-By-Row operation)
WITH (UserID='me')
--WITH (UserID='firstname.lastname@domainname.com')

How to Get all messages with search condition(s)

This example shows how to use search condition(s) to filter mails for search operation. For more examples refer https://support.google.com/mail/answer/7190

SELECT * 
FROM Messages
--Use WITH clause --OR-- Key column(s) in WHERE clause. If you use Id in Where clause then Search parameter is ignored
--WHERE [Id] = '18fb7747b0136726' 
WITH(
	Search='after:<<yearstart+1d|~|yyyy/MM/dd,FUN_TO_DATE>> before:<<today-1d|~|yyyy/MM/dd,FUN_TO_DATE>>'  --search for mail sent between some dates (after year start plus one day and before yesterday (i.e. today-1d))
--	Search='after:2024/01/31 before:2024/05/25'  --use of static date

--for more examples refer https://support.google.com/mail/answer/7190

--  Search='FREE PRODUCT' --search example for=> has words anywhere in the subject or body
--  Search='"FREE PRODUCT"' --search example for=> has exact phrase anywhere in the subject or body
--  Search='subject:FREE PRODUCT' --search example for=> has subject (contains any word)
--  Search='subject:(FREE PRODUCT)' --search example for => has subject (contains exact phrase)
--  Search='Rfc822msgid:<008601dae391$fda78bf0$f8f6a3d0$@zappysys.com>' --search example for => Search by Rfc822MessageID column (Alternate Message ID for searching - you can find from get_messages or  get_message endpoint output
--  Search='to:david' --search example for => to (name)
--  Search='to:david.smith@gmail.com' --search example for => to (email)
--  Search='from:david' --search example for => from (name)
--  Search='from:david.smith@gmail.com' --search example for => from (email)
--  Search='from:(Platform Notifications) OR from:(no-reply@accounts.google.com) ' --search example for => from (name or email)
--  Search='cc:david' --search example for => cc (name)
--  Search='bcc:david' --search example for => bcc (name)
--  Search='is:unread AND subject:(Share request for)' --search example for => multiple conditions (AND)
--  Search='is:unread OR subject:(Share request for)' --search example for => multiple conditions (OR)
--  Search='from:someuser@example.com' --search example for => from sender=from:someuser@example.com
--  Search='from:a@company.com OR from:b@company.com' --search example for => from sender (OR)
--  Search='from:amy' --search example for => from sender by name=from:amy
--  Search='from:amy OR from:bob ' --search example for => from sender by name (OR)
--  Search='has:attachment' --search example for => has attachment=has:attachment
--  Search='larger:10M' --search example for => has attachment (larger than 10MB)
--  Search='smaller:10M' --search example for => has attachment (smaller than 10MB)
--  Search='smaller:10M' --search example for => has attachment (smaller than 10MB)
--  Search='1024' --search example for => size (exact bytes)
--  Search='unread=is:unread' --search example for => is unread=is:unread
--  Search='is:read' --search example for => is read=is:read
--  Search='after:2004/04/16' --search example for => date after=after:2004/04/16
--  Search='before:2004/04/16' --search example for => date before=before:2004/04/16
--  Search='older_than:2d' --search example for => age older than (2days)
--  Search='older_than:4y' --search example for => age older than (4years)
--  Search='newer_than:2d' --search example for => age newer than (2days)
--  Search='newer_than:4m' --search example for => age newer than (4months)
--  Search='from:someuser@example.com rfc822msgid:<008601dae391zzzzzzz> is:unread' --search example for => from specific sender with specific message id(RFC822) and unread emails only=from:someuser@example.com rfc822msgid:<somemsgid@example.com> is:unread
--  Search='in:sent after:2014/01/01 before:2014/02/01' --search example for => sent date range (static dates)
--  Search='in:sent after:<<yearstart-1day+22hour|~|yyyy/MM/dd,FUN_TO_DATE>> before:<<today-1day|~|yyyy/MM/dd,FUN_TO_DATE>>' --search example for => sent date range (dynamic dates). You can use keywords like now,today,yesterday,monthstart,monthend,yearstart,yearend,weekstart,weekend along with operator + / - [hour,minute,second,day,month,year]
	  
)

get_messages endpoint belongs to Messages table(s), and can therefore be used via those table(s).

Stored Procedures and Views

Create Custom Stored Procedure

You can create procedures to encapsulate custom logic and then only pass handful parameters rather than long SQL to execute your API call.

Steps to create Custom Stored Procedure in ZappySys Driver. You can insert Placeholders anywhere inside Procedure Body. Read more about placeholders here

  1. Go to Custom Objects Tab and Click on Add button and Select Add Procedure:
    ZappySys Driver - Add Stored Procedure

  2. Enter the desired Procedure name and click on OK:
    ZappySys Driver - Add Stored Procedure Name

  3. Select the created Stored Procedure and write the your desired stored procedure and Save it and it will create the custom stored procedure in the ZappySys Driver:
    Here is an example stored procedure for ZappySys Driver. You can insert Placeholders anywhere inside Procedure Body. Read more about placeholders here

    CREATE PROCEDURE [usp_get_orders]
        @fromdate = '<<yyyy-MM-dd,FUN_TODAY>>'
     AS
        SELECT * FROM Orders where OrderDate >= '<@fromdate>';
    

    ZappySys Driver - Create Custom Stored Procedure

  4. That's it now go to Preview Tab and Execute your Stored Procedure using Exec Command. In this example it will extract the orders from the date 1996-01-01:

    Exec usp_get_orders '1996-01-01';

    ZappySys Driver - Execute Custom Stored Procedure

  5. Let's generate the SQL Server Query Code to make the API call using stored procedure. Go to Code Generator Tab, select language as SQL Server and click on Generate button the generate the code.
    As we already created the linked server for this Data Source, in that you just need to copy the Select Query and need to use the linked server name which we have apply on the place of [MY_API_SERVICE] placeholder.

    SELECT * FROM OPENQUERY([LS_TO_GMAIL_IN_GATEWAY], 'EXEC usp_get_orders @fromdate=''1996-07-30''')

    ZappySys Driver - Generate SQL Server Query

  6. Now go to SQL served and execute that query and it will make the API call using stored procedure and provide you the response.
    ZappySys Driver - Generate SQL Server Query

Create Custom Virtual Table

ZappySys API Drivers support flexible Query language so you can override Default Properties you configured on Data Source such as URL, Body. This way you don't have to create multiple Data Sources if you like to read data from multiple EndPoints. However not every application support supplying custom SQL to driver so you can only select Table from list returned from driver.

If you're dealing with Microsoft Access and need to import data from an SQL query, it's important to note that Access doesn't allow direct import of SQL queries. Instead, you can create custom objects (Virtual Tables) to handle the import process.

Many applications like MS Access, Informatica Designer wont give you option to specify custom SQL when you import Objects. In such case Virtual Table is very useful. You can create many Virtual Tables on the same Data Source (e.g. If you have 50 URLs with slight variations you can create virtual tables with just URL as Parameter setting.

  1. Go to Custom Objects Tab and Click on Add button and Select Add Table:
    ZappySys Driver - Add Table

  2. Enter the desired Table name and click on OK:
    ZappySys Driver - Add Table Name

  3. And it will open the New Query Window Click on Cancel to close that window and go to Custom Objects Tab.

  4. Select the created table, Select Text Type AS SQL and write the your desired SQL Query and Save it and it will create the custom table in the ZappySys Driver:
    Here is an example SQL query for ZappySys Driver. You can insert Placeholders also. Read more about placeholders here

    SELECT
      "ShipCountry",
      "OrderID",
      "CustomerID",
      "EmployeeID",
      "OrderDate",
      "RequiredDate",
      "ShippedDate",
      "ShipVia",
      "Freight",
      "ShipName",
      "ShipAddress",
      "ShipCity",
      "ShipRegion",
      "ShipPostalCode"
    FROM "Orders"
    Where "ShipCountry"='USA'

    ZappySys Driver - Create Custom Table

  5. That's it now go to Preview Tab and Execute your custom virtual table query. In this example it will extract the orders for the USA Shipping Country only:

    SELECT * FROM "vt__usa_orders_only"

    ZappySys Driver - Execute Custom Virtual Table Query

  6. Let's generate the SQL Server Query Code to make the API call using stored procedure. Go to Code Generator Tab, select language as SQL Server and click on Generate button the generate the code.
    As we already created the linked server for this Data Source, in that you just need to copy the Select Query and need to use the linked server name which we have apply on the place of [MY_API_SERVICE] placeholder.

    SELECT * FROM OPENQUERY([LS_TO_GMAIL_IN_GATEWAY], 'EXEC [usp_get_orders] ''1996-01-01''')

    ZappySys Driver - Generate SQL Server Query

  7. Now go to SQL served and execute that query and it will make the API call using stored procedure and provide you the response.
    ZappySys Driver - Generate SQL Server Query

Get List of Messages for a Specific User in MS Excel

  1. In Excel click Data, then select Get Data, proceed with From Other Sources and choose From ODBC item. This will get data from ODBC data source we created:

    Create ODBC data source in Excel
  2. A small window opens, then simply select the data source you created in previous steps:

    Select DSN data source in Excel
  3. Most likely, you will be asked to authenticate to a newly created DSN. Just select Windows authentication option together with Use my current credentials option:

    Excel DSN Authentication
  4. Finally, you will be asked to select a table or view to get data from. Select one and load the data!

    Excel DSN Authentication
  5. Finally, finally, use data extracted from Gmail API in an Excel worksheet:

    Excel Select DSN Table

Refresh data source in Excel

When data updates in Gmail, it is not automatically refreshed in Excel. To update the worksheet, go to Data or Table Design tab and click the Refresh button:

Refresh data source in Excel

More actions supported by Gmail Connector

Learn how to perform other actions directly in MS Excel with these how-to guides:

More integrations

All
Data Integration
Database
BI & Reporting
Productivity
Programming Languages
Automation & Scripting
ODBC applications