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

fernando1987

Active Member
Licensed User

Version 9.6 Now Available!​


We're constantly improving! The new Version 9.6 of GsheetsPlus introduces two powerful methods in the GDrive class to make working with Google Drive files and folders even easier.




What’s New in This Version (GsheetsPlus only):​


FindFolderIDByName(FolderName As String)
Easily retrieve the ID of a Google Drive folder by its name.


FindFileIDByName(FileName As String)
Quickly locate the ID of any file by its name.


Both methods handle access tokens automatically and trigger custom events with the results, making them perfect for asynchronous integrations.

Example: Uploading PDF Files from Local Folders to Google Drive with Automatic Folder Handling for B4A:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim gd As GDrive
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    
    gd.Initialize(Me,"gd","xxxxxxxxxxxxxxxx.apps.googleusercontent.com")
    
End Sub


Sub UploadPdf
    Dim n As Int = 0
    ProgressDialogShow2("Uploading PDF files", False)

    ' Find the root folder ID in Google Drive
    gd.FindFolderIDByName("myfolder")
    Wait For gd_FindFolderIDByName(success As Boolean, FolderIdRoot As String)
    If success = False Then
        Log("Root folder not found")
        Return
    Else
        ' Local root folder path
        Dim localRootFolder As String = File.Combine(File.DirRootExternal, "myfolder")
        If File.IsDirectory(localRootFolder, "") = False Then
            Log("Local folder does not exist: " & localRootFolder)
            Return
        End If

        ' List subfolders in the local root folder
        Dim content As List = File.ListFiles(localRootFolder)
        For Each item As String In content
            Dim fullSubPath As String = File.Combine(localRootFolder, item)
            If File.IsDirectory(fullSubPath, "") Then
                Log("Processing folder: " & item)

                ' Try to find the corresponding folder in Google Drive
                gd.FindFolderIDByName(item)
                Wait For gd_FindFolderIDByName(success As Boolean, Id As String)
                If success Then
                    ' Upload files to the existing Drive folder
                    Dim localFolder As String = File.Combine(File.DirRootExternal, "myfolder/" & item)
                    If File.IsDirectory(localFolder, "") = False Then
                        Log("Local folder not found: " & localFolder)
                        Return
                    End If

                    Dim files As List = File.ListFiles(localFolder)
                    For Each fileName As String In files
                        n = n + 1
                        ProgressDialogShow2("Uploading PDF files " & n & "/" & files.Size, False)
                        gd.UploadFile(fileName, localFolder, fileName, Id, "")
                        Wait For gd_FileUploaded(Success As Boolean, FileID As String)
                        If Success Then
                            File.Delete(localFolder, fileName)
                            Sleep(2000)
                        End If
                    Next
                Else
                    ' If folder doesn't exist in Drive, create it
                    gd.CreateFolder(item, FolderIdRoot)
                    Wait For gd_FolderCreated_success(FolderName As String, Id As String)

                    Dim localFolder As String = File.Combine(File.DirRootExternal, "myfolder/" & FolderName)
                    If File.IsDirectory(localFolder, "") = False Then
                        Log("Local folder not found: " & localFolder)
                        Return
                    End If

                    Dim files As List = File.ListFiles(localFolder)
                    For Each fileName As String In files
                        n = n + 1
                        ProgressDialogShow2("Uploading PDF files " & n & "/" & files.Size, False)
                        gd.UploadFile(fileName, localFolder, fileName, Id, "")
                        Wait For gd_FileUploaded(Success As Boolean, FileID As String)
                        If Success Then
                            File.Delete(localFolder, fileName)
                            Sleep(2000)
                        End If
                    Next
                End If
            End If
        Next
    End If

    ' Clean up: remove empty folders
    DeleteEmptyFolders(File.DirRootExternal & "/myfolder")
    ProgressDialogHide
End Sub

' Recursively delete empty folders
Sub DeleteEmptyFolders(Folder As String)
    Dim files As List = File.ListFiles(Folder)
    For Each name As String In files
        Dim fullPath As String = File.Combine(Folder, name)
        If File.IsDirectory(Folder, name) Then
            ' Recurse into subfolder
            DeleteEmptyFolders(fullPath)
            ' Check again after deleting subfolders
            Dim innerFiles As List = File.ListFiles(fullPath)
            If innerFiles.IsInitialized And innerFiles.Size = 0 Then
                File.Delete(Folder, name)
                Log("Folder deleted: " & fullPath)
            End If
        End If
    Next
End Sub




How to Get the Update:​


If you’ve previously purchased GsheetsPlus, just:


  1. Log in to your user panel at b4xapp.com
  2. Go to the "Recent Orders" section
  3. Download the latest version at no additional cost

It’s that simple!
 

fernando1987

Active Member
Licensed User

Version 9.7 Now Available!​

Hello everyone


A new version of the GSheet Library is now available!
This update fully integrates with Google’s new authentication system using AuthorizationClient.




Main changes​


AuthorizationClient Integration


  • The entire internal code has been updated to use the modern Google authentication flow.
  • The Client ID (e.g., 1xxxxxxx-xxxx.apps.googleusercontent.com) is no longer required.
  • Your app only needs to be registered in Google Cloud Console with matching package name and SHA1 (on Android).

Retrieve account information
You can now easily access:


  • User email address
  • Display name
  • Profile photo

Logout and account switching


  • Full Sign-out support has been added, allowing users to switch accounts seamlessly.

Simplified Initialize method


  • No need for credentials or client IDs.
  • Just ensure your app is properly registered in Google Cloud Console.



Manifest update​


If you are updating from an older version, replace this section in your manifest:

B4X:
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>
)

with:

B4X:
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)




Simplified example – Get user info​

B4X:
Sub Process_Globals
    Private s As GSheets
End Sub

Sub Activity_Create(FirstTime As Boolean)
    s.Initialize(Me, "gs")
End Sub

Sub btnConnect_Click
    s.Connect
End Sub

Sub gs_Connect (Success As Boolean)
    If Success Then
        s.GetDriveUserInfo
        Wait For gs_AccountInfoAvailable (Success As Boolean, AccountInfo As UserInformation)
        If Success Then
            Log($"Name: ${AccountInfo.displayName}"$)
            Log($"Email: ${AccountInfo.emailAddress}"$)
            Log($"Photo: ${AccountInfo.photoLink}"$)
        End If
    End If
End Sub

Sub btnLogout_Click
    s.GoogleAndroidAuthorization.SignOut
    Wait For gs_SignOut (Success As Boolean)
    If Success Then Log("User signed out successfully")
End Sub


Coming soon​


Updated examples will be released soon showing how to:


  • Sign in using AuthorizationClient
  • Retrieve user information
  • Read and write data from Google Sheets



Thanks for supporting the project and helping the B4X community grow!
 

fernando1987

Active Member
Licensed User
[UPDATE - GSheet Library (B4A Example)]


The B4A example project has been updated in the main thread GSheet Library - Integrate Google Sheets into your B4X apps easily


Important:
To compile and run the project successfully, you must change the package name to one that is registered in your Google Cloud Console, or create a new project and register that package name.
This ensures that the package name matches the SHA1 signature configured in your Google Cloud credentials, allowing proper authentication with the Google Sheets API.


Google Sheet used by the example:
Example Sheet


Additional Library Used:
This example also makes use of the B4X XUI WobbleMenu - A Cross-Platform Animated Bottom Navigation library to provide a modern and animated bottom navigation bar.








 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…