OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Export all charts from a Calc spreadsheet to EPS

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
jrfonseca
Newbie
Newbie


Joined: 26 Jul 2007
Posts: 3

PostPosted: Thu Jul 26, 2007 6:02 pm    Post subject: Export all charts from a Calc spreadsheet to EPS Reply with quote

I needed to export all chars in a Calc spreadsheet to EPS to include a LaTeX report, so I wrote this macro based on snippets from other posts.

Code:
' Export all charts from a Calc spreadsheet -- Jose Fonseca

Sub Main
   Dim oDoc, oDocCtrl, oDocFrame, oDispatchHelper
   oDoc = ThisComponent
   oDocCtrl = oDoc.getCurrentController()
   oDocFrame = oDocCtrl.getFrame()
   oDispatchHelper = createUnoService( "com.sun.star.frame.DispatchHelper" )
 
   Dim storeUrl
   storeUrl = oDoc.getURL()
   storeUrl = Left( storeUrl, Len( storeUrl ) - 4 )

   nCharts = 0
   
   ' Search the draw page for the chart.
   Dim oSheets, oSheet, oDrawPage, oShape
   oSheets = oDoc.getSheets()
   For i = 0 to oSheets.getCount() - 1
      oSheet = oSheets.getByIndex( i )
      oDrawPage = oSheet.getDrawPage()
      For j = 0 to oDrawPage.getCount() - 1
         oShape = oDrawPage.getByIndex( j )
         ' Can't call supportsService unless the com.sun.star.lang.XServiceInfo is present.
         If HasUnoInterfaces( oShape, "com.sun.star.lang.XServiceInfo" ) Then
            If oShape.supportsService( "com.sun.star.drawing.OLE2Shape" ) Then
               ' Is it a Chart?
               If oShape.CLSID = "12DCAE26-281F-416F-a234-c3086127382e" Then
                  ' Select the chart shape.
                  oDocCtrl.select( oShape )
                  oDispatchHelper.executeDispatch( oDocFrame, ".uno:Copy", "", 0, Array() )
                  ' export the chart
                  nCharts = nCharts + 1
                  ExportSelection( storeUrl + "_chart" + nCharts + ".eps", "image/x-eps" )
               EndIf
            EndIf
         EndIf
      Next
   Next
End Sub

Sub ExportSelection(url As String, mediaType As String)
   ' Create a new Draw document
   Dim aArgs(1) As New com.sun.star.beans.PropertyValue
   aArgs(0).Name = "Hidden"
   aArgs(0).Value = True
   oDrawDoc = StarDesktop.loadComponentFromURL( "private:factory/sdraw", "_blank", 0, aArgs() )
   
   ' Past current selection
   Dim oDrawDocCtrl, oDrawDocFrame, oDispatchHelper
   oDrawDocCtrl = oDrawDoc.getCurrentController()
   oDrawDocFrame = oDrawDocCtrl.getFrame()
   oDispatchHelper = createUnoService( "com.sun.star.frame.DispatchHelper" )
   oDispatchHelper.executeDispatch( oDrawDocFrame, ".uno:Paste", "", 0, Array() )
     
   ' Get an export filter object
   Dim exportFilter
   exportFilter = createUnoService( "com.sun.star.drawing.GraphicExportFilter" )

   ' get first draw page
   Dim oDrawPages, oDrawPage, oShape
   oDrawPages = oDrawDoc.getDrawPages()
   oDrawPage = oDrawPages.getByIndex( 0 )
   oShape = oDrawPage.getByIndex( 0 )
   exportFilter.setSourceDocument( oShape )
 
   ' Set the filter data
   Dim aFilterData(5) As New com.sun.star.beans.PropertyValue
   aFilterData(0).Name = "Level" '1=PS level 1, 2=PS level 2
   aFilterData(0).Value = 2
   aFilterData(1).Name = "ColorFormat" '1=color, 2=grayscale
   aFilterData(1).Value = 1
   aFilterData(2).Name = "TextMode" '0=glyph outlines, 1=no glyph outlines, see ooo bug 7918
   aFilterData(2).Value = 1
   aFilterData(3).Name = "Preview" '0=none, 1=TIFF, 2=EPSI, 3=TIFF+EPSI
   aFilterData(3).Value = 0
   aFilterData(4).Name = "CompressionMode" '1=LZW, 2=none
   aFilterData(4).Value = 2
   
   Dim aProps(2) As New com.sun.star.beans.PropertyValue
   aProps(0).Name = "MediaType"
   aProps(0).Value = mediaType
   aProps(1).Name = "URL"
   aProps(1).Value = url
   aProps(2).Name = "FilterData"
   aProps(2).Value = aFilterData()
   
   exportFilter.filter( aProps() )
