Formatació de vores al Calc amb macros

Mitjançant l'ús dels llenguatges de programació BASIC i Python, és possible escriure macros que apliquen formats a intervals de cel·les al Calc.

Formatació de vores en intervals de cel·les

El fragment de codi següent crea una Sub anomenada FormatCellBorder que aplica nous formats a les vores d'un determinat rang d'adreces al full de Calc actual.


    Sub FormatCellBorder(cellAddress as String, newStyle as Byte, newWidth as Long, Optional newColor as Long)
        ' Crea l'estructura UNO que emmagatzemarà el nou format de línia
        Dim lineFormat as New com.sun.star.table.BorderLine2
        lineFormat.LineStyle = newStyle
        lineFormat.LineWidth = newWidth
        If Not IsMissing(newColor) Then lineFormat.Color = newColor
        ' Obté la cel·la objectiu
        Dim oCell as Object
        Set oCell = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName(cellAddress)
        ' Aplica el format nou a totes les vores
        oCell.TopBorder = lineFormat
        oCell.RightBorder = lineFormat
        oCell.LeftBorder = lineFormat
        oCell.BottomBorder = lineFormat
    End Sub
  

La Sub descrita anteriorment pren quatre arguments:

Per cridar a FormatCellBorder creeu una nova macro i passeu els arguments desitjats, tal com es mostra a continuació:


    Sub MyMacro
        ' Dona accés a les constants d'estil de línia
        Dim cStyle as Object
        Set cStyle = com.sun.star.table.BorderLineStyle
        ' Formata «B5» amb vores blaves sòlides
        FormatCellBorder("B5", cStyle.SOLID, 20, RGB(0, 0, 255))
        ' Formata totes les vores de l'interval «D2:F6» amb vores puntejades vermelles
        FormatCellBorder("D2:F6", cStyle.DOTTED, 20, RGB(255, 0, 0))
    End Sub
  

És possible implementar la mateixa funcionalitat en Python:


    from uno import createUnoStruct
    from scriptforge import CreateScriptService
    
    def formatCellBorder(cellAddress, newStyle, newWidth, newColor=0):
        # Defineix el format de línia nou
        line_format = createUnoStruct("com.sun.star.table.BorderLine2")
        line_format.LineStyle = newStyle
        line_format.LineWidth = newWidth
        line_format.Color = newColor
        # Servei de l'ScriptForge per a accedir als intervals de cel·les
        doc = CreateScriptService("Calc")
        cell = doc.XCellRange(cellAddress)
        cell.TopBorder = line_format
        cell.RightBorder = line_format
        cell.LeftBorder = line_format
        cell.BottomBorder = line_format
  

El fragment de codi següent implementa una macro anomenada myMacro que crida 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

El codi Python presentat anteriorment utilitza la biblioteca ScriptForge que està disponible des del LibreOffice 7.2.


Estils de línia

Els estils de línia es defineixen com a constants enteres. La taula següent enumera les constants dels estils de línia disponibles a Format ▸ Cel·les ▸ Vores:

Nom de la constant

Valor enter

Nom de l'estil de línia

SOLID

0

Sòlid

DOTTED

1

Puntejat

DASHED

2

Ratllat

FINE_DASHED

14

Ratllat fi

DOUBLE_THIN

15

Doble fi

DASH_DOT

16

Ratlla punt

DASH_DOT_DOT

17

Ratlla punt punt


tip

Consulteu Referència a la constant BorderLineStyle a la documentació de l'API de LibreOffice per aprendre més sobre les constants de l'estil de línia.


Formatació de vores mitjançant TableBorder2

Range objects have a property named TableBorder2 that can be used to format range borders as it is done in the Format - Cells - Borders dialog in the Line Arrangement section.

In addition to top, bottom, left and right borders, TableBorder2 also defines vertical and horizontal borders. The macro below applies only the top and bottom borders to the range "B2:E5".


    Sub TableBorder2Example
        Dim cStyle as Object
        Set cStyle = com.sun.star.table.BorderLineStyle
        ' Defineix el format de línia nou
        Dim lineFormat as New com.sun.star.table.BorderLine2
        lineFormat.LineStyle = cStyle.SOLID
        lineFormat.LineWidth = 15
        lineFormat.Color = RGB(0, 0, 0)
        ' Estructura que emmagatzema la definició del nou «TableBorder2»
        Dim tableFormat as New com.sun.star.table.TableBorder2
        tableFormat.TopLine = lineFormat
        tableFormat.BottomLine = lineFormat
        tableFormat.IsTopLineValid = True
        tableFormat.IsBottomLineValid = True
        ' Aplica la formatació de taula a l'interval «B2:E5»
        Dim oCell as Object
        oCell = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("B2:E5")
        oCell.TableBorder2 = tableFormat
    End Sub
  

La macro es pot implementar en Python d'aquesta manera:


    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

Refer to the TableBorder2 Struct Reference in the LibreOffice API documentation to learn more about its attributes.


Ens cal la vostra ajuda!