Exporting a Diagram/Chart from a Spreadsheet

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
oliziegle
Posts: 1
Joined: Thu Nov 03, 2016 6:52 pm

Exporting a Diagram/Chart from a Spreadsheet

Post by oliziegle »

Dear all,

I'm currently working on program which fills a .ods-file with xml-data. From this data a chart is generated. Now I want to export the chart/diagram to png/jpeg by using the java uno api.

So far my efforts are this:

Code: Select all

		XComponent document = xComponentLoader.loadComponentFromURL("private:stream/scalc", "_blank", 0,
				conversionProperties);

		XSpreadsheetDocument xSpreadsheetDocument = UnoRuntime.queryInterface(XSpreadsheetDocument.class, document);
		XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();

		Object sheet = xSpreadsheets.getByName("Chart");
		XSpreadsheet xSpreadsheet = UnoRuntime.queryInterface(XSpreadsheet.class, sheet);

		XTableChartsSupplier xTableChartsSupplier = UnoRuntime.queryInterface(XTableChartsSupplier.class, xSpreadsheet);
		Object chart = xTableChartsSupplier.getCharts()
				.getByName(xTableChartsSupplier.getCharts().getElementNames()[0]);
		XTableChart xTableChart = UnoRuntime.queryInterface(XTableChart.class, chart);

		XEmbeddedObjectSupplier xEmbeddedObjectSupplier = UnoRuntime.queryInterface(XEmbeddedObjectSupplier.class,
				xTableChart);

		XInterface xInterface = xEmbeddedObjectSupplier.getEmbeddedObject();
		XChartDocument xChart = UnoRuntime.queryInterface(XChartDocument.class, xInterface);

		Object oGraphicExportFilter = xComponentContext.getServiceManager()
				.createInstanceWithContext("com.sun.star.drawing.GraphicExportFilter", xComponentContext);
		XExporter xExporter = UnoRuntime.queryInterface(XExporter.class, oGraphicExportFilter);

		XComponent xComp = UnoRuntime.queryInterface(XComponent.class, xChart.getDiagram());
		xExporter.setSourceDocument(xChart);
               // xExporter.setSourceDocument(xComp); isn't working either
I'm trying to export it with the XExporter and XFilter, but I'm stuck at

Code: Select all

xExporter.setSourceDocument(xChart);
It says:

Code: Select all

com.sun.star.lang.IllegalArgumentException: 
	at com.sun.star.lib.uno.environments.remote.Job.remoteUnoRequestRaisedException(Job.java:173)
	at com.sun.star.lib.uno.environments.remote.Job.execute(Job.java:139)
	at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:334)
	at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:303)
	at com.sun.star.lib.uno.environments.remote.JavaThreadPool.enter(JavaThreadPool.java:87)
	at com.sun.star.lib.uno.bridges.java_remote.java_remote_bridge.sendRequest(java_remote_bridge.java:636)
	at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.request(ProxyFactory.java:146)
	at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.invoke(ProxyFactory.java:128)
	at com.sun.proxy.$Proxy14.setSourceDocument(Unknown Source)
Is there another way or do I miss something.

Thanks for helping in advance.
Regards,
Oliver
OpenOffice 4.1, Ubuntu
UnklDonald418
Volunteer
Posts: 1544
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: Exporting a Diagram/Chart from a Spreadsheet

Post by UnklDonald418 »

Since no one else has responded to this, here is Basic code to save a Calc chart as a .png and also a .jpg file

Code: Select all

REM  *****  BASIC  *****

Sub SaveChartAsImage

oFilter=CreateUnoService("com.sun.star.drawing.GraphicExportFilter")
Dim args(1) As New com.sun.star.beans.PropertyValue
Dim oDoc as object
Dim oPage as object
Dim oChart as object

oDoc = ThisComponent
oPage=oDoc.drawPages.getByIndex(0)
oChart = oPage.getByIndex(0)
oFilter.setSourceDocument(oChart)

'Save as png file
args(0).Name = "URL"
args(0).Value = "file:///f:/Test.PNG"
args(1).Name = "MediaType"
args(1).Value = "image/png"   'Mime type from www.sitepoint.com/web-foundations/mime-types-summary-list/
oFilter.filter(args())

'Now as jpg 
args(0).Value = "file:///f:/Test.JPG"
args(1).Value = "image/jpeg"  'Mime type from www.sitepoint.com/web-foundations/mime-types-summary-list/ 
oFilter.filter(args())

End Sub
To use setSourceDocument(Chart) you need to get the Chart object from the Drawpages not the Sheet.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
User avatar
einstein
Posts: 47
Joined: Sat Nov 05, 2016 1:45 am
Location: State of Mexico, México.

Re: Exporting a Diagram/Chart from a Spreadsheet

Post by einstein »

Very nice code UnklDonald418
oliziegle wrote:I'm trying to export it with the XExporter and XFilter, but I'm stuck at
xExporter.setSourceDocument(xChart);
It can be a bug in Java. Here "similar" topic:
viewtopic.php?f=20&t=82965
lo 5.1.6.2 | aoo 4.1.3 | win 7/10
All I know is that I know nothing
UnklDonald418
Volunteer
Posts: 1544
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: Exporting a Diagram/Chart from a Spreadsheet

Post by UnklDonald418 »

I don't think there is a bug here, just a case of using the wrong object.
I'm not a Java programmer but looking at the code provided it appears to me that the the xchart object is obtained using getSheets().
When I use MRI in my code and inspect a chart object obtained from getSheets() it does not have the method setSourceDocument() so of course it would generate an exception.
However, when I obtain the chart object from DrawPages it does have the setSourceDocument() method and I can successfully save the first Chart on the first Page(Sheet1) to a graphics file. Actually I have another version of the macro that has 2 nested loops that saves every chart(and graphic) from every page of a spreadsheet to graphics files.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
Post Reply