Post Data to a Google Sheet

[updated Oct 2021]

Thought I would write this one up on here, can’t take any credit for it, as original idea posted on youtube (credits below), but if I have it here it is easier to grab and use when I need it.

I have previously worked up a solution using a Google Form to submit data to a google sheet from an AppInventor app, but this method uses a standalone google apps script to do the same, but without the baggage of the google form. We will need to:

  • Have a google account (we want to use)

  • Create a spreadsheet, and set this out with the columns we need

  • Create a google apps script

  • Create an App Inventor app with the correct blocks

Your google account will be the owner of the google sheet and the google apps script, this overcomes any permissions when posting data

Open your google drive and create a new spreadsheet, given it a memorable name, add the columns you will need for your data with headers for each. In my example the columns are on Sheet1, the headers are id and name. My screenshot already has some test data submissions added.

Copy the File ID for the google sheet, you will need this for the next stage

Back in your google drive, create a new Google Apps Script, and paste in this code, overwriting the default myfunction() {}. Make sure you now paste in the sheet file ID you copied earlier to the two places where it says: FILE ID HERE. Adjust your sheet and column header parameters if needed. This is where the mistakes creep in, so take care.

function doPost(e) {

var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/<FILE ID HERE>/edit#gid=0");

var sheet = ss.getSheetByName("Sheet1");

var id = e.parameter.id ;

var name = e.parameter.name ;

sheet.appendRow([id,name]);

}


Now save your work, give permissions etc. and then publish this as a web app.

Grab the url of your web app, you will need this for the next stage:

https://script.google.com/macros/s/AKfyJMzcnZgexctQgIcyz_ST8ZdWmQB3l55qM/exec

(this is a fake url)

Open up AppInventor, create a new project, drag out a couple of textboxes (or however many you need to match the columns in your google sheet (I have only two). Also drag out a Web component from Connectivity.

Now create the blocks you need to make the Submit button work

Test the app, check the google sheet. Hey presto!

(If it doesn’t work, you may need to open up the permissions on your google sheet to “Anyone with the link”)

For adding further columns in spreadsheet and script, ensure blocks are updated and the script is republished

Credits go to Android Labs for the original idea