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. Show
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: 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: ={"id";ArrayFormula(if(B2:B<>"";row(A2:A)-1;""))} A couple of things going on here.
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. 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:
So how does it all work ?
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:
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.AIA (my thanks to Danya Solncev for pointing out a problem with international characters) SCRIPT INSTRUCTIONS One user, Amit, asked for an addition to the script to change the content of a single known cell. We added this code to the script: //CHANGE SINGLE CELL }else if (e.parameter.func == "ChangeCell") { var ss = SpreadsheetApp.getActive(); var sh = ss.getSheets()[0]; sh.getRange(e.parameter.A1NotRange).setValue(e.parameter.cellValue); return ContentService.createTextOutput("Cell Changed").setMimeType(ContentService.MimeType.TEXT); ..... The two parameters: A1NotRange would be, for example: "F4" (column 6 row 4) cellValue would be, for example: "sometext" The url would look something like: https://<YOUR SCRIPT URL>/exec?func=ChangeCell&A1NotRange=F4&cellValue=sometext How do I create a Web input Form in Google Sheets?There's also a link to Google Forms in Docs, Sheets, and Slides: click File > New > Form to start a new blank form. Or, in Google Sheets, click Tools > Create a Form to start a blank new form that's automatically linked to that spreadsheet.
How do I create a Form in Google Sheets?Create a form from a Google spreadsheet:
Click the Tools drop-down menu, scroll to Form, and select Create a form. The form responses will be collected in the tab from which you create the form. 2. In the form template that opens, you can add any questions and options you'd like.
How do I create a data entry Form with Google HTML service and submit data to Google Sheets?Submit a HTML form to Google Sheets. Set up a Google Sheet. Go to Google Sheets and create a new sheet. ... . Create a Google App Script. Click on Extensions -> Apps Script . ... . Run the initialSetup function. You should see a modal asking for permissions. ... . Add a trigger for the script. ... . Publish the project. ... . Configure your HTML form.. |