B4J Question Solved - Tableview – copy data from tableview to clipboard

Nokia

Active Member
Licensed User
Longtime User
Looking for recommendation on how to copy data from tableview w/wo headers to clipboard so that user can paste into excel or some other editor.
 
Solution
Solved...

B4X:
Sub miScriptCopyGridData_Action
    
    mdlShared.CopyGridDataToClipbard(tblScript, TAB, True)

End Sub

Sub CopyGridDataToClipbard(Table As TableView, DelimiterChar As String, IncludeHeader As Boolean)
    
    'Data
    Dim lData As List
    lData.Initialize
    Dim DataForClipBoard As String
        
    For i = 0 To Table.Items.Size - 1 '--------------------
        Dim s As String
        Dim aRow() As Object = Table.Items.Get(i)
                
        For iI = 0 To aRow.Length - 1
            If aRow(iI) Is Label Then
                s = s  & GetlabelText(aRow(iI)).Text & DelimiterChar
            Else
                s = s & aRow(iI) & DelimiterChar
            End If
        Next       
        lData.Add(s)...

MegatenFreak

Active Member
Licensed User
You can extract the contents of the TableView in an organized List variable as follows:

B4X:
    Dim TV As TableView
    Dim RowData As List
  
    RowData.Initialize
    For i = 0 To TV.Items.Size - 1
        Dim Row() As Object = TV.Items.Get(i)
        Dim this_row(Row.Length) As String
        For k = 0 To Row.Length - 1
            'If the table items are just strings:
            this_row(k) = Row(k).As(String)
            'If each cell is defined as a kind of node:
            If Row(k) Is Label Then
                this_row(k) = Row(k).As(Label).Text
            Else If Row(k) Is TextField Then
                this_row(k) = Row(k).As(TextField).Text
            'And so on
            End If
        Next
        RowData.Add(this_row)
    Next

Headers can be extracted from TV.GetColumnHeader(index)
Now each item in RowData will contain all the data in one of the rows (as a string array).

To get them into clipboard, these could work:
1. sort them into plain text, using tabs and new line character and then use fx.Clipboard.SetString
2. output the results into an Excel file (using jExcel to create the file) and then use fx.Clipboard.SetFiles to copy the file into clipboard.

Hope this helps!
 
Last edited:
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
You can extract the contents of the TableView in an organized List variable as follows:

B4X:
    Dim TV As TableView
    Dim RowData As List
 
    RowData.Initialize
    For i = 0 To TV.Items.Size - 1
        Dim Row() As Object = TV.Items.Get(i)
        Dim this_row(Row.Length) As String
        For k = 0 To Row.Length - 1
            'If the table items are just strings:
            this_row(k) = Row(k).As(String)
            'If each cell is defined as a kind of node:
            If Row(k) Is Label Then
                this_row(k) = Row(k).As(Label).Text
            Else If Row(k) Is TextField Then
                this_row(k) = Row(k).As(TextField).Text
            'And so on
            End If
        Next
        RowData.Add(this_row)
    Next

Headers can be extracted from TV.GetColumnHeader(index)
Now each item in RowData will contain all the data in one of the rows (as a string array).

To get them into clipboard, these could work:
1. sort them into plain text, using tabs and new line character and then use fx.Clipboard.SetString
2. output the results into an Excel file (using jExcel to create the file) and then use fx.Clipboard.SetFiles to copy the file into clipboard.

Hope this helps!


this helps.. here is my code example...

B4X:
Sub CopyGridDataToClipbard(Table As TableView, DelimiterChar As String, IncludeHeader As Boolean)
    
    'Data
    Dim lData As List
    lData.Initialize
    Dim DataForClipBoard As String
        
    For i = 0 To Table.Items.Size - 1 '--------------------
        Dim s As String
        Dim aRow() As Object = Table.Items.Get(i)
                
        For iI = 0 To aRow.Length - 1
            If aRow(iI) Is Label Then
                s = s  & GetlabelText(aRow(iI)).Text & DelimiterChar
            Else
                s = s & aRow(iI) & DelimiterChar
            End If
        Next       
        lData.Add(s)
    Next
    
    'header
    If IncludeHeader = True Then
        Dim hdr As String
        For i = 0 To Table.ColumnsCount - 1
            hdr =  hdr & Table.GetColumnHeader(i) & DelimiterChar
        Next       
        DataForClipBoard = hdr & CRLF
    End If
    
    For d = 0 To lData.Size - 1
        DataForClipBoard = DataForClipBoard & lData.Get(d) & CRLF
    Next
    
    fx.Clipboard.SetString( DataForClipBoard)
    
End Sub

and my data does paste into excel but only in first column... how would I pass the Tab char in order for excel to put them into each cell?
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
Does anybody know how I would pass this to my string:

ASCII character 9, or Unicode U+0009.
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
Solved...

B4X:
Sub miScriptCopyGridData_Action
    
    mdlShared.CopyGridDataToClipbard(tblScript, TAB, True)

End Sub

Sub CopyGridDataToClipbard(Table As TableView, DelimiterChar As String, IncludeHeader As Boolean)
    
    'Data
    Dim lData As List
    lData.Initialize
    Dim DataForClipBoard As String
        
    For i = 0 To Table.Items.Size - 1 '--------------------
        Dim s As String
        Dim aRow() As Object = Table.Items.Get(i)
                
        For iI = 0 To aRow.Length - 1
            If aRow(iI) Is Label Then
                s = s  & GetlabelText(aRow(iI)).Text & DelimiterChar
            Else
                s = s & aRow(iI) & DelimiterChar
            End If
        Next       
        lData.Add(s)
    Next
    
    'header
    If IncludeHeader = True Then
        Dim hdr As String
        For i = 0 To Table.ColumnsCount - 1
            hdr =  hdr & Table.GetColumnHeader(i) & DelimiterChar
        Next       
        DataForClipBoard = hdr & CRLF
    End If
    
    For d = 0 To lData.Size - 1
        DataForClipBoard = DataForClipBoard & lData.Get(d) & CRLF
    Next
    
    fx.Clipboard.SetString(DataForClipBoard)
    
    lData.Clear
    
End Sub
 
Upvote 0
Solution
Top