https://bugs.documentfoundation.org/show_bug.cgi?id=99035

--- Comment #18 from Paul <pbpub...@gmail.com> ---
Ok, I asked and received a really nice macro reply. I slightly altered it by
adding an Input box to spec the number of columns on the fly, and to exit if
the input box was left blank. The Ask thread is at http://bit.ly/2ETWnbD.

I will print the finished macro below. But there is one strange problem. Though
the macro works fine when invoked from the Basic editor, it errors out when I
use a custom Menu entry for it. The error is: 

    A Scripting Framework error occurred while running the Basic script 
    Standard.Module1.Writer_SelectionToColumns.

    Message: wrong number of parameters!

Also errors out via hotkey. Here's the macro. I would like to see something
like this incorporated into Core somehow. :)

Sub Writer_SelectionToColumns( ByRef iNumColumns As Integer, Optional
sSeparator )
  REM Courtesy of librebel, 2/19/18; http://bit.ly/2ETWnbD. My input box added
  REM Convert selected Text into a specified number of Table Columns.
  REM <iNumColumns> : The number of columns for the Table to be created.
  REM <sSeparator>  : String pattern separating the Text portions to be
inserted into the Table Cells.
  REM NB. This method does not preserve the existing text format of the
selection.
  REM TODO: Format text; AutoFormat Table.
    Dim oDocument As Object : oDocument = ThisComponent
    If oDocument.supportsService( "com.sun.star.text.TextDocument" ) Then
        Dim iNumColumns As Integer 
        iNumColumns =  InputBox("Enter desired number of columns (blank = 0)")

        If iNumColumns <= 0 Then Exit Sub
        If IsMissing( sSeparator ) Or sSeparator  = "" Then sSeparator =
chr(10)

        Dim oSelection As Object : oSelection =
oDocument.CurrentController.Selection.getByIndex( 0 )
        Dim aParts() As String   : aParts     = Split( oSelection.getString(),
sSeparator )
        Dim iNumRows As Integer  : iNumRows   = ( 1 + uBound( aParts ) ) \
iNumColumns
        If ( 1 + uBound( aParts ) ) Mod iNumColumns > 0 Then iNumRows =
iNumRows + 1    REM Ceiling()...
        If iNumRows = 0 Then Exit Sub

        Dim oTable As Object     : oTable     = oDocument.createInstance(
"com.sun.star.text.TextTable" )
        oTable.initialize( iNumRows, iNumColumns )

        oDocument.Text.insertTextContent( oSelection, oTable, True )    REM
Replace selection with Table.

        Dim i As Integer
        For i = 0 To uBound( aParts )
            oTable.getCellByPosition( i Mod iNumColumns, i \ iNumColumns
).setString( aParts( i ) )
        Next i

    End If

End Sub

-- 
You are receiving this mail because:
You are the assignee for the bug.
_______________________________________________
Libreoffice-bugs mailing list
Libreoffice-bugs@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-bugs

Reply via email to