Formatierung von Rändern in Calc mit Makros

Makros für die Formatierung von Zellbereichen in Calc können in den Programmiersprachen Basic oder Python geschrieben werden.

Ränder in Zellbereichen formatieren

Der folgende Codeausschnitt erstellt ein Sub namens FormatCellBorder, das neue Rahmenformate auf eine bestimmte Bereichsadresse in der aktuellen Calc-Tabelle anwendet.


    Sub FormatCellBorder(cellAddress as String, newStyle as Byte, newWidth as Long, Optional newColor as Long)
        ' Erstellt die UNO-Struktur, die das neue Zeilenformat speichern wird
        Dim lineFormat as New com.sun.star.table.BorderLine2
        lineFormat.LineStyle = newStyle
        lineFormat.LineWidth = newWidth
        If Not IsMissing(newColor) Then lineFormat.Color = newColor
        ' Ruft die Zielzelle auf
        Dim oCell as Object
        Set oCell = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName(cellAddress)
        ' Wendet das neue Format auf alle Rahmen an
        oCell.TopBorder = lineFormat
        oCell.RightBorder = lineFormat
        oCell.LeftBorder = lineFormat
        oCell.BottomBorder = lineFormat
    End Sub
  

Das oben beschriebene Sub enthält vier Argumente:

Um FormatCellBorder aufzurufen, erstellen Sie ein neues Makro und übergeben Sie die gewünschten Argumente, wie unten gezeigt:


    Sub MyMacro
        ' Ermöglicht den Zugriff auf die Linienstil-Konstanten
        Dim cStyle as Object
        Set cStyle = com.sun.star.table.BorderLineStyle
        ' Formatiert "B5" mit durchgezogenem blauen Rahmen
        FormatCellBorder("B5", cStyle.SOLID, 20, RGB(0, 0, 255))
        ' Formatiert alle Rahmen im Bereich "D2:F6" mit rot gepunkteten Rändern
        FormatCellBorder("D2:F6", cStyle.DOTTED, 20, RGB(255, 0, 0))
    End Sub
  

Es ist möglich, die gleiche Funktionalität in Python zu implementieren:


    from uno import createUnoStruct
    from scriptforge import CreateScriptService
    
    def formatCellBorder(cellAddress, newStyle, newWidth, newColor=0):
        # Definiert das neue Linienformat
        line_format = createUnoStruct("com.sun.star.table.BorderLine2")
        line_format.LineStyle = newStyle
        line_format.LineWidth = newWidth
        line_format.Color = newColor
        # Dienst Scriptforge für den Zugriff auf Zellbereiche
        doc = CreateScriptService("Calc")
        cell = doc.XCellRange(cellAddress)
        cell.TopBorder = line_format
        cell.RightBorder = line_format
        cell.LeftBorder = line_format
        cell.BottomBorder = line_format
  

Der folgende Codeausschnitt implementiert ein Makro mit dem Namen myMacro, das formatCellBorder aufruft:


    from com.sun.star.table import BorderLineStyle as cStyle
    
    def myMacro():
        bas = CreateScriptService("Basic")
        formatCellBorder("B5", cStyle.SOLID, 20, bas.RGB(0, 0, 255))
        formatCellBorder("D2:F6", cStyle.DOTTED, 20, bas.RGB(255, 0, 0))
  
note

Der oben vorgestellte Python-Code verwendet die Bibliothek ScriptForge, die seit LibreOffice 7.2 verfügbar ist.


Linienstile

Linienstile werden durch ganzzahlige Konstanten definiert. In der folgenden Tabelle sind die Konstanten für die verfügbaren Linienstile unter Format – Zellen… – Register: Umrandung aufgeführt:

Name der Konstanten

Ganzzahliger Wert

Name des Linienstils

SOLID

0

Durchgängig

DOTTED

1

Gepunktet

DASHED

2

Gestrichelt

FINE_DASHED

14

Fein gestrichelt

DOUBLE_THIN

15

Fein doppelt

DASH_DOT

16

Strich-Punkt

DASH_DOT_DOT

17

Strich-Punkt-Punkt


tip

Lesen Sie für weitere Details über die Konstanten der Linienstile die englische Seite BorderLineStyle Constant Reference in der Dokumentation zur LibreOffice-API.


Umrandungen formatieren mit TableBorder2

Bereichsobjekte haben eine Eigenschaft namens TableBorder2, die zum Formatieren der Bereichsumrandungen verwendet werden kann, genauso wie Sie sie im Dialog Format – Zellen… – Register: Umrandungen im Bereich Linienanordnung formatieren können.

Zusätzlich zu den oberen, unteren, linken und rechten Umrandungen kann TableBorder2 auch vertikale und horizontale Umrandungen festlegen. Das Makro unten wendet nur die obere und untere Umrandung auf den Bereich "B2:E5" an.


    Sub TableBorder2Example
        Dim cStyle as Object
        Set cStyle = com.sun.star.table.BorderLineStyle
        ' Legt das neue Linienformat fest
        Dim lineFormat as New com.sun.star.table.BorderLine2
        lineFormat.LineStyle = cStyle.SOLID
        lineFormat.LineWidth = 15
        lineFormat.Color = RGB(0, 0, 0)
        ' Struktur, die die neue Definition von TableBorder2 speichert
        Dim tableFormat as New com.sun.star.table.TableBorder2
        tableFormat.TopLine = lineFormat
        tableFormat.BottomLine = lineFormat
        tableFormat.IsTopLineValid = True
        tableFormat.IsBottomLineValid = True
        ' Wendet das Tabellenformat auf den Bereich "B2:E5" an
        Dim oCell as Object
        oCell = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("B2:E5")
        oCell.TableBorder2 = tableFormat
    End Sub
  

Das Makro kann in Python wie folgt implementiert werden:


    from com.sun.star.table import BorderLineStyle as cStyle
    from scriptforge import CreateScriptService
    
    def tableBorder2Example():
        bas = CreateScriptService("Basic")
        line_format = createUnoStruct("com.sun.star.table.BorderLine2")
        line_format.LineStyle = cStyle.SOLID
        line_format.LineWidth = 18
        line_format.Color = bas.RGB(0, 0, 0)
        table_format = createUnoStruct("com.sun.star.table.TableBorder2")
        table_format.TopLine = line_format
        table_format.BottomLine = line_format
        table_format.IsTopLineValid = True
        table_format.IsBottomLineValid = True
        doc = CreateScriptService("Calc")
        cell = doc.XCellRange("B2:E5")
        cell.TableBorder2 = table_format
  
tip

Lesen Sie für weitere Details über die Eigenschaften die englische Seite TableBorder2 Struct Reference in der Dokumentation zur LibreOffice-API.


Bitte unterstützen Sie uns!