ການຈັດຮູບແບບຂອບໃນ Calc ດ້ວຍມາໂຄຣ

ໂດຍການໃຊ້ພາສາ Basic ຫຼື Python, ທ່ານສາມາດຂຽນມາໂຄຣເພື່ອຈັດຮູບແບບໃຫ້ກັບຊ່ວງຂອງເຊວໃນ Calc ໄດ້.

ການຈັດຮູບແບບຂອບໃນຊ່ວງຂອງເຊວ

ສ່ວນຂອງໂຄ້ດລຸ່ມນີ້ຈະສ້າງ Sub ທີ່ຊື່ວ່າ FormatCellBorder ເພື່ອຈັດຮູບແບບຂອບໃໝ່ໃຫ້ກັບທີ່ຢູ່ຊ່ວງທີ່ກຳນົດໃນແຜ່ນງານ Calc ປັດຈຸບັນ.


    Sub FormatCellBorder(cellAddress as String, newStyle as Byte, newWidth as Long, Optional newColor as Long)
        ' ສ້າງໂຄງສ້າງ (struct) UNO ທີ່ຈະເກັບຮູບແບບເສັ້ນໃໝ່
        Dim lineFormat as New com.sun.star.table.BorderLine2
        lineFormat.LineStyle = newStyle
        lineFormat.LineWidth = newWidth
        If Not IsMissing(newColor) Then lineFormat.Color = newColor
        ' ດຶງຂໍ້ມູນເຊວເປົ້າໝາຍ
        Dim oCell as Object
        Set oCell = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName(cellAddress)
        ' ນຳໃຊ້ຮູບແບບໃໝ່ໃຫ້ກັບຂອບທັງໝົດ
        oCell.TopBorder = lineFormat
        oCell.RightBorder = lineFormat
        oCell.LeftBorder = lineFormat
        oCell.BottomBorder = lineFormat
    End Sub
  

Sub ທີ່ອະທິບາຍຂ້າງເທິງນີ້ຮັບອາຄິວເມນ 4 ຢ່າງ:

ເພື່ອຮຽກໃຊ້ FormatCellBorder, ໃຫ້ສ້າງມາໂຄຣໃໝ່ ແລະ ສົ່ງອາຄິວເມນທີ່ຕ້ອງການ ດັ່ງທີ່ສະແດງລຸ່ມນີ້:


    Sub MyMacro
        ' ໃຫ້ການເຂົ້າເຖິງຄ່າຄົງທີ່ຂອງຮູບແບບເສັ້ນ
        Dim cStyle as Object
        Set cStyle = com.sun.star.table.BorderLineStyle
        ' ຈັດຮູບແບບ "B5" ດ້ວຍຂອບເສັ້ນທຶບສີຟ້າ
        FormatCellBorder("B5", cStyle.SOLID, 20, RGB(0, 0, 255))
        ' ຈັດຮູບແບບຂອບທັງໝົດໃນຊ່ວງ "D2:F6" ດ້ວຍຂອບເສັ້ນຈ້ຳສີແດງ
        FormatCellBorder("D2:F6", cStyle.DOTTED, 20, RGB(255, 0, 0))
    End Sub
  

ທ່ານສາມາດນຳໃຊ້ຄຸນສົມບັດດຽວກັນນີ້ໃນ Python ໄດ້:


    from uno import createUnoStruct
    from scriptforge import CreateScriptService
    
    def formatCellBorder(cellAddress, newStyle, newWidth, newColor=0):
        # ກຳນົດຮູບແບບເສັ້ນໃໝ່
        line_format = createUnoStruct("com.sun.star.table.BorderLine2")
        line_format.LineStyle = newStyle
        line_format.LineWidth = newWidth
        line_format.Color = newColor
        # ບໍລິການ Scriptforge ເພື່ອເຂົ້າເຖິງຊ່ວງເຊວ
        doc = CreateScriptService("Calc")
        cell = doc.XCellRange(cellAddress)
        cell.TopBorder = line_format
        cell.RightBorder = line_format
        cell.LeftBorder = line_format
        cell.BottomBorder = line_format
  

ສ່ວນຂອງໂຄ້ດລຸ່ມນີ້ສ້າງມາໂຄຣທີ່ຊື່ວ່າ myMacro ເຊິ່ງຈະຮຽກໃຊ້ 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))
  
ໄອຄອນ ບັນທຶກ

