OpenOffice Calc宏添加饼图 [英] OpenOffice Calc macro to add pie chart
问题描述
我正在尝试使用宏在开放式办公室中插入一个饼图。但是代码显示错误:
I am trying to insert a piechart in open-office using macro. But the code shows error:
行:
将oDiagram用作新的com.sun.star.chart.PieDiagram
错误:
对象无法访问。引用无效。
我不知道为什么。请帮助。这是我完整的宏代码:
I am unable to figure out why. Kindly help. Here is my complete macro code:
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
以下是输入工作表数据:
Here is the input sheet data:
推荐答案
您不能直接实例化 com.sun.star.chart.PieDiagram
,而与现有图表无关。相反,您必须首先创建图表,然后创建 PieDiagram
。因此,要使宏起作用,请执行以下操作:
You can't instantiate a com.sun.star.chart.PieDiagram
directly, independent of an already-existing chart. Instead, you'll have to create the chart first, and then create a PieDiagram
. Thus, to make the macro work, do the following:
- 将行
Dim oDiagram删除为New com.sun .star.chart.PieDiagram
- 将行
oChart.Diagram = oDiagram
更改为oChart.Diagram = oChart.createInstance( com.sun.star.chart.PieDiagram)
。
- remove the line
Dim oDiagram As New com.sun.star.chart.PieDiagram
- change the line
oChart.Diagram = oDiagram
tooChart.Diagram = oChart.createInstance("com.sun.star.chart.PieDiagram")
.
这将导致以下代码(我已经在Win7上使用OpenOffice.org Calc 4.1.0对其进行了测试):
This results in the following code (i've tested this with OpenOffice.org Calc 4.1.0 on Win7):
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
运行宏应给出以下结果:
Running the macro should give the following result:
NB:宏不会在LibreOffice Calc上运行;它仅适用于OpenOffice.org Calc。我认为这是一个LibreOffice错误,因为我在OOo和LO的API中找不到任何差异。
NB: The macro won't run on LibreOffice Calc; it only works with OpenOffice.org Calc. I think this a LibreOffice bug, since i coudn't find any differences in the API of OOo and LO.
这篇关于OpenOffice Calc宏添加饼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!