Use gviz to get and query google sheet data
gviz is an abbreviation for Google Visualization API which is used to access google sheets and google charts, among other things. In this tutorial we will focus on getting and querying data on google sheets, to work with or display this data in App Inventor apps.
If our spreadsheet is set with "anyone with the link" permissions, gviz can be used directly on the google sheet. If our google sheet permission is set to private, then we would need to use a google apps script web app, created by the owner of the google sheet, in order to use gviz. (This approach is covered in another howto or this one)
Any queries use the Google Query Language, which is very similar to SQL, so if we know how to query with SQL, we are most of the way there. However, there are differences, it is therefore best to refer to the Google Query Language Reference
There are several different methods for constructing query urls using gviz, here we will use a syntax which is closely aligned to other url building methods for google sheets that we see in App inventor, for consistency.
The spreadsheet we will use has a set of mock data
The spreadsheet is set to "Anyone with the link", which allows direct access to the data
The demo app will use a web component and a webviewer (more later) along with buttons and labels for display
The app requires the user to type in the query
Here is a screenshot of the spreadsheet. We have here highlighted the spreadsheet ID, the grid ID (gid) and the grid (sheet) name. The grid we will be working with is called "myData", and to begin with this grid is positioned as the first grid on the spreadsheet (first from left)
The basic syntax used for a gviz query is as follows:
the link to the sheet
plus the spreadsheet ID
plus the gviz syntax
plus the query (here an example. if left blank, all the data is returned)
Put together we get:
If you run this url in your computer browser, it will probably download a file called json.js, or it may display a json in your browser window. This is because the default output for gviz is JSON. Here is an example of some of the output:
Not easy to read, or parse. Fortunately, we can enhance our url, and adapt the syntax to output the data in csv format, using tqx:
Put together this now reads:
and in your computer browser will return either a file named data.csv or print to screen:
Much more accessible. We will use tqx=out:csv when using the web component. App Inventor will not try to download a file, it will capture the data as text, and return it to the responseContent in the Web1.GotText event block. We can then work with the data as a list.
In the example above, we have applied the same basic url with csv output. The query returns just the first three columns and the first 10 rows. Note the headers are included. The gviz query will reference the first grid(sheet) from the left in the spreadsheet. This is currently "myData" - where out data is. We have kept the label output to just responseContent for display purposes, we would usually convert to a list from csv table, and set this to a variable.
What happens then if the grid (sheet) is not first from the left? We have a choice. we can either use the grid(sheet) name, e.g. "myData", or we can use the "gid", e.g. "1820138425".
Here you can see we have moved the grid "myData" to be the 4th grid on the spreadsheet.
and below are the two methods available to access the data.
using the grid/sheet name:
2. Using the gid:
We can also work on just a range of the dataset if we wish. We simply identify the range in A1 notation, and then the query will only reference that data. of course, if we leave the query blank, just that range will be returned. In the example below we select the sheet "myData" and the range A1:D10.
We mentioned the webviewer earlier. Another option for tqx is to output to html. This is useful if we only want to display the data in our app as a simple table. Our tqx syntax requires a simple change:
and we call the url with the webviewer goto block, instead of using the web component
Note: if the headers are not returned in the header row, add... &headers=1 ...to the url