首页 > 解决方案 > libreoffice Basic:绘图和表格形状:如何格式化单元格?

问题描述

我正在尝试使用基本宏来绘制以创建包含表格的页面。

到目前为止,我的代码是这样的:

sub completeCellule( x&, y&, s$ )
    fonte = "Times New Roman"
    hauteurChar = 10
    
    ' titres des colonnes
    cellule = table.Model.getCellByPosition( x, y )
    cellule.getText().setString( s )
    cellule.CharFontName = fonte
    cellule.CharHeight = hauteurChar
end sub

sub completeCelluleC( x&, y&, s$ )
    fonte = "Times New Roman"
    hauteurChar = 10
    
    ' titres des colonnes
    cellule = table.Model.getCellByPosition( x, y )
    cellule.getText().setString( s )
    cellule.CharFontName = fonte
    cellule.CharHeight = hauteurChar
    cellule.TextHorizontalAdjust = com.sun.star.drawing.TextHorizontalAdjust.CENTER
end sub

sub completeCelluleR( x&, y&, s$ )
    fonte = "Times New Roman"
    hauteurChar = 10
    
    ' titres des colonnes
    cellule = table.Model.getCellByPosition( x, y )
    cellule.getText().setString( s )
    cellule.CharFontName = fonte
    cellule.CharHeight = hauteurChar
    cellule.TextHorizontalAdjust = com.sun.star.drawing.TextHorizontalAdjust.RIGHT
end sub

sub testTable
    oDoc = ThisComponent
    page = oDoc.getDrawPages().getByIndex(0)
    ' 14.85 cm -> 148.5 mm -> 148500 1/100 mm
    page.width = 14850
    page.height = 10500
    page.borderLeft = 500
    page.borderTop = 500
    page.borderRight = 500
    page.borderBottom = 500
    
    page.orientation = com.sun.star.view.PaperOrientation.LANDSCAPE
    
    table = ThisComponent.createInstance("com.sun.star.drawing.TableShape")
    page.add(table)
    
    ' nombre de lignes et de colonnes
    table.Model.Rows.insertByIndex(0,6)
    table.Model.Columns.insertByIndex(0,4)
    
    ' taille et position de la table
    table.setSize( createSize(10000, 5600) )
    table.setPosition( createPoint( 500, 500 ) )
    
    fonte = "Times New Roman"
    hauteurChar = 10
    
    ' titres des colonnes
    call completeCelluleC( 2, 0, "Poids (kg)" )
    call completeCelluleC( 3, 0, "Prix au kg" )
    call completeCelluleC( 4, 0, "Prix TTC" )
    
    ' ligne 1
    call completeCelluleC( 0, 1, 1 )
    call completeCellule( 1, 1, "chapon(s) poulet" )
    call completeCelluleR( 2, 1, 3.22 )
    call completeCelluleR( 3, 1, 14 )
    call completeCelluleR( 4, 1, 45.08 )
    
    ' ligne 2
    call completeCelluleC( 0, 2, 1 )
    call completeCellule( 1, 2, "poularde(s)" )
    call completeCelluleR( 2, 2, 2.2 )
    call completeCelluleR( 3, 2, 15 )
    call completeCelluleR( 4, 2, 35.52 )
    
    ' ligne 3
    call completeCelluleC( 0, 3, 0 )
    call completeCellule( 1, 3, "chapon(s) pintade" )
    call completeCelluleR( 2, 3, 0 )
    call completeCelluleR( 3, 3, 0 )
    call completeCelluleR( 4, 3, 0 )
    
    ' prix total TTC
    call completeCellule( 3, 5, "Total TTC" )
    call completeCelluleR( 4, 5, 80.60 )
    
    ' total en francs
    call completeCellule( 3, 6, "en francs :" )
    call completeCelluleR( 4, 6, 528.70 )
    
end sub

我已经提出cellule.TextHorizontalAdjust = com.sun.star.drawing.TextHorizontalAdjust.RIGHT了尝试将文本居中的说明,但它不起作用:在使用调试器检查时,TextHorizontalAdjust属性会更改值,但文本不会更改单元格中的位置。

我必须改变什么才能得到它?当我这样做时,如何让单元格调整行和列大小以适合其内容(与 Ca​​lc 中的“最佳宽度/高度”功能相同)。

谢谢!

标签: drawlibreofficebasic

解决方案


根据https://stackoverflow.com/a/59968723/5100564的答案:

cellule.setPropertyValue("ParaAdjust", com.sun.star.style.ParagraphAdjust.CENTER)

至于您问题的另一部分,调度员可以设置最佳列宽。

oFrame = ThisComponent.CurrentController.Frame
oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
oDispatcher.executeDispatch(oFrame, ".uno:SetOptimalColumnWidth", "", 0, Array())

为了让后面的代码正常工作,我手动选择了这些列。要自动选择列,以下内容在 Writer 中对我有用,但在我在这里尝试时表现不一致。

columns = 5
rows = 2
oCellsRange = oTable.Model.getCellRangeByPosition(0, 0, columns - 1, rows - 1)
oController.select(oCellsRange)

推荐阅读