Saturday, March 26, 2022

App Inventor Google Sheets

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.


Read one Record #2
In this video we show how to read a single recording from Google Sheets using an Android interface

Update Record #3
This video shows how to update a single record in Google Sheets from an app made with App Inventor2.


Create New Record #4
This video shows how to create new record in Google Sheets from an app made with App Inventor2.



Code

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");

  }

}


7 comments:

  1. Thank you for this toturial
    it'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);

    }

    }

    ReplyDelete
    Replies
    1. 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
      {
      https://drive.google.com/file/d/1EM-ZKGCmvgEUSh4vx5R9noLPIXh4RAEI/view?usp=sharing

      sorry the line 5:6 of my comment wrong paste.
      thank you..

      Delete
  2. and the following link from app inventor

    https://drive.google.com/file/d/1H1IJGP8K_o0RtTp6Ei9CPe-zg6lwVw7A/view?usp=sharing

    ReplyDelete
  3. https://drive.google.com/file/d/1vNUCZX1Enaz7BtrFqGPveo3mkUXMCX0l/view?usp=sharing

    See there the link.Im getting this error.Code is not working.please support

    ReplyDelete
  4. не работает ваш скрипт

    ReplyDelete
  5. hello, 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