B4J Question [BANano] Load csv file

angel_

Well-Known Member
Licensed User
Longtime User
I am trying to load a csv file into a two dimensional array.

- Is it possible to use jStringUtils to load the csv?
- Does BANano support two-dimensional arrays?
 
Solution
Is it possible to use jStringUtils to load the csv?
No, this is not a BANano library
Does BANano support two-dimensional arrays?
JavaScript does not support two-dimensional arrays, hence so doesn't BANano

You can however use an external JavaScript library like PapaParse in BANano. Javascript file for this lib can be found here: https://github.com/mholt/PapaParse/blob/master/papaparse.min.js and 'fake' a 2-dimensional Array.

B4X:
Sub Process_Globals
    Private BANano As BANano 'ignore
    ' make a variable to hold the Papa javascript object
    Private PapaParse As BANanoObject
End Sub

Sub AppStart (Form1 As Form, Args() As String)    
    ' The name of your library. Strongly suggested to let it begin with...

alwaysbusy

Expert
Licensed User
Longtime User
Is it possible to use jStringUtils to load the csv?
No, this is not a BANano library
Does BANano support two-dimensional arrays?
JavaScript does not support two-dimensional arrays, hence so doesn't BANano

You can however use an external JavaScript library like PapaParse in BANano. Javascript file for this lib can be found here: https://github.com/mholt/PapaParse/blob/master/papaparse.min.js and 'fake' a 2-dimensional Array.

B4X:
Sub Process_Globals
    Private BANano As BANano 'ignore
    ' make a variable to hold the Papa javascript object
    Private PapaParse As BANanoObject
End Sub

Sub AppStart (Form1 As Form, Args() As String)    
    ' The name of your library. Strongly suggested to let it begin with BANano!
    BANano.Initialize("BANano", "BANanoCSV", DateTime.Now)
    
    ' add the papaparse.min.js file.  Must be in the /Files folder of your project  (do not forget to sync in the Files tab!)  
    BANano.Header.AddJavascriptFile("papaparse.min.js")
            
    ' start the build
    BANano.Build(File.DirApp)
End Sub

Sub BANano_Ready()
   ' assign the JavaScript Papa variable to our B4J PapaParse variable
    PapaParse.Initialize("Papa")
    
    ' CSV as a string example
    Dim SB As StringBuilder
    SB.Initialize
    SB.Append($"col1a",15"$ & CRLF)
    SB.Append($"col1b",20"$ & CRLF)
    SB.Append($"col1c",30"$)    
    Dim MyCSV As String = SB.ToString
    
    ' or for example from a file on your server
    Dim MyCSV As String = BANano.Await(BANano.GetFileAsText("http://gorgeousapps.com/sample.csv", Null, "UTF-8"))

   ' set some options of the PapaParse library
    Dim Options As BANanoObject
    Options.Initialize5
    Options.SetField("dynamicTyping", True)
    Options.SetField("skipEmptyLines", True)
    
   ' run the parse method on the PapaParse library
    Dim result As BANanoObject
    result = PapaParse.RunMethod("parse", Array(MyCSV, Options)).Result
    ' returns a list of lists
    Dim MyArray As List = result.GetField("data")
    
    Log(Get(MyArray, 0,0)) ' result is MyArray(0,0)
End Sub

' helper method to get a value from the list of lists object
Sub Get(Arr As List, indx1 As Long, indx2 As Long) As Object
    Return Arr.Get(indx1).As(List).Get(indx2)
End Sub

Alwaysbusy
 
Upvote 0
Solution
Cookies are required to use this site. You must accept them to continue using the site. Learn more…