【问题标题】:Resize and modify selected chart with libreoffice basic使用 libreoffice basic 调整和修改选定图表的大小
【发布时间】:2021-02-02 15:26:02
【问题描述】:

我正在使用 libreoffice basic 编写一个宏来修改我的电子表格中选定(且仅选定)图表的样式和大小。 经过多次文档阅读和多次尝试,我设法部分解决了我的问题:

修改所有图表样式

我设法通过它的索引选择了一个图表,并用这个宏修改了它的样式:

Sub ModifyChart
Dim oDoc As Object  
Dim oChart As Object
Dim aSize as new com.sun.star.awt.Size 
  oSheet = ThisComponent.sheets(0)
  oCharts = oSheet.getCharts()
  oChart = oCharts.getByIndex(0).getEmbeddedObject() 

  MsgBox oChart.ImplementationName
  oChart.Title.String = "My title"
  oChart.Title.CharColor = RGB(0,0,200)
  oChart.Title.CharFontName = "Arial"
  oChart.Title.CharHeight = 16

  oChart.SubTitle.String = "My subtitle"
  oChart.SubTitle.CharColor = RGB(0,0,200)
  oChart.SubTitle.CharFontName = "Arial"
  oChart.SubTitle.CharHeight = 12

  oChart.Diagram.Wall.FillStyle = com.sun.star.drawing.FillStyle.SOLID
  oChart.Diagram.Wall.FillColor = RGB(200,50,150) 
    
  oChart.Area.FillStyle = com.sun.star.drawing.FillStyle.SOLID
  oChart.Area.FillColor = RGB(100,50,250)
    
  aSize.Width=640
  aSize.Height=400

  oChart.setsize(aSize)  ' Error occur at this point
End Sub

问题:它不适用于所选图表,我无法调整它的大小。 setsize 方法不适用于此类对象。我试图获取图表的父对象(框架?或形状?)但没有成功。

ImplementationName,发现对象是com.sun.star.comp.sc.scshapeobj

修改所选图表大小

我设法使用这个宏修改了选定的图表大小

Sub ResizeChart
Dim oCurrSel As Object
Dim oItem As Object
Dim aSize As New com.sun.star.awt.Size
  oCurrSel = ThisComponent.getCurrentSelection()
  oItem = oCurrSel.getByIndex(0)
  MsgBox oItem.ImplementationName
  aSize.width=16000
  aSize.height=12000
  oItem.setsize(aSize)
End Sub

问题:我无法访问其他样式的图表。我试图找到一种方法来获取对象内容但没有成功。我还尝试使用oItem.dbg_propertiesoItem.dbg_methods 调查该对象,但没有发现任何有用的东西。

ImplementationName,发现对象是com.sun.star.comp.chart2.chartmodel

我查看了libreoffice api,但我没有找到这两种对象是如何连接的。

是否可以使用 libreoffice basic 实现我想要的功能?

谁能解释一下 libreoffice 图表对象(父母、孩子……)的层次结构以及如何处理它?

【问题讨论】:

    标签: charts resize libreoffice libreoffice-calc libreoffice-basic


    【解决方案1】:

    是的,你是绝对正确的 - 解析当前选择不是一项简单的任务。

    由于当前选择可能包含各种对象 - Cell、CellRange、SheetCellRanges、Shapes、ShapeCollection、GraphicObjectShape 甚至在某些情况下只是 Text,因此解析变得类似于游戏“Miner” - 每个下一步都需要额外的检查 (或在 On Error Resume Next 的帮助下“盲目地”处理错误)

    您使用 ImplementationName 来识别对象的想法总体上是好的。但是4.1. Debugging And Inspecting Macros 的 Andrew Pitonyak 写道 “要确定文档类型,请查看它支持的服务......我认为这比使用 getImplementationName() 更安全” 我倾向于相信他。

    从当前选择到嵌入图表的转换可以是这样的:

    Sub ModifyChartInCurrentSelection
    Dim oCurrentSelection As Variant
    Dim i As Long
    Dim oNextElementOfSelection As Variant
    Dim oEmbeddedObject As Variant
    Dim oComponent As Variant
    Dim aSize As New com.sun.star.awt.Size
        oCurrentSelection = ThisComponent.getCurrentSelection()
        If oCurrentSelection.supportsService("com.sun.star.drawing.ShapeCollection") Then
            For i = 0 To oCurrentSelection.getCount()-1
                oNextElementOfSelection = oCurrentSelection.getByIndex(i)
                If oNextElementOfSelection.supportsService("com.sun.star.drawing.OLE2Shape") Then
    Rem Size of shape (outer wrapper around the chart)
                    If oNextElementOfSelection.supportsService("com.sun.star.drawing.Shape") Then
                        aSize = oNextElementOfSelection.getSize()
                        aSize.Height = aSize.Height * 2 ' or any other
                        aSize.Width = aSize.Width / 2
                        oNextElementOfSelection.setSize(aSize)
                    EndIf 
    Rem Properties of EmbeddedObject
                    oEmbeddedObject = oNextElementOfSelection.EmbeddedObject
                    If Not IsEmpty(oEmbeddedObject) Then
                        oComponent = oEmbeddedObject.getComponent()
                        oComponent.getTitle().String = "Foo-bar Foo-bar Foo-bar"
    Rem and other settings...
                    EndIf 
                EndIf 
            Next i
        EndIf 
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多