Ramiki w Calc z makrami formatěrowaś

Z pomocu programěrowańskich rěcow Basic abo Python daju se makra pisaś, kótarež formaty na celowe wobceŕki w Calc nałožuju.

Ramiki w celowych wobceŕkach formatěrowaś

Slědujucy kodowy wótrězk Sub z mjenim FormatCellBorder napórajo, kótaryž nowe ramikowe formaty na danu wobceŕkowu adresu w aktualnej tabeli Calc nałožujo.

Sub FormatCellBorder(cellAddress as String, newStyle as Byte, newWidth as Long, Optional newColor as Long)
    ' Napórajo UNO-strukturu, kótaraž nowy smužkowy format składujo
    Dim lineFormat as New com.sun.star.table.BorderLine2
    lineFormat.LineStyle = newStyle
    lineFormat.LineWidth = newWidth
    If Not IsMissing(newColor) Then lineFormat.Color = newColor
    ' Wuwołujo celowu celu
    Dim oCell as Object
    Set oCell = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName(cellAddress)
    ' Nałožujo nowy format na wšykne ramiki
    oCell.TopBorder = lineFormat
    oCell.RightBorder = lineFormat
    oCell.LeftBorder = lineFormat
    oCell.BottomBorder = lineFormat
End Sub

Górjejce wopisany Sub styri argumenty pśiwzejo:

Aby FormatCellBorder wuwołał, napórajśo nowe makro a pśepódajśo póžedane argumenty, ako se dołojce pokazujo:

Sub MyMacro
    ' Zmóžnja pśistup ku konstantam linijowego stila
    Dim cStyle as Object
    Set cStyle = com.sun.star.table.BorderLineStyle
    ' Formatěrujo "B5" z pśeśěgnjonymi módrymi ramikami
    FormatCellBorder("B5", cStyle.SOLID, 20, RGB(0, 0, 255))
    ' Formatěrujo wšykne ramiki we wobceŕku "D2:F6" z cerwjenymi dypkatymi ramikami
    FormatCellBorder("D2:F6", cStyle.DOTTED, 20, RGB(255, 0, 0))
End Sub

Jo móžno, samsku funkcionalnosć w Python implementěrowaś:

from uno import createUnoStruct
from scriptforge import CreateScriptService

def formatCellBorder(cellAddress, newStyle, newWidth, newColor=0):
    # Definěrujo nowy linijowy format
    line_format = createUnoStruct("com.sun.star.table.BorderLine2")
    line_format.LineStyle = newStyle
    line_format.LineWidth = newWidth
    line_format.Color = newColor
    # Słužba Scriptforge za pśistup k celowym wobceŕkam
    doc = CreateScriptService("Calc")
    cell = doc.XCellRange(cellAddress)
    cell.TopBorder = line_format
    cell.RightBorder = line_format
    cell.LeftBorder = line_format
    cell.BottomBorder = line_format

Slědujucy kodowy wurězk makro z mjenim myMacro implementěrujo, kótarež formatCellBorder wuwołujo:

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

Górjejce pśedstajony kod Python biblioteku ScriptForge wužywa, kótaraž jo wót wersije LibreOffice 7.2 k dispoziciji.


Linijowe stile

Linijowe stile su ako cełolicbowe konstanty definěrowane. Slědujuca tabela konstanty za linijowe stile nalicyjo, kótarež su w Format – Cele – Rejtarik: Ramiki k dispoziciji:

Mě konstanty

Cełolicbowa gódnota

Mě linijowego stila

SOLID

0

Pśeśěgnjony

DOTTED

1

Dypkaty

DASHED

2

Smužkowany

FINE_DASHED

14

Drobnosmužkowany

DOUBLE_THIN

15

Dwójny śańki

DASH_DOT

16

Smužkowy dypk

DASH_DOT_DOT

17

Smužkowy dypkowy dypk


tip

Cytajśo BorderLineStyle Constant Reference (engelski) w dokumentaciji API LibreOffice, aby wěcej wó konstantach linijowego stila zgónił.


Z TableBorder2 ramiki formatěrowaś

Wobceŕkowe objekty maju kakosć z mjenim TableBorder2, kótaruž móžośo wužywaś, aby wobceŕkowe ramiki formatěrował, rowno tak ako w dialogu Format – Cele… – Ramiki we wótrězku Linijowy pórěd.

Mimo górnych, dolnych, lěwych a pšawych ramikow TableBorder2 teke wertikalne a horicontalne ramiki definěrujo. Slědujuce makro janž górny a dolny ramik na wobceŕk "B2:E5" nałožujo.

Sub TableBorder2Example
    Dim cStyle as Object
    Set cStyle = com.sun.star.table.BorderLineStyle
    ' Definěrujo nowy linijowy format
    Dim lineFormat as New com.sun.star.table.BorderLine2
    lineFormat.LineStyle = cStyle.SOLID
    lineFormat.LineWidth = 15
    lineFormat.Color = RGB(0, 0, 0)
    ' Struktura, kótaraž nowu definiciju TableBorder2 składujo
    Dim tableFormat as New com.sun.star.table.TableBorder2
    tableFormat.TopLine = lineFormat
    tableFormat.BottomLine = lineFormat
    tableFormat.IsTopLineValid = True
    tableFormat.IsBottomLineValid = True
    ' Nałožujo tabelowy format na wobceŕk "B2:E5"
    Dim oCell as Object
    oCell = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("B2:E5")
    oCell.TableBorder2 = tableFormat
End Sub

Makro dajo se w Python ako slědujo implementěrowaś:

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

Cytajśo TableBorder2 Struct Reference (engelski) w dokumentaciji API LibreOffice, aby wěcej wó jogo atributach zgónił.


Pšosym pódprějśo nas!