【问题标题】:OpenOffice Calc macro to add pie chartOpenOffice Calc 宏添加饼图
【发布时间】:2014-06-24 15:00:33
【问题描述】:

我正在尝试使用宏在开放式办公室中插入饼图。但是代码显示错误:

行:

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

这是输入的工作表数据:

【问题讨论】:

  • AFAIK 您无法使用New com.sun.star.chart.PieDiagram 实例化新图表。相反,您可以只使用Dim oChart as Object。但是现在,oCharts.getByName(cTitle).embeddedObject 抛出了 NoSuchElementException - 我不知道为什么,sample code from OOo Wiki 也会发生同样的情况... :-(
  • 那如何添加饼图呢?
  • 我添加了一个使用 OOo.Calc(但不使用 LIbreOffice)的示例的答案。
  • 在 4.3.0 版中,问题 seems to be solved - 至少关于 OOo Wiki 中的简单代码。

标签: macros charts openoffice-calc basic


【解决方案1】:

您不能独立于现有图表直接实例化com.sun.star.chart.PieDiagram。相反,您必须先创建图表,然后再创建PieDiagram。因此,要使宏工作,请执行以下操作:

  • 删除Dim oDiagram As New com.sun.star.chart.PieDiagram这一行
  • oChart.Diagram = oDiagram 行更改为oChart.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 错误,因为我没有发现 OOo 和 LO 的 API 有任何差异。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多