Macros/Writer/003/es

    From The Document Foundation Wiki
    < Macros‎ | Writer
    This page is a translated version of the page Macros/Writer/003 and the translation is 3% complete.


    Other languages:

    Description

    This macro manipulates the borders of either an entire table, or a single cell.

    The table borders are accessible via the property com.sun.star.table.TableBorder[1].

    The elements of this struct have two types:

    • Borders (upper, lower, left, right, horizontal, vertical); the borders are the structs com.sun.star.table.BorderLine2[2] which "add" style elements[3] for the struct com.sun.star.table.BorderLine[4].
    • Booleans (IsLeftLineValid, IsRightLineValid, IsTopLineValid, etc.). In writing, defining them as True to modify the line correspondingly, and False to ignore; meaning it keeps its current value.

    The definition for spacing with the content is via the property com.sun.star.table.TableBorderDistances[5] of the table. This struct is manipulated via the same principle, using BottomDistance, LeftDistance, etc. and booleans for modifying the name.

    The macro below is executed on the currently selected table. In the macro, we do the following:

    • We start by checking that the cursor is indeed inside a table.
    • The objective is to replace the current borders (the parameters Is... are defined as True) to obtain a an external perimeter and horizontal double lines. We don't want vertical lines. Therefore we need .IsVerticalLineValid = true and VerticalLine = LibONoLine, a struct defined for applying "empty" to a line. We cannot simply use .IsVerticalLineValid = false since the current table could have vertical lines defined and we want to suppress them. With the parameter false, the existing lines would be left in their current state.
    • The desired border width is 0.30 cm.
    • The parameter Fuse the adjacent line styles is defined by the property CollapsingBorders of the table.

    Code

    Border line distances and widths are expressed in 1/100 mm.

    LibreOffice Basic

    Option Explicit
    
    
    Sub Main()
        '''<span lang="en" dir="ltr" class="mw-content-ltr">Choose which of the two subroutines to call, by commenting out the one you don't want</span>'''
    
        '<span lang="en" dir="ltr" class="mw-content-ltr">AddBordersToAllCells</span>
        <span lang="en" dir="ltr" class="mw-content-ltr">_AddBordersToCell</span>("<span lang="en" dir="ltr" class="mw-content-ltr">Table1</span>","B2")
    
    End Sub ' Main
    
    
    Sub <span lang="en" dir="ltr" class="mw-content-ltr">AddBordersToAllCells</span>()
        ''' <span lang="en" dir="ltr" class="mw-content-ltr">Add double-borders to all cells in the currently selected table</span>'''
    
        Dim oCursor As Object
        Dim oTable As Object
        Dim oTableBorder As Object
    
        Dim oLine As New com.sun.star.table.BorderLine2
        Dim oNoLine As New com.sun.star.table.BorderLine2
        Dim oBorderDistances As New com.sun.star.table.TableBorderDistances
        oCursor = ThisComponent.CurrentController.ViewCursor
    
        If (isEmpty(oCursor.textTable)) Then
            MsgBox "<span lang="en" dir="ltr" class="mw-content-ltr">The cursor is not inside a table.</span>", _
                MB_ICONINFORMATION, _
                "<span lang="en" dir="ltr" class="mw-content-ltr">Warning from AddBordersToAllCells()</span>"
        Else
            With oLine
                .Color = 0
                .InnerLineWidth = 10
                .LineDistance = 60 
                .LineStyle = com.sun.star.table.BorderLineStyle.DOUBLE 
                .LineWidth = 5
                .OuterLineWidth = 10
            End With
            With oNoLine
                .Color = 0
                .InnerLineWidth = 0
                .LineDistance = 0
                .LineStyle = 0
                .LineWidth = 0
                .OuterLineWidth = 0
            End With
            With oBorderDistances
                .BottomDistance = 300
                .IsBottomDistanceValid = True   
                .IsLeftDistanceValid = True   
                .IsRightDistanceValid = True   
                .IsTopDistanceValid = True   
                .LeftDistance = 300   
                .RightDistance = 300
                .TopDistance = 300
            End With
            
            oTable = oCursor.TextTable
            oTableBorder = oTable.TableBorder
            
            With oTableBorder        
                .IsLeftLineValid = true
                .IsRightLineValid = true
                .IsTopLineValid = true
                .IsBottomLineValid = true
                .IsHorizontalLineValid = true
                .IsVerticalLineValid = true
                .LeftLine = oLine
                .RightLine = oLine
                .TopLine = oLine
                .BottomLine = oLine 
                .HorizontalLine = oLine
                .VerticalLine = oNoLine
            End With
            
            With oTable
                .TableBorder = oTableBorder 
                .HoriOrient = com.sun.star.text.HoriOrientation.FULL 
                .CollapsingBorders = true
                .TableBorderDistances = oBorderDistances
            End With  
    
        End If
    
    End Sub ' <span lang="en" dir="ltr" class="mw-content-ltr">AddBordersToAllCells</span>
    
    
    Private Sub <span lang="en" dir="ltr" class="mw-content-ltr">_AddBordersToCell</span>(myTable As String, myCell As String)
        '''<span lang="en" dir="ltr" class="mw-content-ltr">Add double-borders to specified cell in specified table</span>'''
    
        Dim oTable As Object
        Dim oCell As Object
        Dim oLine As New com.sun.star.table.BorderLine2
    
        With oLine
            .Color = 0
            .InnerLineWidth = 10
            .LineDistance = 60 
            .LineStyle = com.sun.star.table.BorderLineStyle.DOUBLE
            .LineWidth = 2
            .OuterLineWidth = 10
        End With
    
        oTable = ThisComponent.TextTables.getByName(myTable)
        oCell = oTable.getCellByName(myCell)
    
        With oCell
            .LeftBorder = oLine
            .LeftBorderDistance = 100
            .RightBorder = oLine
            .RightBorderDistance = 100
            .TopBorder = oLine
            .TopBorderDistance = 100
            .BottomBorder = oLine 
            .BottomBorderDistance = 100
        End With
    
    End Sub ' <span lang="en" dir="ltr" class="mw-content-ltr">AddBordersToCell</span>

    Python

    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    from com.sun.star.table import BorderLine2
    from com.sun.star.table import TableBorderDistances
    from com.sun.star.table.BorderLineStyle import DOUBLE
    import uno
    import msgbox
    from com.sun.star.text.HoriOrientation import FULL
    </div>
    
    
    <div lang="en" dir="ltr" class="mw-content-ltr">
    def _msgbox(prompt='', title='LibreOffice'):
        """ Displays a dialog box containing a message and returns a value."""
        mb = msgbox.MsgBox(uno.getComponentContext())
        mb.addButton('OK')
        mb.show(prompt, 0, title)
    </div>
    
    
    <div lang="en" dir="ltr" class="mw-content-ltr">
    def AddBordersToAllCells():
        """Add double-borders to all cells in the currently selected table"""
        desktop = XSCRIPTCONTEXT.getDesktop()
        model = desktop.getCurrentComponent()
        view_cursor = model.getCurrentController()
        cursor = view_cursor.getViewCursor()
        line = BorderLine2()
        no_line = BorderLine2()
        border_distances = TableBorderDistances()
        if cursor.TextTable is None:
            _msgbox("The cursor is not inside a table.", "Warning from AddBordersToAllCells()")
        else:
            line.Color = 0
            line.InnerLineWidth = 10
            line.LineDistance = 60
            line.LineStyle = DOUBLE
            line.LineWidth = 5
            line.OuterLineWidth = 10
    </div>
    
            <div lang="en" dir="ltr" class="mw-content-ltr">
    no_line.Color = 0
            no_line.InnerLineWidth = 0
            no_line.LineDistance = 0
            no_line.LineStyle = 0
            no_line.LineWidth = 0
            no_line.OuterLineWidth = 0
    </div>
    
            <div lang="en" dir="ltr" class="mw-content-ltr">
    border_distances.BottomDistance = 300
            border_distances.IsBottomDistanceValid = True
            border_distances.IsLeftDistanceValid = True
            border_distances.IsRightDistanceValid = True
            border_distances.IsTopDistanceValid = True
            border_distances.LeftDistance = 300
            border_distances.RightDistance = 300
            border_distances.TopDistance = 300
    </div>
    
            <div lang="en" dir="ltr" class="mw-content-ltr">
    table = cursor.TextTable
            table_border = table.TableBorder
            table_border.IsLeftLineValid = True
            table_border.IsRightLineValid = True
            table_border.IsTopLineValid = True
            table_border.IsBottomLineValid = True
            table_border.IsHorizontalLineValid = True
            table_border.IsVerticalLineValid = True
            table_border.LeftLine = line
            table_border.RightLine = line
            table_border.TopLine = line
            table_border.BottomLine = line
            table_border.HorizontalLine = line
            table_border.VerticalLine = no_line
    </div>
    
            <div lang="en" dir="ltr" class="mw-content-ltr">
    table.TableBorder = table_border
            table.CollapsingBorders = True
            table.TableBorderDistances = border_distances
    </div>
    
    
    <div lang="en" dir="ltr" class="mw-content-ltr">
    def Main():
        _AddBordersToCell("Table1", "B2")
    </div>
    
    
    <div lang="en" dir="ltr" class="mw-content-ltr">
    def _AddBordersToCell(my_table, my_cell):
        """Add double-borders to specified cell in specified table"""
        desktop = XSCRIPTCONTEXT.getDesktop()
        model = desktop.getCurrentComponent()
    </div>
    
        <div lang="en" dir="ltr" class="mw-content-ltr">
    line = BorderLine2()
        line.Color = 0
        line.InnerLineWidth = 10
        line.LineDistance = 60
        line.LineStyle = DOUBLE
        line.LineWidth = 2
        line.OuterLineWidth = 10
    </div>
    
        <div lang="en" dir="ltr" class="mw-content-ltr">
    table = model.TextTables.getByName(my_table)
        cell = table.getCellByName(my_cell)
    </div>
    
        <div lang="en" dir="ltr" class="mw-content-ltr">
    cell.LeftBorder = line
        cell.LeftBorderDistance = 100
        cell.RightBorder = line
        cell.RightBorderDistance = 100
        cell.TopBorder = line
        cell.TopBorderDistance = 100
        cell.BottomBorder = line
        cell.BottomBorderDistance = 100
    </div>
    
    
    <div lang="en" dir="ltr" class="mw-content-ltr">
    g_exportedScripts = (AddBordersToAllCells, Main)
    </div>
    
    <div lang="en" dir="ltr" class="mw-content-ltr">

    Notes