End Sub


For more information, see also:
Back to top
View user's profile Send private message
sanderbakkes
Newbie
Newbie


Joined: 13 Jun 2008
Posts: 1

PostPosted: Fri Jun 13, 2008 5:31 am    Post subject: Reply with quote

i registered just to say that you are a hero, thanks man!
Back to top
View user's profile Send private message
alexis779
Newbie
Newbie


Joined: 31 Aug 2008
Posts: 2

PostPosted: Sun Aug 31, 2008 6:44 am    Post subject: does not support special characters Reply with quote

Hi,

First i wanted to say I used the macro to convert my charts in Calc to eps files so that i can include them in a latex document.

It works fine for english-language charts but a big problem is that it does not support characters with accents like "á é í ó ú" or greek letters like lambda.

If you could please update your macro so that it can render them properly it would be of even greater use for the non-english users.

Thanks
Back to top
View user's profile Send private message
alexis779
Newbie
Newbie


Joined: 31 Aug 2008
Posts: 2

PostPosted: Sun Aug 31, 2008 11:15 am    Post subject: without macro Reply with quote

Thanks to https://answers.launchpad.net/ubuntu/+source/openoffice.org/+question/11787.
They say copy paste the chart in Draw and export as EPS.

The difference is that the conversion has to be done by hand and not programatically.
Back to top
View user's profile Send private message
steve@sliderule
Newbie
Newbie


Joined: 14 Sep 2008
Posts: 1

PostPosted: Sun Sep 14, 2008 7:05 am    Post subject: Brilliant Reply with quote

I registered with this form just to say,

Thanks very much I now have decent graphs in my Lyx document without running multiple apps.

Steve. Very Happy
Back to top
View user's profile Send private message
climater
Newbie
Newbie


Joined: 30 Oct 2009
Posts: 2

PostPosted: Fri Oct 30, 2009 5:50 pm    Post subject: Reply with quote

Hello,

Could anyone help to verify if Jose's macro is still working? I tried it in OO Calc 3.1.1 on Windows XP, but it gave me an error message "Action not supported. Invalid procedure call", pointing to the following line of code:

storeUrl = Left( storeUrl, Len( storeUrl ) - 4 )

Thanks,
David
Back to top
View user's profile Send private message
climater
Newbie
Newbie


Joined: 30 Oct 2009
Posts: 2

PostPosted: Fri Oct 30, 2009 6:41 pm    Post subject: Reply with quote

Sorry, please ignore my previous message. For whatever reasons unknown to me, the macro is now working! Thanks, Jose, for this wonderful tool!
Back to top
View user's profile Send private message
RalfG
Newbie
Newbie


Joined: 23 Feb 2010
Posts: 3

PostPosted: Wed Feb 24, 2010 8:29 am    Post subject: Reply with quote

climater wrote:
Hello,

Could anyone help to verify if Jose's macro is still working? I tried it in OO Calc 3.1.1 on Windows XP, but it gave me an error message "Action not supported. Invalid procedure call", pointing to the following line of code:

storeUrl = Left( storeUrl, Len( storeUrl ) - 4 )

Thanks,
David


Hi Dave,

I had the same error - and this was the reason:
Before saving a document, its storeUrl would be an blank string "" (Unnamed).
Now, try to substract the extension (4 chars) from an empty string...!

You can fix this by:

if Len(storeUrl)>3 then
storeUrl = Left( storeUrl, Len( storeUrl ) - 4 )
endif
Back to top
View user's profile Send private message
jrfonseca
Newbie
Newbie


Joined: 26 Jul 2007
Posts: 3

PostPosted: Fri Mar 19, 2010 8:17 am    Post subject: Reply with quote

I was requested to license the above macro, here it is:

/**************************************************************************
*
* Copyright 2007 Jose Fonseca
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
**************************************************************************/
Back to top
View user's profile Send private message
jasper6004
Newbie
Newbie


Joined: 12 Jun 2013
Posts: 1

PostPosted: Wed Jun 12, 2013 5:42 am    Post subject: Reply with quote

Thanks! works perfectly!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group