Ready to get started?

Learn more about the CData JDBC Driver for Google Spreadsheets or download a free trial:

Download Now

Query Google Sheets Data in ColdFusion

Write standard ColdFusion data access code to connect to Google Sheets data.

The CData JDBC Driver for Google Sheets seamlessly integrates connectivity to Google Sheets data with the rapid development tools in ColdFusion. This article shows how to connect to Google Sheets data in ColdFusion and query Google Sheets tables.

Create a JDBC Data Source for Google Sheets in ColdFusion

The JDBC data source enables you to execute SQL from standard ColdFusion tags like cfquery and CFScript like executeQuery.

  1. Copy the driver JAR and .lic file from the installation directory onto the ColdFusion classpath. For example, copy the files into C:\MyColdFusionDirectory\cfusion\wwwroot\WEB-INF\lib. Or, open the Java and JVM page in the ColdFusion Administrator and enter the path to the files in the ColdFusion Class Path box.

    The JAR and license for the driver are located in the lib subfolder of the installation directory.

    Restart the server after this step.

  2. Add the driver as a data source:

    From the ColdFusion administrator interface, expand the Data & Services node and click Data Sources. In the Add New Data Source section, enter a name for the data source and select Other in the Driver menu.

  3. Populate the driver properties:

    • JDBC URL: Enter connection properties in the JDBC URL. The JDBC URL begins with jdbc:googlesheets: and is followed by the connection properties in a semicolon-separated list of name=value pairs.

      You can connect to a spreadsheet by providing authentication to Google and then setting the Spreadsheet connection property to the name or feed link of the spreadsheet. If you want to view a list of information about the spreadsheets in your Google Drive, execute a query to the Spreadsheets view after you authenticate.

      ClientLogin (username/password authentication) has been officially deprecated since April 20, 2012 and is now no longer available. Instead, use the OAuth 2.0 authentication standard. To access Google APIs on behalf on individual users, you can use the embedded credentials or you can register your own OAuth app.

      OAuth also enables you to use a service account to connect on behalf of users in a Google Apps domain. To authenticate with a service account, you will need to register an application to obtain the OAuth JWT values.

      See the Getting Started chapter in the help documentation to connect to Google Sheets from different types of accounts: Google accounts, Google Apps accounts, and accounts using two-step verification.

      Built-in Connection String Designer

      For assistance in constructing the JDBC URL, use the connection string designer built into the Google Sheets JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.

      java -jar cdata.jdbc.googlesheets.jar

      Fill in the connection properties and copy the connection string to the clipboard.

      A typical JDBC URL is below:

      jdbc:googlesheets:Spreadsheet=MySheet;InitiateOAuth=GETANDREFRESH
    • Driver Class: Enter the driver class. The driver class is cdata.jdbc.googlesheets.GoogleSheetsDriver.
    • Driver Name: Enter a user-defined name for the driver.
    • Username: Enter the username used to authenticate.
    • Password: Enter the password used to authenticate.

You can now test the connection by enabling the CData Google Sheets data source in the Actions column. After reporting a status of OK, the Google Sheets data source is ready for use.

Execute Queries

The cfquery tag can pass SQL statements to Google Sheets, including INSERT, UPDATE, and DELETE.. Use the cfqueryparam tag to create parameterized queries and prevent SQL injection through the query string.

<cfquery name="GoogleSheetsQuery" dataSource="CDataGoogleSheets"> SELECT * FROM Orders WHERE ShipCity = <cfqueryparam value="#ShipCity#" cfsqltype="cf_sql_varchar"> </cfquery> <cfdump var="#GoogleSheetsQuery#">

Below is the equivalent in CFScript:


<cfscript>
result = queryExecute(
  "SELECT * FROM Orders WHERE ShipCity = ?", 
  [
    { value="Madrid", cfsqltype="cf_sql_varchar" }
  ],
  { datasource="CDataGoogleSheets" }
);

writeDump( var= result );
</cfscript> 

You can then make requests to your .cfm like the following:

http://MyServer:8500/query.cfm?ShipCity=Madrid