B4J Question xlsx remove row not working

Tim Chapman

Active Member
Licensed User
Longtime User
Here is my code:
Can anyone tell me what to do to make the remove row work?
Line 40 is the culprit.

Thank you all in advance!

B4J Code:
Private Sub DeleteFileFromSpreadsheet(FileID As String)
    LogColor("Sub DeleteFileFromSpreadsheet started", 0xFF009901)
    Dim XL As XLUtils
    XL.Initialize

    ' Define folder and filename
    Dim XLSXFolder As String = File.DirApp '"D:\Downloads" ' Set your folder path here
    Dim XLSXFileName As String = "FileList.xlsx" ' Spreadsheet filename

    ' Create workbook and get the sheet
    Dim workbook As XLWorkbookWriter = XL.CreateWriterFromTemplate(XLSXFolder, XLSXFileName)
    Dim sheet As PoiSheet = workbook.PoiWorkbook.GetSheetByName("Files")

    ' Find the row with the FileID and delete it
    Dim RowNum As Int
    Dim NumberOfRows As Int = sheet.LastRowNumber + 1
    LogColor("Number Of Rows In the Spreadsheet = " & NumberOfRows,0xFFFF0000)
    
    For RowNum = 1 To NumberOfRows - 1 ' Assuming header is in the first row, we start at row 1.  Row 0 contains the header.
        'Note: NumberOfRows is not zero based like RowNum is.  That is why the -1.
        Dim Row As PoiRow = sheet.GetRow(RowNum)
        Dim currentFileId As String = Row.GetCell(5).ValueString ' Assuming Thread ID is in the first column
        LogColor("currentFileId = " & currentFileId,0xFFFF0000)
        
        If currentFileId = FileID Then
            ' Shift rows up to remove the entry
            Dim jo As JavaObject = sheet
            Dim lastRowNum As Int = sheet.LastRowNumber
            LogColor("Row numumber with File on it = " & RowNum,0xFFFF0000)
            LogColor("Last Row in spreadsheet = " & lastRowNum,0xFFFF0000)

            ' Shift rows upwards to fill in the deleted row
            Dim nextRow As PoiRow = sheet.GetRow(RowNum + 1)
            
            If nextRow <> Null Then
                Log("nextRow is not blank")
                Dim fromrow As Int = RowNum + 1
                Log("FromRow = " & fromrow)
                If fromrow > lastRowNum Then
                    jo.RunMethod("removeRow", Array(lastRowNum))
                Else
                    Dim torow As Int = lastRowNum
                    LogColor("torow = " & torow,0xFFFF0000)
                    Dim noofrows As Int = -1
                    LogColor("noofrows = " & noofrows,0xFFFF0000)
                    Log("")
                    
                    jo.RunMethod("shiftRows", Array(fromrow,torow,noofrows))
                    
                End If
            End If

            'Log("File with ID " & FileID & " deleted from spreadsheet.")
            Exit
        End If
    Next

    ' Save the updated workbook after deletion
    Dim f As String = workbook.SaveAs(XLSXFolder, XLSXFileName, True)
    'Wait For (XL.OpenExcel(f)) Complete (Success As Boolean)
    ShowOpenAIFilesList
    
    Log("FIle with ID " & FileID & " deleted from spreadsheet.")
    LogColor("Sub DeleteFileFromSpreadsheet finished", 0xFFFF7100)
End Sub
 

Tim Chapman

Active Member
Licensed User
Longtime User
Sorry. I suppose that would be helpful. Here is the error from the log:
It is caused by line 40 in my code above.

Sub CreateFileListRow finished
09/24/2024 23:51:20 - Files list table completed.
Sub ParseFileList finished
Sub OpenAI_Response finished
Sub OpenAI Library JobDone finished
Sub btnDeleteFile_Click started
Here at DeleteFile Click in main code
Sub DeleteFileFromSpreadsheet started
Number Of Rows In the Spreadsheet = 2
currentFileId = file-CMWGnEMc60tEQSEZAFKJ1aSK
Row numumber with File on it = 1
Last Row in spreadsheet = 1
nextRow is not blank
FromRow = 2
Error occurred on line: 2313 (Main)
java.lang.RuntimeException: Method: removeRow not matched.
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:130)
at b4j.example.main._deletefilefromspreadsheet(main.java:1878)
at b4j.example.main._btndeletefile_click(main.java:1760)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA$1.run(BA.java:236)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:832)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…