Macros/Writer/003

From The Document Foundation Wiki
< Macros‎ | Writer
Jump to: navigation, search

Template:TopMenu/en Template:Menu/en Template:Menu.Macros/en

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

In LibreOffice Basic:

Option Explicit


Sub Main()
    '''Choose which of the two subroutines to call, by commenting out the one you don't want'''

    AddBordersToAllCells
    'AddBordersToCell("Table1","B2")

End Sub ' Main


Sub AddBordersToAllCells()
    '''Add double-borders to all cells in the currently selected table'''

    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 "The cursor is not inside a table.", 64, "Warning from AddBordersToAllCells()"
    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 ' AddBordersToAllCells


Sub AddBordersToCell(myTable As String, myCell As String)
    '''Add double-borders to specified cell in specified table'''

    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 ' AddBordersToCell

ODT file to test macro

Notes