B4J Question [XLUtils] Reorder sheets

Alessandro71

Well-Known Member
Licensed User
Longtime User
I've found no methods to get the sheets list from an excel workbook and move them around, for example, for sorting them in ascending order, or inserting a new one in a specific position based on the existing sheets names.
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
Gratefully, I contribute back with a sorting Sub
B4X:
Private Sub SortSheets(workbook As XLWorkbookWriter)
    Dim sheets() As String = workbook.PoiWorkbook.GetSheetNames
    If (sheets.Length > 1) Then
        Dim sheetslist As List
        sheetslist.Initialize2(sheets)
        sheetslist.Sort(True)
        
        For i = 0 To (sheetslist.Size - 1)
            workbook.jWorkbook.RunMethod("setSheetOrder", Array(sheetslist.Get(i), i))
        Next
    End If
End Sub
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
time for a little optimization then...
B4X:
Private Sub SortSheets(workbook As XLWorkbookWriter)
    Dim sheets As List = workbook.PoiWorkbook.GetSheetNames
    If (sheets.Size > 1) Then
        sheets.Sort(True)
        
        For i = 0 To (sheets.Size - 2)    'the last one will fall into place automatically
            workbook.jWorkbook.RunMethod("setSheetOrder", Array(sheets.Get(i), i))
        Next
    End If
End Sub
 
Upvote 0
Top