Get or Read Data from Google Sheet in MIT App Inventor

Hello friends and welcome to Obsidian Soft,
In today’s MIT App Inventor tutorial, I will teach you a simple and easy way of reading data from a Google sheet and showing it in an MIT App Inventor App.


Create a new project in App Inventor and name it ReadStudentData.

Make align horizontal center for screen1.

Now, add Listview from the user interface. Make the width 90% and the text color yellow.

Add a Button from the user interface. Rename to loadDataBtn. Make the background color yellow. Font bold, font size 20, shape rounded and text color black.

Add a Web component from connectivity.

Now, either create a Google Sheet if you don’t have one already by clicking on the plus button and writing the data.

https://docs.google.com/spreadsheets/u/0/

I am going to use the same sheet that we used in a previous tutorial for sending data to a Google sheet from MIT App Inventor.

Click on the blue share button.


A dialog will open up. Choose “General access” and in the options, choose “Anyone with the link”.

Now, press copy link button and press the done button to close this dialog.


Paste the link in some text file. It will look something like this:

https://docs.google.com/spreadsheets/d/1tLlr0ps7-gxNV1-NCTMS4fYvynvUPu8Cxkz8/edit?usp=sharing

Replace edit?usp=sharing by export?format=cs

Make sure that it is exactly this: export?format=csv

Now copy this entire changed link (don’t use below link and use the one from your own google sheet):

https://docs.google.com/spreadsheets/d/1tLlr0ps7-gxNV1-NCTMS4fYvynvUPu8Cxkz8/export?format=csv

Go back to your app inventor project and its designer view, and choose web1 component in your project. In its properties, paste the entire link with the updated export part in its Url property.


Now, go to the blocks section.

Make a global variable.


Get the button click event for loadDataBtn and call the web component’s Get procedure.


Now, this procedure Get will trigger the GotText event of Web1 component. So get that event from Web1.

First of all set the sheetData variable to list from csv table block from the lists section: the text coming in from our google sheet is the responseContent.

Remove the first row from this sheetData as it will contain headings: timestamp, name, and age and we don’t want that to show. You don’t need to do this if your sheet doesn’t have column headings.

Next, make a local variable and assign it to an empty list. Now, we will go through our sheetdata which is actually in rows and columns form (table form) using a for loop from control. Each item in this sheetData is actually a row from our actual Google sheet so it has 3 pieces of info: timestamp, name, and age. Hence, it is actually a list too. So, for each item, we are going to get the value inside it for index/column 2 (name) and index/column 3 (age) and join them together and add it as an item to our local list: datatobeshown.

Once we are done, we will set our listview elements to our local list.


Think about why I am using a local variable and not a global variable for dataToBeShown list. I would love to hear your answers in the comments.
I hope you liked this tutorial and found this method of reading data from a Google Sheet super easy.

You can have a look at the video tutorial here:

Please like my videos and share it with your friends and family. Also, subscribe to my channel and press the bell icon so you don't miss any of the great projects I have planned for you.


Please like my Facebook page and Instagram page for more educational resources and tips.

Pinterest:
https://www.pinterest.co.uk/obsidiansoft/_created/
For links to free educational apps, have a look at the educational apps page

Comments

  1. it has a error
    get responsecontent has an error

    ReplyDelete
    Replies
    1. Use a notifier to show alert with responseContent as input. Do this as the first thing in Web1.GotText event.

      Delete

Post a Comment

Popular posts from this blog

Jolly Phonics Group 1

Jolly Phonics Group 2

Jolly Phonics Flash Cards