7/06/2010

python code snippets for openoffice calc

getReady() -> return oDoc (calc document)
gotoOffset -> similar to Range.Offset(x, y) in excel VBA
gotoLeftEnd -> Range.End(xlToLeft) in VBA for excel


import uno

def gotoOffset(oCell, coffset, roffset):
currentColumn = oCell.getCellAddress().Column
currentRow = oCell.getCellAddress().Row

newColumn = currentColumn + coffset
newRow = currentRow + roffset

if newColumn < 0 : newColumn = 0
if newRow < 0 : newRow = 0

tmpCell = oCell.getSpreadsheet().getCellByPosition(newColumn, newRow)
return tmpCell


def gotoRightEnd(oCell):
tmpCell = oCell
while True :
tmpCell = gotoOffset(tmpCell, 1, 0)
if tmpCell.getString() == '' :
tmpCell = gotoOffset(tmpCell, -1, 0)
break
return tmpCell

def gotoLeftEnd(oCell):
tmpCell = oCell
while True :
tmpCell = gotoOffset(tmpCell, -1, 0)
if tmpCell.getRangeAddress().StartColumn == 0 :
break
if tmpCell.getString() == '' :
tmpCell = gotoOffset(tmpCell, 1, 0)
break
return tmpCell

def gotoTop(oCell):
tmpCell = oCell
while True :
tmpCell = gotoOffset(tmpCell, 0, -1)
if tmpCell.getRangeAddress().StartRow == 0 :
break
if tmpCell.getString() == '' :
tmpCell = gotoOffset(tmpCell, 0, 1)
break
return tmpCell

def gotoBottom(oCell):
tmpCell = oCell
while True :
tmpCell = gotoOffset(tmpCell, 0, 1)
if tmpCell.getString() == '' :
tmpCell = gotoOffset(tmpCell, 0, -1)
break
return tmpCell



def getReady():
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )
ctx = resolver.resolve( "uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
oDoc = desktop.getCurrentComponent()

return oDoc

댓글 없음: