I’d like to be able to create a chart programmatically. My macro code:
Sub Main
Dim Doc As Object, Sheet As Object, Charts As Object, Chart As Object
Dim Source(0) As New com.sun.star.table.CellRangeAddress
Doc = ThisComponent
Sheet = Doc.Sheets.getByName("Sheet1")
Const ChartH = 3, ChartW = 3, ChartName = "Test1"
Charts = Sheet.Charts
'Chart = Charts.getByName(ChartName)
'Chart = Charts(ChartName)
'selection of data
Source(0).Sheet = 0
Source(0).StartColumn = 0
Source(0).StartRow = 0
Source(0).EndColumn = ChartW - 1
Source(0).EndRow = ChartH - 1
oDataRanges = Doc.createInstance("com.sun.star.sheet.SheetCellRanges")
oDataRanges.addRangeAddress(Source(0), False)
'If IsNull(Chart) Then
'If not Charts.hasByName(ChartName) Then
If not Charts.hasElements Then
'if there is no chart, create one
Dim Rect As new com.sun.star.awt.Rectangle
Rect.X = 15000
Rect.Y = 1000
Rect.Width = 12000
Rect.Height = 6800
'Charts.addNewByName ChartName, Rect, Source, True, True
Charts.addNewByName ChartName, Rect, oDataRanges.RangeAddresses, False, False
'Doc.calculateAll
'Chart = Charts.getByName(ChartName)
Chart = Charts(0)
Else
'Chart = Charts.getByName(ChartName)
Chart = Charts(0)
'Chart.setRanges Source
Chart.setRanges oDataRanges.RangeAddresses
EndIf
End Sub
When I try to run in LO Basic, first it raises an exception, on the line Charts.addNewByName…:
BASIC runtime error.
An exception occurred
Type: com.sun.star.lang.IllegalArgumentException
Message: .
The next time I try, the addNewByName call silently fails, and Charts(0) fails with the following message:
Inadmissible value or data type.
Index out of defined range.
If I try using Charts.getByName instead of Charts(0), it also fails:
BASIC runtime error.
An exception occurred
Type: com.sun.star.container.NoSuchElementException
Message: .
No chart is created anyway.
Version: 7.2.1.2 (x64) / LibreOffice Community
Build ID: 87b77fad49947c1441b67c559c339af8f3517e22
CPU threads: 4; OS: Windows 10.0 Build 19043; UI render: Skia/Raster; VCL: win
Locale: en-US (en_US); UI: en-US
Calc: threaded