B4A Library GSheet Library (integrate google sheets into your B4X apps easily)

Gsheet

Author:
Fernando Arevalo
Version: 1.0

Functions:
  • Process_Globals As String
B4A, B4I
  • Initialize (ClientId As String)
B4J
  • Initialize(ClientId As String,ClientSecret As String)
B4X
  • SpreadsheetsId As String
    The ID of your document. This is the big long aplha-numeric code in the middle of the document's URL.
  • Sheets_name As String
    Name of the sheet we are going to work on
  • Sub delete(id As Int)
    id: row number to delete
  • Updatesheet(id As String, l As List)
    id: row number to Update and l: list of new items
  • UpdateSheet_range(range As String, value As String)
    range: position of the value to be updated example: "A2". value: new value
  • insert_list(l As List)
    l: list of new items, these items will be added in the next empty row( if a list is created with the total or partial elements of a database it will be added as a single query immediately to your sheet)
Price: 15 USD
Payment method: PayPal
Email paypal: hazlofacil87@gmail.com

Gsheetplus

Author:
Fernando Arevalo
Version: 1.0

Functions:
  • Process_Globals As String
B4A, B4I
  • Initialize (ClientId As String)
B4J
  • Initialize(ClientId As String,ClientSecret As String)
B4X
  • SpreadsheetsId As String
    The ID of your document. This is the big long aplha-numeric code in the middle of the document's URL.
  • Sheets_name As String
    Name of the sheet we are going to work on
  • Sub delete(id As Int)
    id: row number to delete
  • Updatesheet(id As String, l As List)
    id: row number to Update and l: list of new items
  • UpdateSheet_range(range As String, value As String)
    range: position of the value to be updated example: "A2". value: new value
  • insert_list(l As List)
    l: list of new items, these items will be added in the next empty row( if a list is created with the total or partial elements of a database it will be added as a single query immediately to your sheet)
  • copytodrive(title As String,sheetcopy As String)
    creates a copy in the drive of the account with which you enter a selected sheet you must define the name of the new spreadsheet (title) and the name of the sheet to copy (sheetcopy)
  • createtitle( l As List, sheetsname As String, fontsize As Int,bold As Boolean)
    l: list of titles, sheetsname: name of the sheet where the titles will be inserted
  • create_spreadsheets(title1 As String)
    title: name of the new spreadsheet
  • create_sheets(name_sheets As String,SpreadsheetId As String)
    name_sheets: name of the new sheet, SpreadsheetId:The ID of your document
  • deletesheet(sheetsname As String )
    name_sheets: name of the sheet for delete into spreadsheet
  • newspreadsheetsid As String
    allows to retrieve the id after creating a new spreadsheet or after backing up one in drive to save it in a database or variable it is recommended to put a wait of 2s before calling the value of this variable
  • newnamesheet As String
    allows you to retrieve the name of the new sheet created to save it in a database or variable it is recommended to wait for 2s before calling the value of this variable

Price: 25 USD
Payment method: PayPal
Email paypal: hazlofacil87@gmail.com

NOTE:

This library is not free, because, it took a lot of time and gray hair to create all the methods and learn how the api works.
Please write GSheets or GSheetplus in the order description, thanks.

Paypal: hazlofacil87@gmail.com

Thanks for your understanding. :)

This library depends the Google OAuth2 class:
follow the tutorial in this topic to create your project in google console developer, enable google dive and google sheets apis, make sure to add your package name to your project, and create an oauth screen.

Add this in your manifest:

manifest:
AddActivityText(Main,
  <intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="$PACKAGE$" />
  </intent-filter>
   )

Read example for v4 api:
Read sheets:
Sub GetSheet
table1.ClearAll
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://sheets.googleapis.com/v4/spreadsheets/17YACjfqxKHDCXM_N7Jnq9BC_KzzkEgXLKzHDwaIM5Zs/values/Example!A2:Z?key=xxxxxxxxxxxxxxxxxxxxxxxxx")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim parser As JSONParser
        parser.Initialize(j.GetString.Trim)
        Dim root As Map = parser.NextObject
        'Dim majorDimension As String = root.Get("majorDimension")
        Dim values As List = root.Get("values")
 
        For Each colvalues As List In values
           
  Dim id1 As Int = colvalues.Get("4")
            Dim create As String = colvalues.Get("3")
            Dim Telephone1 As Int = colvalues.Get("0")
            Dim Age1 As Int = colvalues.Get("2")
            Dim Name1 As String = colvalues.Get("1")
            If colvalues.Size > 0 Then
             table1.AddRow(Array As String(id1,Telephone1,Name1,Age1,create))
            End If
     
        Next
        Dim range As String = root.Get("range")
        Log(range)
    End If

 

    j.Release
End Sub

