OpenOffice的Calc的宏观添加饼图(OpenOffice Calc macro to ad

2019-10-20 08:35发布

我想插入使用宏开办公室饼图。 但代码显示错误:

线:

Dim oDiagram As New com.sun.star.chart.PieDiagram

错误:

"Object not accessible. Invalid reference."

我无法找出原因。 请帮助。 这里是我完整的宏代码:

Sub Macro1

    Dim oRange as Object
    Dim oRangeAddress(1) As New com.sun.star.table.CellRangeAddress
    Dim oDiagram As New com.sun.star.chart.PieDiagram
    Dim oRect As New com.sun.star.awt.Rectangle
    Dim cTitle as String

    oRange = thisComponent.getCurrentSelection.getRangeAddress
    oSheets = ThisComponent.getSheets()
    oSheet = oSheets.getByIndex(0)
    oCharts = oSheet.Charts

    oRect.Width = 10000
    oRect.Height = 10000
    oRect.X = 8000
    oRect.Y = 1000

    oRangeAddress(0).Sheet = oRange.Sheet
    oRangeAddress(0).StartColumn = 0
    oRangeAddress(0).StartRow = 0
    oRangeAddress(0).EndColumn = 1
    oRangeAddress(0).EndRow = 2

    cTitle = "Test Results"
    oCharts.addNewByName(cTitle,oRect,oRangeAddress(),TRUE, TRUE)
    oChart = oCharts.getByName(cTitle).embeddedObject
    oChart.Diagram = oDiagram
    oChart.HasMainTitle = True
    oChart.Title.String = cTitle

End Sub

下面是输入纸张数据:

Answer 1:

你不能实例化一个com.sun.star.chart.PieDiagram直接的,独立的已经存在的图表。 相反,你必须先创建图表,然后创建一个PieDiagram 。 因此,为了使宏观工作,做到以下几点:

  • 删除行Dim oDiagram As New com.sun.star.chart.PieDiagram
  • 更改行oChart.Diagram = oDiagramoChart.Diagram = oChart.createInstance("com.sun.star.chart.PieDiagram")

这导致了下面的代码(我用Win7上OpenOffice.org Calc中4.1.0测试此):

Sub Macro1

    Dim oRange as Object
    Dim oRangeAddress(1) As New com.sun.star.table.CellRangeAddress
    Dim oRect As New com.sun.star.awt.Rectangle
    Dim cTitle as String

    oRange = thisComponent.getCurrentSelection.getRangeAddress
    oSheets = ThisComponent.getSheets()
    oSheet = oSheets.getByIndex(0)
    oCharts = oSheet.Charts

    oRect.Width = 10000
    oRect.Height = 10000
    oRect.X = 8000
    oRect.Y = 1000

    oRangeAddress(0).Sheet = oRange.Sheet
    oRangeAddress(0).StartColumn = 0
    oRangeAddress(0).StartRow = 0
    oRangeAddress(0).EndColumn = 1
    oRangeAddress(0).EndRow = 2

    cTitle = "Test Results"
    oCharts.addNewByName(cTitle,oRect,oRangeAddress(),TRUE, TRUE)
    oChart = oCharts.getByName(cTitle).embeddedObject
    oChart.Diagram = oChart.createInstance("com.sun.star.chart.PieDiagram")
    oChart.HasMainTitle = True
    oChart.Title.String = cTitle

End Sub

运行宏应给予以下结果:

注:宏将不在的LibreOffice Calc的运行 ; 它仅适用于OpenOffice.org Calc中。 我认为这是一个错误的LibreOffice,因为我coudn't找到的OOo和LO的API中的任何差异。



文章来源: OpenOffice Calc macro to add pie chart