Jump to content

マクロ/Writer/003

From The Document Foundation Wiki
This page is a translated version of the page Macros/Writer/003 and the translation is 100% complete.


説明

このマクロは、表全体または単一のセルの罫線を操作します。

:スタイルを使用して枠線を操作する方が効率的な場合があります。https://ask.libreoffice.org/t/how-to-set-textframe-borders-at-once/108826 を参照してください。

表の罫線には、プロパティ com.sun.star.table.TableBorder[1] からアクセスできます。

この構造体の要素には 2 つのタイプがあります。

  • 境界線(上、下、左、右、水平、垂直)。境界線は、構造体 com.sun.star.table.BorderLine2[2] であり、構造体 com.sun.star.table.BorderLine[3] にスタイル要素 [4] を「追加」したものです。
  • ブール値(IsLeftLineValid、IsRightLineValid、IsTopLineValid など)。記述時に、これらをTrueとして定義すると、それに応じて線が変更され、Falseとして定義すると、無視されます。つまり、現在の値が保持されます。

コンテンツとの間隔の定義は、テーブルのプロパティ com.sun.star.table.TableBorderDistances[5] で定義されます。この構造体も、同様の方法で操作され、BottomDistance、LeftDistance など と 名前を変更するためのブール値が使用されます。

次のマクロは、現在選択されているテーブルに対して実行されます。このマクロでは、次のことを行います。

  • まず、カーソルが実際にテーブル内にあることを確認します。
  • 現在の境界線(パラメータ Is...True として定義されています)を置き換えて、外周と水平の二重線を取得します。縦線は必要ありません。したがって、す。.IsVerticalLineValid = true VerticalLine = LibONoLine が必要で、行に "empty" を適用するために定義された構造体です。現在のテーブルには縦線が定義されている可能性があり、それらを抑制する必要があるため、単純に .IsVerticalLineValid = false を使用することはできません。パラメータ false を使用すると、既存の行は現在の状態のままになります。
  • 調整する枠幅は 0.30 cm とします。
  • パラメータ Fuse the adjacent line styles は、テーブルのプロパティ CollapsingBorders で定義されます。

Code

境界線の距離と幅は1/100 mmで表されます。

Option Explicit


Sub Main()
    '''不要なサブルーチンをコメントアウトして、2つのサブルーチンのどちらを呼び出すかを選択します。'''

    'AddBordersToAllCells
    _AddBordersToCell("Table1","B2")

End Sub ' Main


Sub AddBordersToAllCells()
    ''' 現在選択されている表のすべてのセルに二重罫線を追加する'''

    Dim oCursor As Object
    Dim oTable As Object
    Dim oTableBorder As Object

    Dim oLine As New com.sun.star.table.BorderLine2
    Dim oNoLine As New com.sun.star.table.BorderLine2
    Dim oBorderDistances As New com.sun.star.table.TableBorderDistances
    oCursor = ThisComponent.CurrentController.ViewCursor

    If (isEmpty(oCursor.textTable)) Then
        MsgBox "カーソルがテーブル内にありません。", _
            MB_ICONINFORMATION, _
            "AddBordersToAllCells()からの警告"
    Else
        With oLine
            .Color = 0
            .InnerLineWidth = 10
            .LineDistance = 60 
            .LineStyle = com.sun.star.table.BorderLineStyle.DOUBLE 
            .LineWidth = 5
            .OuterLineWidth = 10
        End With
        With oNoLine
            .Color = 0
            .InnerLineWidth = 0
            .LineDistance = 0
            .LineStyle = 0
            .LineWidth = 0
            .OuterLineWidth = 0
        End With
        With oBorderDistances
            .BottomDistance = 300
            .IsBottomDistanceValid = True   
            .IsLeftDistanceValid = True   
            .IsRightDistanceValid = True   
            .IsTopDistanceValid = True   
            .LeftDistance = 300   
            .RightDistance = 300
            .TopDistance = 300
        End With
        
        oTable = oCursor.TextTable
        oTableBorder = oTable.TableBorder
        
        With oTableBorder        
            .IsLeftLineValid = true
            .IsRightLineValid = true
            .IsTopLineValid = true
            .IsBottomLineValid = true
            .IsHorizontalLineValid = true
            .IsVerticalLineValid = true
            .LeftLine = oLine
            .RightLine = oLine
            .TopLine = oLine
            .BottomLine = oLine 
            .HorizontalLine = oLine
            .VerticalLine = oNoLine
        End With
        
        With oTable
            .TableBorder = oTableBorder 
            .HoriOrient = com.sun.star.text.HoriOrientation.FULL 
            .CollapsingBorders = true
            .TableBorderDistances = oBorderDistances
        End With  

    End If

