CRUD with Google Sheets, Web App and AI2

CRUD

That is create / read / update / delete to the uninitiated and the base requirement for the management of the data in a database.  There are many solutions on AI2 already that access remote data and database systems, and I have already explored some of the possibilities for connecting with Google Sheets, but never really put it all together. This effort aims to address that and to provide a single web app, built from google apps script as an interface between AI2 and a Google Sheet. This means you can keep your google sheet private yet provide access to the data via the mobile app.

Working this thing up will require some knowledge of google sheets, google apps script / javascript, and building apps in Appinventor 2.

I was partially driven to this by the lack of usable / functional  / (free) online data storage solutions that everyone can easily use, the upcoming demise of fusion tables and loss of firebase access from Ai2, and as said above, the need to pull various previous efforts together in one place.

We will start with the google sheet, then move on to the web app, and finally the AI2 app. Some rigour will be needed in order for all this to work, so I have set out some requirements and indicators along the way: 

Google Sheet

Here is my google sheet with the data, simply an id, name and phone number:

Nothing special so far, but let us take a closer look:

A couple of things going on here.

  1. In cell A1 we have an array formula that automatically fills column A. This enables us to follow the same list principle as in AI2, whereby if you delete a record all the indexes move by one from below. Leave this formula here, and do not enter any data directly into column A, otherwise the indexing will break.
  2. This array formula relies on there being content in every row of column B. Make sure this is so, or edit the formula where “B2:B” to a column that has entries.
  3. Keep your heading labels short, abbreviate if necessary, working to a max of 6 characters. this will help with layout and alignment in the mobile app. Also best to have no spaces.

Linked in with the array formula above, you will see that there are no empty rows at the end of the data listing. This is because the array formula “takes over” the entire column. If you programmatically add an entry with “appendRow()”, the entry will be added at the bottom of the sheet / out of view. Remove any empty rows after the last entry. Google adds a new row when there is a new entry.

You can manually add records to the spreadsheet directly, just remember you do not need to enter an id, this will happen automatically.

Web App

I created the script bound to the google sheet (makes it easier to find again), you can publish the script as a web app in just the same way as a standalone google apps script. If you prefer, everything will work in the same way if you create a standalone script, but you will need to call the google sheet ID instead of “getActive()”. A few things that will need bearing in mind:

  1. These scripts are dynamic so should work with any number of columns and rows in the google sheet.

  2. Remember to always re-publish your script if you make any changes.

  3. When publishing, you need to run the script as “you” but allow access to “Anyone, even anonymous“.

  4. The script includes a “lock” which should prevent concurrent changes.  You may need to enable additional API’s with Google for this to work. (Google should tell you what to do!)

The Script

 

So how does it all work ?

  1. We first create the doGet and do Post functions (I use both in case for some reason one doesn’t work).
  2. We then have a responseHandler function – what to do when we receive the http request
  3. The web app is activated when the url to the webapp is called with parameters. Each action available in the web app is summoned with the “func” parameter. There a six func parameters to choose from:
    1. CREATE – to create a new record
    2. READALL – to call all the records
    3. READRECORD (not used)
    4. READQUERY – to query (with SQL) a subset of records
    5. UPDATE – change a record
    6. DELETE – remove a record
  4. The last two activities make use of the “id”, hence its importance
  5. After each activity a response is returned, this is either data or a message. If a valid func request is not made, then an error message is returned.
  6. The job at the AI2 end is to build the url required by the web app. Here is an example:
    1. We want to create a new record
    2. We have our script url >  https://<scripturl>
    3. We have our first parameter : ?func=CREATE  (note, no quotes!)
    4. The AI2 app generates the remaining parameters:
      &name=Bob%20Simmons&phone=891%202340%207745
    5. We end up with:
      https://<scripturl>?func=CREATE&name=Bob%20Simmons&phone=891%202340%207745
    6. Note the app needs to html encode the parameter if there are spaces in the content with %20
    7. For testing this can be pasted into your PC browser address bar
    8. The CREATE part of the web app script does the rest:

AI2 App

Believe it or not, the above was the easy part, structuring the AI2 app to handle all of the above was much harder!

Some things we need to know or be aware of when creating the AI2 app:

  1. The app was developed on version n174 native MIT Ai2 framework, using Companion App 2.50, and a genyMotion Emulator.
  2. I used responsive sizing and the Device Default theme.
  3. The app was also tested on an HTC One 8 and Google Nexus 7 real devices.
  4. The example project is dynamic so can accept and work with a dataset of any shape and size (device memory willing!)
  5. For specific, unchanging needs, the blocks can be re-written for a set number of columns
  6. The app will always send an entire record with each header and field for creating and updating.  Always ensure that they are present in the url.
  7. The app only sends the record id for deleting
  8. The dataset is only ever temporarily stored on the app, the root/master dataset is on the google sheet (google sheets has an excellent version history too)

Screenshots of App

 

Blocks

 

Video

 

Summary

So there you have it, a CRUD method for google sheets from AI2 using a google web app. Admittedly, the google sheet is only a flat file database, but this is much more accessible than mySQL, and most people on the AI2 forums seem to use flat file of some sort for their work. This collection of tools can be used for just me, or shared with a wide community of users. 

I attach an apk of the app for trialling purposes, the google sheet is refreshed every night.

CRUD_GS_AI2_v1

The source code (aia file) and setup instructions are available following a donation via Paypal

Please leave any comments or suggestions you may have, or start / join a conversation on the AppInventor 2 Forum.

Let me know if I can be of any help with your googling or appinventing.

 

Leave a Reply

Your email address will not be published. Required fields are marked *