Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →How to pipe Google Sheets Data to CSV in PowerShell
Use standard PowerShell cmdlets to access Google Sheets tables.
The CData Cmdlets Module for Google Sheets is a standard PowerShell module offering straightforward integration with Google Sheets. Below, you will find examples of using our GoogleSheets Cmdlets with native PowerShell cmdlets.
Creating a Connection to Your Google Sheets Data
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.
$conn = Connect-GSheets -Spreadsheet "$Spreadsheet"
Selecting Data
Follow the steps below to retrieve data from the Orders table and pipe the result into to a CSV file:
Select-GoogleSheets -Connection $conn -Table Orders | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myOrdersData.csv -NoTypeInformation
You will notice that we piped the results from Select-GoogleSheets into a Select-Object cmdlet and excluded some properties before piping them into an Export-Csv cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each "row" in the result set, and we do not necessarily want that information in our CSV file.
The Connection, Table, and Columns are appended to the results in order to facilitate piping results from one of the CData Cmdlets directly into another one.Deleting Data
The following line deletes any records that match the criteria:
Select-GoogleSheets -Connection $conn -Table Orders -Where "ShipCity = Madrid" | Remove-GoogleSheets
Inserting and Updating Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Google Sheets, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MyOrdersUpdates.csv | %{ $record = Select-GoogleSheets -Connection $GoogleSheets -Table Orders -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-GoogleSheets -Connection $googlesheets -Table Orders -Columns ("Shipcountry","OrderPrice") -Values ($_.Shipcountry, $_.OrderPrice) -Where ("Id = `'"+$_.Id+"`'") }else{ Add-GoogleSheets -Connection $googlesheets -Table Orders -Columns ("Shipcountry","OrderPrice") -Values ($_.Shipcountry, $_.OrderPrice) } }
As always, our goal is to simplify the way you connect to data. With cmdlets users can install a data module, set the connection properties, and start building. Download Cmdlets and start working with your data in PowerShell today!