7/15/2010

openoffice calc macro : Copy a chart to GDI meta format by python macro


def myAddChart(oDataRangeAddress, oSheet, oChartPositionCell):
tmpChartName = "tmpChartName"
#--------- make a chart
oCharts = oSheet.Charts

X = oChartPositionCell.Position.X
Y = oChartPositionCell.Position.Y

oCharts.addNewByName(tmpChartName, makeRectangle( X, Y, 8000, 3500 ), Array( oDataRangeAddress ), True, True)
oChart = oCharts.getByName(tmpChartName)

#==========================================================
#--------- Copy the Chart to GDI meta image into the position on where the chart is
#==========================================================
oDoc = getReady()
oController = oDoc.getCurrentController()

oDrawPage = oSheet.getDrawPage()
nNumShapes = oDrawPage.getCount()

### select the chart.
### No way for direct selecting a chart. So, get shapes and find the chart from shapes.
for loopi in range(nNumShapes) :
oShape = oDrawPage.getByIndex( loopi )
if "CLSID" in dir(oShape) : # only charts have the CLSID attribute
if oShape.CLSID == "12DCAE26-281F-416F-a234-c3086127382e" : # this is the CLSID of charts
oController.select( oShape )
break

# PropertyValue format=3 means the PasteSpecial format to be the GDI meta file format
PropertyValue = uno.createUnoStruct("com.sun.star.beans.PropertyValue")
PropertyValue.Name = "Format"
PropertyValue.Value = 3

serviceMgr = uno.getComponentContext().ServiceManager
dp = serviceMgr.createInstance('com.sun.star.frame.DispatchHelper')
#--- copy the selected object (chart)
dp.executeDispatch( oController.getFrame(), ".uno:Copy", "", 0, ())
#--- remove the selected object
oCharts.removeByName(tmpChartName)
#--- now select the cell where the GDI meta image will be on
oController.select(oChartPositionCell)
#--- paste
dp.executeDispatch( oController.getFrame(), ".uno:PasteSpecial", "", 0, (PropertyValue,)) # <- The last (PropertyValue,) option is important.




..

댓글 없음: