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!
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…