Macro for manipulating table borders
TDF LibreOffice Document Liberation Project Community Blogs Weblate Nextcloud Redmine Ask LibreOffice Donate
Description
This macro manipulates the borders of either an entire table, or a single cell.
Note: it might be more efficient to manipulate borders using styles, see https://ask.libreoffice.org/t/how-to-set-textframe-borders-at-once/108826
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.
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.", _
MB_ICONINFORMATION, _
"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
Private 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
# -*- 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
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)
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
no_line.Color = 0
no_line.InnerLineWidth = 0
no_line.LineDistance = 0
no_line.LineStyle = 0
no_line.LineWidth = 0
no_line.OuterLineWidth = 0
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
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
table.TableBorder = table_border
table.CollapsingBorders = True
table.TableBorderDistances = border_distances
def Main():
_AddBordersToCell("Table1", "B2")
def _AddBordersToCell(my_table, my_cell):
"""Add double-borders to specified cell in specified table"""
desktop = XSCRIPTCONTEXT.getDesktop()
model = desktop.getCurrentComponent()
line = BorderLine2()
line.Color = 0
line.InnerLineWidth = 10
line.LineDistance = 60
line.LineStyle = DOUBLE
line.LineWidth = 2
line.OuterLineWidth = 10
table = model.TextTables.getByName(my_table)
cell = table.getCellByName(my_cell)
cell.LeftBorder = line
cell.LeftBorderDistance = 100
cell.RightBorder = line
cell.RightBorderDistance = 100
cell.TopBorder = line
cell.TopBorderDistance = 100
cell.BottomBorder = line
cell.BottomBorderDistance = 100
g_exportedScripts = (AddBordersToAllCells, Main)
Writer file to test macro
Notes
- ↑ See the API documentation for com.sun.star.table.TableBorder
- ↑ See the API documentation for com.sun.star.table.BorderLine2
- ↑ See the API documentation for com.sun.star.table.BorderLineStyle
- ↑ See the API documentation for com.sun.star.table.BorderLine
- ↑ See the API documentation for com.sun.star.table.TableBorderDistances