Pemformatan Batas di Calc dengan Macro

Degan menggunakan bahasa pemograman Basic atau Python dimungkinkan untuk menulis makro yang menerapkan format rentang sel di Calc.

Memformat Batas dalam Rentang Sel

Cuplikan kode di bawah ini membuat Sub yang disebut FormatCellBorder yang menerapkan format batas baru ke alamat rentang tertentu di lembar Calc saat ini.


    Sub FormatCellBorder(cellAddress as String, newStyle as Byte, newWidth as Long, Optional newColor as Long)
        ' Membuat struct UNO yang akan menyimpan format baris baru
        Dim lineFormat as New com.sun.star.table.BorderLine2
        lineFormat.LineStyle = newStyle
        lineFormat.LineWidth = newWidth
        If Not IsMissing(newColor) Then lineFormat.Color = newColor
        ' Mendapatkan target sel
        Dim oCell as Object
        Set oCell = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName(cellAddress)
        ' Menerapkan format baru ke semua batas
        oCell.TopBorder = lineFormat
        oCell.RightBorder = lineFormat
        oCell.LeftBorder = lineFormat
        oCell.BottomBorder = lineFormat
    End Sub
  

Sub yang dijelaskan di atas mengambil empat argumen:

Untuk memanggil FormatCellBorder buat makro baru dan berikan argumen yang diinginkan, seperti yang ditunjukkan di bawah ini:


    Sub MyMacro
        ' Memberikan akses ke konstanta gaya garis
        Dim cStyle as Object
        Set cStyle = com.sun.star.table.BorderLineStyle
        ' Format "B5" dengan batas biru solid
        FormatCellBorder("B5", cStyle.SOLID, 20, RGB(0, 0, 255))
        ' Format semua batas dalam rentang "D2:F6" dengan batas merah titik-titik
        FormatCellBorder("D2:F6", cStyle.DOTTED, 20, RGB(255, 0, 0))
    End Sub
  

Dimungkinkan untuk mengimplementasikan fungsi yang sama dengan Python:


    from uno import createUnoStruct
    from scriptforge import CreateScriptService
    
    def formatCellBorder(cellAddress, newStyle, newWidth, newColor=0):
        # Mendefinisikan format baris baru
        line_format = createUnoStruct("com.sun.star.table.BorderLine2")
        line_format.LineStyle = newStyle
        line_format.LineWidth = newWidth
        line_format.Color = newColor
        # Layanan scriptforge untuk mengakses rentang sel
        doc = CreateScriptService("Calc")
        cell = doc.XCellRange(cellAddress)
        cell.TopBorder = line_format
        cell.RightBorder = line_format
        cell.LeftBorder = line_format
        cell.BottomBorder = line_format
  

Cuplikan kode di bawah ini mengimplementasikan makro bernama myMacro yang memanggil 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

Kode Python yang disajikan di atas menggunakan pustaka ScriptForge yang tersedia sejak LibreOffice 7.2.


Gaya Garis

Gaya garis didefinisikan sebagai konstanta bilangan integer. Tabel di bawah mencantumkan konstanta untuk gaya garis yang tersedia di Format - Sel - Batas:

Nama konstanta

Nilai integer

Nama gaya garis

SOLID

0

Padat

DOTTED

1

Titik-titik

DASHED

2

Putus-putus

FINE_DASHED

14

Putus-putus halus

DOUBLE_THIN

15

Garis ganda

DASH_DOT

16

Putus-putus titik

DASH_DOT_DOT

17

Garis titik-titik


tip

Mengacu pada Referensi Konstanta BorderLineStyle di dokumentasi LibreOffice API untuk mempelajari lebih lanjut tentang konstanta gaya garis.


Memformat Batas Menggunakan TableBorder2

Objek rentang memiliki properti bernama TableBorder2 yang dapat digunakan untuk memformat batas rentang seperti yang dilakukan dalam dialog Format - Sel - Batas dalam bagian Penataan Garis.

Selain batas atas, bawah, kiri dan kanan, TableBorder2 juga mendefinisikan batas vertikal dan horizontal. Makro di bawah ini hanya menerapkan batas atas dan bawah ke rentang "B2:E5".


    Sub TableBorder2Example
        Dim cStyle as Object
        Set cStyle = com.sun.star.table.BorderLineStyle
        ' Mendefinisikan format baris baru
        Dim lineFormat as New com.sun.star.table.BorderLine2
        lineFormat.LineStyle = cStyle.SOLID
        lineFormat.LineWidth = 15
        lineFormat.Color = RGB(0, 0, 0)
        ' Struct yang menyimpan definisi TableBorder2 baru
        Dim tableFormat as New com.sun.star.table.TableBorder2
        tableFormat.TopLine = lineFormat
        tableFormat.BottomLine = lineFormat
        tableFormat.IsTopLineValid = True
        tableFormat.IsBottomLineValid = True
        ' Menerapkan format tabel ke dalam rentang "B2:E5"
        Dim oCell as Object
        oCell = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("B2:E5")
        oCell.TableBorder2 = tableFormat
    End Sub
  

Makro dapat diimplementasikan dalam Python sebagai berikut:


    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

Mengacu pada Referensi Struct TableBorder2 di dokumentasi LibreOffice API untuk mempelajari lebih lanjut tentang atributnya.


Mohon dukung kami!