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");
}
}
Thank you for this toturial
ReplyDeleteit's a very useful toturial ..
I have an issue to display a specific value from a cell in google spreadsheets. I am tried to make as you done here but the value that shawn in textbox Like this
{
SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1xRs9GO4RJlSGoIzXpZ-_--Uv_sjthonsyGlFPPM0LLs/edit#gid=1647294854");
}
and this is the app script:
function doGet(e) {
return ManageSheet(e);
}
function doPost(e) {
return ManageSheet(e);
}
function ManageSheet(e) {
//READ SINGLE RECORD
if ( e.parameter.func == "ReadRecord") {
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1xRs9GO4RJlSGoIzXpZ-_--Uv_sjthonsyGlFPPM0LLs/edit#gid=1647294854");
var sh = ss.getSheets()[1];
var rg = sh.getDataRange().getValues();
var outString = '';
outString += rg[parseInt(e.parameter.id)].join(',');
//return ContentService.createTextOutput(outString).setMimeType(ContentService.MimeType.TEXT);
}
}
I have an issue to display a specific value from a cell in google spreadsheets. I am tried to make as you done here but the value that shawn in textbox Like this
Delete{
https://drive.google.com/file/d/1EM-ZKGCmvgEUSh4vx5R9noLPIXh4RAEI/view?usp=sharing
sorry the line 5:6 of my comment wrong paste.
thank you..
and the following link from app inventor
ReplyDeletehttps://drive.google.com/file/d/1H1IJGP8K_o0RtTp6Ei9CPe-zg6lwVw7A/view?usp=sharing
https://drive.google.com/file/d/1vNUCZX1Enaz7BtrFqGPveo3mkUXMCX0l/view?usp=sharing
ReplyDeleteSee there the link.Im getting this error.Code is not working.please support
Teşekkürler
ReplyDeleteне работает ваш скрипт
ReplyDeletehello, in the add one record part why is it that the thing that get put in my textbox is only the title from the spreaADSHEET and not the actual records
ReplyDelete