This video series shows how to use Google Sheet as a database for an Android application made with App Inventor 2.
Read All data, Delete Record #1
In this video we focus on how to read all the data present in a google sheet and assign it to a ListView. And see how to delete a record (row) from Google sheet from the App inventor interface.
Create New Record #4
function doGet(e) {
return ManageSheet(e);
}
function doPost(e) {
return ManageSheet(e);
}
function ManageSheet(e) {
//READ ALL RECORDS
if ( e.parameter.func == "ReadAll") {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheets()[0];
var rg = sh.getDataRange().getValues();
var outString = '';
for(var row=0 ; row<rg.length ; ++row){
outString += rg[row].join(',') + '\n';
}
return ContentService.createTextOutput(outString).setMimeType(ContentService.MimeType.TEXT);
}
//DELETE SINGLE RECORD
else if (e.parameter.func == "Delete") {
var record = e.parameter.id;
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheets()[0];
sh.deleteRow(parseInt(record) + 1);
return ContentService.createTextOutput("Success");
}
//READ SINGLE RECORD
else if ( e.parameter.func == "ReadRecord") {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheets()[0];
var rg = sh.getDataRange().getValues();
var outString = '';
outString += rg[parseInt(e.parameter.id)].join(',');
return ContentService.createTextOutput(outString).setMimeType(ContentService.MimeType.TEXT);
}
//UPDATE SINGLE RECORD
else if (e.parameter.func == "Update") {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheets()[0];
var data = [ [ e.parameter.first, e.parameter.last, e.parameter.gender, e.parameter.country, e.parameter.age ] ];
sh.getRange("B"+(parseInt(e.parameter.id)+1)+":F"+(parseInt(e.parameter.id)+1)).setValues(data);
return ContentService.createTextOutput("Success");
}
//CREATE NEW RECORD
if (e.parameter.func == "Create") {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheets()[0];
var data =[e.parameter.id, e.parameter.first, e.parameter.last, e.parameter.gender, e.parameter.country, e.parameter.age];
sh.appendRow(data);
return ContentService.createTextOutput("Success");
}
}