End Sub ' AddBordersToAllCells


Private Sub _AddBordersToCell(myTable As String, myCell As String)
    '''指定した表の指定したセルに二重罫線を追加する'''

    Dim oTable As Object
    Dim oCell As Object
    Dim oLine As New com.sun.star.table.BorderLine2

    With oLine
        .Color = 0
        .InnerLineWidth = 10
        .LineDistance = 60 
        .LineStyle = com.sun.star.table.BorderLineStyle.DOUBLE
        .LineWidth = 2
        .OuterLineWidth = 10
    End With

    oTable = ThisComponent.TextTables.getByName(myTable)
    oCell = oTable.getCellByName(myCell)

    With oCell
        .LeftBorder = oLine
        .LeftBorderDistance = 100
        .RightBorder = oLine
        .RightBorderDistance = 100
        .TopBorder = oLine
        .TopBorderDistance = 100
        .BottomBorder = oLine 
        .BottomBorderDistance = 100
    End With

End Sub ' AddBordersToCell
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from com.sun.star.table import BorderLine2
from com.sun.star.table import TableBorderDistances
from com.sun.star.table.BorderLineStyle import DOUBLE
import uno
import msgbox
from com.sun.star.text.HoriOrientation import FULL

def _msgbox(prompt='', title='LibreOffice'):
    """ メッセージを含むダイアログボックスを表示し、値を返します。"""
    mb = msgbox.MsgBox(uno.getComponentContext())
    mb.addButton('OK')
    mb.show(prompt, 0, title)


def AddBordersToAllCells():
    """現在選択されている表のすべてのセルに二重罫線を追加する"""
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
    view_cursor = model.getCurrentController()
    cursor = view_cursor.getViewCursor()
    line = BorderLine2()
    no_line = BorderLine2()
    border_distances = TableBorderDistances()
    if cursor.TextTable is None:
        _msgbox("カーソルがテーブル内にありません。", "Warning from AddBordersToAllCells()")
    else:
        line.Color = 0
        line.InnerLineWidth = 10
        line.LineDistance = 60
        line.LineStyle = DOUBLE
        line.LineWidth = 5
        line.OuterLineWidth = 10

        no_line.Color = 0
        no_line.InnerLineWidth = 0
        no_line.LineDistance = 0
        no_line.LineStyle = 0
        no_line.LineWidth = 0
        no_line.OuterLineWidth = 0

        border_distances.BottomDistance = 300
        border_distances.IsBottomDistanceValid = True
        border_distances.IsLeftDistanceValid = True
        border_distances.IsRightDistanceValid = True
        border_distances.IsTopDistanceValid = True
        border_distances.LeftDistance = 300
        border_distances.RightDistance = 300
        border_distances.TopDistance = 300

        table = cursor.TextTable
        table_border = table.TableBorder
        table_border.IsLeftLineValid = True
        table_border.IsRightLineValid = True
        table_border.IsTopLineValid = True
        table_border.IsBottomLineValid = True
        table_border.IsHorizontalLineValid = True
        table_border.IsVerticalLineValid = True
        table_border.LeftLine = line
        table_border.RightLine = line
        table_border.TopLine = line
        table_border.BottomLine = line
        table_border.HorizontalLine = line
        table_border.VerticalLine = no_line

        table.TableBorder = table_border
        table.CollapsingBorders = True
        table.TableBorderDistances = border_distances


def Main():
    _AddBordersToCell("Table1", "B2")


def _AddBordersToCell(my_table, my_cell):
    """指定した表の指定したセルに二重罫線を追加する"""
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()

    line = BorderLine2()
    line.Color = 0
    line.InnerLineWidth = 10
    line.LineDistance = 60
    line.LineStyle = DOUBLE
    line.LineWidth = 2
    line.OuterLineWidth = 10

    table = model.TextTables.getByName(my_table)
    cell = table.getCellByName(my_cell)

    cell.LeftBorder = line
    cell.LeftBorderDistance = 100
    cell.RightBorder = line
    cell.RightBorderDistance = 100
    cell.TopBorder = line
    cell.TopBorderDistance = 100
    cell.BottomBorder = line
    cell.BottomBorderDistance = 100


g_exportedScripts = (AddBordersToAllCells, Main)

マクロテスト用の Writerファイル

メモ