Sharperlight Query Builder is not only capable of delivering data from a traditional database, but also accessing data from file systems, documents and more – including API endpoints such as the Google Sheets API. Show This article will explore the use of Sharperlight Query Builder to retrieve data from a Google Sheets document via the ‘spreadsheets.values.get’ method of the Google Sheets API.
Pre-requisitesIn order to successfully access data via the Google Sheets API, your organisation must appropriately configure the Google Cloud Platform via the Developer Console. Once you have located the Google Sheets API Management page, click the ‘Enable’ button to allow the endpoint to be queried. Once you have done so, a green tick should indicate that it is successfully enabled. Security & AuthenticationThere are two acceptable forms of authenticating a Google Sheets API Request. How do I get the credentials I need?PlaceholderCredential TypeWhy do I need it?Where do I get it?API_KEYAPI KeyInclude an API Key to identify your project, used to verify enablement and track request quotas.Through the Google API Console. See “Setting up API Keys”.CLIENT_IDOAuth 2.0Use a Client ID in the OAuth flow to acquire an access token associated with your project on behalf of a user’s account.Through the Google API Console. See “Setting up OAuth 2.0”.ACCESS_TOKENOAuth 2.0Include an access (bearer) token to identify the user which completed the OAuth flow with your Client ID.CLIENT_ID is used to retrieve this token at runtime, generally done through an authentication library. SeeAs configuring and navigating user flow and consent with OAuth 2.0 is quite complex and out of scope for this document, we will explore the usage of an API key to allow read-access to a Google sheet. Due to API keys lacking any ‘user’ identification to assign a particular sheet to, Google Sheets must be enabled for open sharing via link in order to be accessed by an API key. Depending on the sensitivity of the data, this may be a security concern. Once you have retrieved your API Key and configured its scope and restrictions where necessary, copy it and keep it saved for later. Building an API request in Query BuilderOnce we have enabled the Sheets API and generated an API key we can then start building our API request. Sharperlight’s Query Builder is typically used to provide access to data from products via ‘Datamodels’. The datamodel, or Product that you are querying is selected in the Query Builder interface. The System product’s ‘tables’ represent the different query tools, administrative and diagnostic queries we can perform. For API requests, we are looking for the ‘table’ entitled “Web Page Table, CSV, JSON, XML or OData”. To query the API endpoint, we can build our request URL using the Google Sheets API specifications. We will use the ‘spreadsheet.values.get‘ method, as this method will allow us to read the data from our spreadsheet directly. The request syntax for this method is as follows:
The spreadsheetId, range, and apiKey values are all variables that will change depending on which Spreadsheet, tab (range) and API key we are using – so to make this query more reusable we can use “Prompt Only Filters”. First, let’s change our URL slightly to allow Query Builder to be able to replace our variables with the “Prompt Only Filter” values. Filter references in Sharperlight are similar to the ones used above, but are prefixed with an @ symbol, e.g. {@Filter_Name}.
Next, we will need to copy our URL into the Query Builder “URL Link” property. This will result in a warning that we have not yet created the filter variables (spreadsheetId, range and apiKey), so let’s handle that now. You can create additional ‘variables’ using Prompt Only Filters. Right click anywhere in the filters section (the top pane of Query Builder), and navigate the drop-down command list to find Other > Text. Add the name of the field you want to create (e.g. “spreadsheetId”) when prompted, and press OK. Lastly, drag the new filter to the top of the filters pane, above “URL Link”. Repeat this step with ‘range’ and ‘apiKey’ so that we have all three required variables, as below. Care should be taken to ensure the attribute names (highlighted in green above) match the required variable name from our URL. If they do not, this can be changed in the options menu, by right clicking on each filter value and selecting options from the dropdown menu. Now that we have our three variables set up, we need to input the values. You should already have the API Key saved from earlier – if not, return to the “Security and Authentication” section for details. Open the Google Sheet you would like to share. The “Spreadsheet ID” can be retrieved from the URL bar. This is the long string of characters following “/d/” To allow the API key access to your sheet, and click the green share button in the top-right corner. In the pop-up window, click “Change to anyone with the link”. Now that we have all the required details, we can input the values into Query Builder and make a request. We have successfully queried the API at this point – and some minor formatting is required to make the dataset readable. Once we have ‘expanded’ the values field and it is no longer nested, we can select our columns and optionally the line id, and preview the formatted output of the API request. And finally, we have our full dataset. This query can be used to drive output for any Sharperlight query, including within Publisher or Excel. When refreshed, it will request updated information from the Google Sheets API. There are additional options for formatting and adjusting the output of the API request – for more information refer to the article Querying JSON. Can I pull data from Google Sheets into Excel?You can convert Google Sheets files to Excel files by using the "Download" option. Google Sheets allows you to download your spreadsheets in a format that can be opened in Microsoft Excel.
How do I pull data from Google Sheets to a website?Use Google Apps Script to pull data from Google Spreadsheet to HTML. Step one: Prepare the Google Spreadsheet data. Make a copy of the Google Spreadsheet data here. ... . Step two: Create a new Google Apps Script project. ... . Step three: Setting up the project. ... . Step four: Add the code snippets. ... . Step five: Deployment.. |