Ready to get started?

Download a free trial of the Google Spreadsheets Cmdlets to get started:

 Download Now

Learn more:

Google Spreadsheets Icon Google Spreadsheets Cmdlets

An easy-to-use set of PowerShell Cmdlets offering real-time access to Google Spreadsheets data. The Cmdlets allow users to easily read, write, update, and delete live data - just like working with SQL server.

PowerShell Scripting to Replicate Google Sheets Data to MySQL



Write a simple PowerShell script to replicate Google Sheets data to a MySQL database.

The CData Cmdlets for Google Sheets offer live access to Google Sheets data from within PowerShell. Using PowerShell scripts, you can easily automate regular tasks like data replication. This article will walk through using the CData Cmdlets for Google Sheets and the CData Cmdlets for MySQL in PowerShell to replicate Google Sheets data to a MySQL database.

After obtaining the needed connection properties, accessing Google Sheets data in PowerShell and preparing for replication consists of four basic steps.

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.

Collecting Google Sheets Data

  1. Install the module:

    Install-Module GoogleSheetsCmdlets
  2. Connect to Google Sheets:

    $googlesheets = Connect-GSheets -Spreadsheet $Spreadsheet
  3. Retrieve the data from a specific resource:

    $data = Select-GSheets -Connection $googlesheets -Table "Orders"

    You can also use the Invoke-GSheets cmdlet to execute pure SQL-92 statements:

    $data = Invoke-GSheets -Connection $googlesheets -Query 'SELECT * FROM Orders WHERE ShipCity = @ShipCity' -Params @{'@ShipCity'='Madrid'}
  4. Save a list of the column names from the returned data.

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name

Inserting Google Sheets Data into the MySQL Database

With the data and column names collected, you are ready to replicate the data into a MySQL database.

  1. Install the module:

    Install-Module MySQLCmdlets
  2. Connect to MySQL, using the server address and port of the MySQL server, valid user credentials, and a specific database with the table in which the data will be replicated:

    $mysql = Connect-MySQL -User $User -Password $Password -Database $Database -Server $Server -Port $Port
  3. Loop through the Google Sheets data, store the values, and use the Add-MySQL cmdlet to insert the data into the MySQL database, one row at a time. In this example, the table will need to have the same name as the Google Sheets resource (Orders) and to exist in the database.

    $data | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "Orders" -Columns $columns -Values $values }

You have now replicated your Google Sheets data to a MySQL database. This gives you freedom to work with Google Sheets data in the same way that you work with other MySQL tables, whether that is performing analytics, building reports, or other business functions.

Notes

  • Once you have connected to Google Sheets and MySQL in PowerShell, you can pipe command results to perform the replication in a single line:

    Select-GSheets -Connection $googlesheets -Table "Orders" | % { $row = $_ $values = @() $columns | % { $col = $_ $values += $row.$($col) } Add-MySQL -Connection $mysql -Table "Orders" -Columns $columns -Values $values }
  • If you wish to replicate the Google Sheets data to another database using another PowerShell module, you will want to exclude the Columns, Connection, and Table columns from the data returned by the Select-GSheets cmdlet since those columns are used to help pipe data from one CData cmdlet to another:

    $columns = ($data | Get-Member -MemberType NoteProperty | Select-Object -Property Name).Name | ? {$_ -NotIn @('Columns','Connection','Table')}