Read example with Gsheets2Json API(TOTALMENTE GRATUITO EN https://gsheets2json.xyz/ ):
ReadGsheet Gsheets2Json API::
Sub GetSheet
table1.ClearAll
 
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://gsheets2json.xyz/api?api_key=XXXXXXXXXXXXXXXXXXX&id=1FHx-a_a4afTQ3L5hXJIOttCVaJNngO3113E-qqFOjbk&columns=false&sheet=Example")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success = True Then
        Dim s1 As String = j.GetString.Trim
        Dim m As Map = s1.As(JSON).ToMap
        Dim rows As List = m.Get("rows")
        For Each colrows As Map In rows
            Dim id1 As Int = colrows.Get("1")
            Dim create As String = colrows.Get("created")
            Dim Telephone1 As Int = colrows.Get("Téléphone")
            Dim Age1 As Int = colrows.Get("Age")
            Dim Name1 As String = colrows.Get("Name")
            If colrows.Size > 0 Then
                table1.AddRow(Array As String(id1,Telephone1,Name1,Age1,create))
             
            End If
        Next
    Else
     
    End If
 
 


 
 
End Sub

Example connect:

Example connect:
#Region  Activity Attributes
    #FullScreen: false
    #IncludeTitle: FALSE
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim s As GSheets
    Private conect1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    s.Initialize("xxxxxxxxxxxxxxxx.apps.googleusercontent.com")'your Clientid google developers console, activate google drive api and google sheets api
    s.Sheets_name = "Example"
    s.SpreadsheetsId = "1FHx-a_a4afTQ3L5hXJIOttCVaJNngO3113E-qqFOjbk"
    Activity.LoadLayout("sheets1")
   
End Sub

'Connect
Sub connect1_Click
    s.connect
End Sub

Sub Activity_Resume

s.oauth2.CallFromResume(Activity.GetStartingIntent)
   
End Sub


Advantages:
  • It is not linked to paying an external api
  • the google sheet api is totally free
  • You can integrate multiple apps into a single project in the google console
  • Easy to use and integrate
 
Last edited:
Google Sheet to db

Gsheets to db:
Sub GetSheet_to_db
 
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://gsheets2json.xyz/api?api_key=XXXXXXXXXXXXXXXXXXX&id=1FHx-a_a4afTQ3L5hXJIOttCVaJNngO3113E-qqFOjbk&columns=false&sheet=Example")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success = True Then
        Dim s1 As String = j.GetString.Trim
        Dim m As Map = s1.As(JSON).ToMap
        Dim rows As List = m.Get("rows")
        For Each colrows As Map In rows
            Dim id1 As Int = colrows.Get("1")
            Dim create As String = colrows.Get("created")
            Dim Telephone1 As Int = colrows.Get("Téléphone")
            Dim Age1 As Int = colrows.Get("Age")
            Dim Name1 As String = colrows.Get("Name")
            If colrows.Size > 0 Then
            Private maps As List
            Private mp As Map
            maps.Initialize
            mp.Initialize
            mp.put("id",id1)
            mp.put("telephone",telephone1)
            mp.put("name",Name1)
            mp.put("age",Age1)
            mp.put("create",create)         
              maps.Add(mp)
              DBUtils.InsertMaps(Main.SQL,"dbname",maps)
          
            End If
        Next
    Else
  
    End If

End Sub
 
Last edited:
New feature added in GSheetsplus.

GSheetsplus
Version:
1.10

Functions:
  • DBtoSpreadsheet(DBname As String, Tablename As String, titlesheet As List, columnsdb As List)
    Convert a sqlitle base into a google sheet
Example:
DB SQLITLE TO GOOGLE SHEETS:
Private Sub Button2_Click

 Dim l2 As List
    l2.Initialize
    l2.Clear
    l2.Add(Array As String("telephone","name","Age","Create","=ARRAYFORMULA(SI(A1:A<>"""";FILA(A1:A);""""))")) 'Place this Formula at the end so that you can retrieve the row number
 
    Dim c As Cursor
    c = Starter.SQL1.ExecQuery("SELECT* FROM sheets")
   
   
    Dim l1 As List
    l1.Initialize
    l1.Clear
    If c.RowCount>0 Then
       
        For i=0 To c.RowCount-1
            c.Position=i
       
l1.Add(Array As String(c.GetString("telephone"), _
c.GetString("name"), _
c.GetString("age"), _
c.GetString("date")))  
   
 
        Next

    end  If
    c.Close
    Try
       
s.DBtoSpreadsheet("My DB","Users",l2,l1)

    Catch
   
        End Try
        Sleep(5000)
        dim datatableid as string
    datatableid = s.newspreadsheetsid 'retrieves the id of the newly created spreadsheet so that you can store it a sleep(5000) is recommended and then retrieves it to store it and be able to work on the newly created spreadsheet
ToastMessageShow(datatableid,True)  
    loadtable
End Sub
 
Last edited:
Top