libreoffice Basic:draw 和 tableshape:如何格式化单元格?

问题描述

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

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

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 )
    cellule.getText().setString( s )
    cellule.CharFontName = fonte
    cellule.CharHeight = hauteurChar
    cellule.TextHorizontalAdjust = com.sun.star.drawing.TextHorizontalAdjust.CENTER
end sub

sub completeCelluleR( 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,"Poids (kg)" )
    call completeCelluleC( 3,"Prix au kg" )
    call completeCelluleC( 4,"Prix TTC" )
    
    ' ligne 1
    call completeCelluleC( 0,1,1 )
    call completeCellule( 1,"chapon(s) poulet" )
    call completeCelluleR( 2,3.22 )
    call completeCelluleR( 3,14 )
    call completeCelluleR( 4,45.08 )
    
    ' ligne 2
    call completeCelluleC( 0,2,"poularde(s)" )
    call completeCelluleR( 2,2.2 )
    call completeCelluleR( 3,15 )
    call completeCelluleR( 4,35.52 )
    
    ' ligne 3
    call completeCelluleC( 0,3,0 )
    call completeCellule( 1,"chapon(s) pintade" )
    call completeCelluleR( 2,0 )
    call completeCelluleR( 3,0 )
    call completeCelluleR( 4,0 )
    
    ' prix total TTC
    call completeCellule( 3,5,"Total TTC" )
    call completeCelluleR( 4,80.60 )
    
    ' total en francs
    call completeCellule( 3,6,"en francs :" )
    call completeCelluleR( 4,528.70 )
    
end sub

我已经放置了诸如 cellule.TextHorizontalAdjust = com.sun.star.drawing.TextHorizontalAdjust.RIGHT 之类的指令来尝试使文本居中,但它不起作用:在使用调试器检查时,TextHorizontalAdjust 属性会更改值,但文本不会更改文本中的位置单元格。

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

谢谢!

解决方法

基于 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","",Array())

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

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