ໂຄ້ດ Python ທີ່ສະແດງຂ້າງເທິງໃຊ້ ຄັງໜັງສື ScriptForge ທີ່ມີໃຫ້ຕັ້ງແຕ່ LibreOffice 7.2 ເປັນຕົ້ນມາ.


ຮູບແບບເສັ້ນ

ຮູບແບບເສັ້ນຖືກກຳນົດເປັນຄ່າຄົງທີ່ຕົວເລກ. ຕາຕະລາງລຸ່ມນີ້ສະແດງຄ່າຄົງທີ່ສຳລັບຮູບແບບເສັ້ນທີ່ມີໃນ ຈັດຮູບແບບ - ເຊວ - ຂອບ:

ຊື່ຄ່າຄົງທີ່

ຄ່າຕົວເລກ

ຊື່ຮູບແບບເສັ້ນ

SOLID

0

ເສັ້ນທຶບ

DOTTED

1

ເສັ້ນຈ້ຳ

DASHED

2

ເສັ້ນຂີດ

FINE_DASHED

14

ເສັ້ນຂີດຖີ່

DOUBLE_THIN

15

ເສັ້ນຄູ່ບາງ

DASH_DOT

16

ຂີດຈ້ຳ

DASH_DOT_DOT

17

ຂີດຈ້ຳຈ້ຳ


ໄອຄອນ ເຄັດລັບ

ອ້າງອີງເຖິງ BorderLineStyle Constant Reference ໃນເອກະສານ LibreOffice API ເພື່ອຮຽນຮູ້ເພີ່ມເຕີມກ່ຽວກັບຄ່າຄົງທີ່ຂອງຮູບແບບເສັ້ນ.


ການຈັດຮູບແບບຂອບໂດຍໃຊ້ TableBorder2

ວັດຖຸຊ່ວງ (Range objects) ມີຄຸນສົມບັດທີ່ຊື່ວ່າ TableBorder2 ເຊິ່ງສາມາດໃຊ້ເພື່ອຈັດຮູບແບບຂອບຂອງຊ່ວງ ຄືກັນກັບທີ່ເຮັດໃນໄດອະລັອກ ຈັດຮູບແບບ - ເຊວ - ຂອບ ໃນສ່ວນ ການຈັດວາງເສັ້ນ.

ນອກຈາກຂອບເທິງ, ລຸ່ມ, ຊ້າຍ ແລະ ຂວາແລ້ວ, TableBorder2 ຍັງກຳນົດຂອບແນວຕັ້ງ ແລະ ແນວນອນນຳອີກ. ມາໂຄຣລຸ່ມນີ້ຈະນຳໃຊ້ສະເພາະຂອບເທິງ ແລະ ລຸ່ມ ໃຫ້ກັບຊ່ວງ "B2:E5".


    Sub TableBorder2Example
        Dim cStyle as Object
        Set cStyle = com.sun.star.table.BorderLineStyle
        ' ກຳນົດຮູບແບບເສັ້ນໃໝ່
        Dim lineFormat as New com.sun.star.table.BorderLine2
        lineFormat.LineStyle = cStyle.SOLID
        lineFormat.LineWidth = 15
        lineFormat.Color = RGB(0, 0, 0)
        ' ໂຄງສ້າງ (struct) ທີ່ເກັບການກຳນົດຄ່າ TableBorder2 ໃໝ່
        Dim tableFormat as New com.sun.star.table.TableBorder2
        tableFormat.TopLine = lineFormat
        tableFormat.BottomLine = lineFormat
        tableFormat.IsTopLineValid = True
        tableFormat.IsBottomLineValid = True
        ' ນຳໃຊ້ຮູບແບບຕາຕະລາງໃຫ້ກັບຊ່ວງ "B2:E5"
        Dim oCell as Object
        oCell = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("B2:E5")
        oCell.TableBorder2 = tableFormat
    End Sub
  

ມາໂຄຣນີ້ສາມາດນຳໃຊ້ໃນ Python ໄດ້ດັ່ງນີ້:


    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
  
ໄອຄອນ ເຄັດລັບ

ອ້າງອີງເຖິງ TableBorder2 Struct Reference ໃນເອກະສານ LibreOffice API ເພື່ອຮຽນຮູ້ເພີ່ມເຕີມກ່ຽວກັບຄຸນລັກສະນະຂອງມັນ.


ກະລຸນາສະໜັບສະໜູນພວກເຮົາ!

ກະລຸນາສະໜັບສະໜູນພວກເຮົາ!