Pag-format ng mga Border sa Calc gamit ang mga Macros

Sa pamamagitan ng paggamit ng Basic o Python programming language posible na magsulat ng mga macro na naglalapat ng mga format sa mga hanay ng mga cell sa Calc.

Pag-format ng mga Border sa Mga Saklaw ng Mga Cell

Ang code snippet sa ibaba ay lumilikha ng a Sub tinawag FormatCellBorder na naglalapat ng mga bagong format ng hangganan sa isang ibinigay na address ng hanay sa kasalukuyang Calc sheet.


    Sub FormatCellBorder(cellAddress as String, newStyle as Byte, newWidth as Long, Optional newColor as Long)
        ' Lumilikha ng UNO struct na mag-iimbak ng bagong format ng linya
        Dim lineFormat as New com.sun.star.table.BorderLine2
        lineFormat.LineStyle = newStyle
        lineFormat.LineWidth = newWidth
        If Not IsMissing(newColor) Then lineFormat.Color = newColor
        ' Nakukuha ang target na cell
        Dim oCell as Object
        Set oCell = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName(cellAddress)
        ' Inilalapat ang bagong format sa lahat ng mga hangganan
        oCell.TopBorder = lineFormat
        oCell.RightBorder = lineFormat
        oCell.LeftBorder = lineFormat
        oCell.BottomBorder = lineFormat
    End Sub
  

Ang Sub inilarawan sa itaas ay tumatagal ng apat na argumento:

Para tumawag FormatCellBorder lumikha ng bagong macro at ipasa ang mga gustong argumento, tulad ng ipinapakita sa ibaba:


    Sub MyMacro
        ' Nagbibigay ng access sa line style constants
        Dim cStyle as Object
        Set cStyle = com.sun.star.table.BorderLineStyle
        ' Nag-format ng "B5" na may solidong asul na mga hangganan
        FormatCellBorder("B5", cStyle.SOLID, 20, RGB(0, 0, 255))
        ' Ipo-format ang lahat ng mga hangganan sa hanay na "D2:F6" na may mga pulang tuldok na hangganan
        FormatCellBorder("D2:F6", cStyle.DOTTED, 20, RGB(255, 0, 0))
    End Sub
  

Posibleng ipatupad ang parehong pag-andar sa Python:


    from uno import createUnoStruct
    from scriptforge import CreateScriptService
    
    def formatCellBorder(cellAddress, newStyle, newWidth, newColor=0):
        # Tinutukoy ang bagong format ng linya
        line_format = createUnoStruct("com.sun.star.table.BorderLine2")
        line_format.LineStyle = newStyle
        line_format.LineWidth = newWidth
        line_format.Color = newColor
        # Serbisyo ng Scriptforge upang ma-access ang mga hanay ng cell
        doc = CreateScriptService("Calc")
        cell = doc.XCellRange(cellAddress)
        cell.TopBorder = line_format
        cell.RightBorder = line_format
        cell.LeftBorder = line_format
        cell.BottomBorder = line_format
  

Ang code snippet sa ibaba ay nagpapatupad ng macro na pinangalanan myMacro na tumatawag formatCellBorder :


    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

Ang Python code na ipinakita sa itaas ay gumagamit ng library ng ScriptForge na magagamit mula noong LibreOffice 7.2.


Mga Estilo ng Linya

Ang mga istilo ng linya ay tinukoy bilang mga integer constant. Inililista ng talahanayan sa ibaba ang mga constant para sa mga istilo ng linya na available sa Format - Mga Cell - Mga Hangganan :

Palagiang pangalan

Halaga ng integer

Pangalan ng istilo ng linya

SOLID

0

Solid

DOTTED

1

May tuldok

DASHED

2

Dashed

FINE_DASHED

14

Fine dashed

DOUBLE_THIN

15

Dobleng manipis

DASH_DOT

16

Dash tuldok

DASH_DOT_DOT

17

Dash dot tuldok


tip

Sumangguni sa BorderLineStyle Constant Reference sa dokumentasyon ng LibreOffice API upang matuto nang higit pa tungkol sa mga pare-pareho ng istilo ng linya.


Pag-format ng mga Border Gamit ang TableBorder2

Ang mga saklaw na bagay ay may isang katangian na pinangalanan TableBorder2 na maaaring magamit upang i-format ang mga hangganan ng saklaw tulad ng ginagawa sa Format - Mga Cell - Mga Hangganan diyalogo sa Pag-aayos ng Linya seksyon.

Bilang karagdagan sa itaas, ibaba, kaliwa at kanang mga hangganan, TableBorder2 Tinutukoy din ang patayo at pahalang na mga hangganan. Ang macro sa ibaba ay nalalapat lamang sa itaas at ibabang mga hangganan sa hanay na "B2:E5".


    Sub TableBorder2Example
        Dim cStyle as Object
        Set cStyle = com.sun.star.table.BorderLineStyle
        ' Tinutukoy ang bagong format ng linya
        Dim lineFormat as New com.sun.star.table.BorderLine2
        lineFormat.LineStyle = cStyle.SOLID
        lineFormat.LineWidth = 15
        lineFormat.Color = RGB(0, 0, 0)
        ' Struct na nag-iimbak ng bagong kahulugan ng TableBorder2
        Dim tableFormat as New com.sun.star.table.TableBorder2
        tableFormat.TopLine = lineFormat
        tableFormat.BottomLine = lineFormat
        tableFormat.IsTopLineValid = True
        tableFormat.IsBottomLineValid = True
        ' Inilapat ang format ng talahanayan sa hanay na "B2:E5"
        Dim oCell as Object
        oCell = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("B2:E5")
        oCell.TableBorder2 = tableFormat
    End Sub
  

Ang macro ay maaaring ipatupad sa Python tulad ng sumusunod:


    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

Sumangguni sa TableBorder2 Struct Reference sa dokumentasyon ng LibreOffice API upang matuto nang higit pa tungkol sa mga katangian nito.


Mangyaring suportahan kami!