from com.sun.star.beans import PropertyValue # import uno # doesn't seem necessary # I confess I still don't really understand the structures # in the next 6 lines - I just hacked away until it all worked. desktop = XSCRIPTCONTEXT.getDesktop() ThisComponent = XSCRIPTCONTEXT.getDocument() sheet = ThisComponent.getSheets().getByIndex(0) frame = ThisComponent.getCurrentController().getFrame() ctx = XSCRIPTCONTEXT.getComponentContext() dispatcher = ctx.ServiceManager.createInstanceWithContext( 'com.sun.star.frame.DispatchHelper', ctx ) def StartHere(): for i in range(0,7): # generate a block of unformatted numbers for j in range (7, 10): sheet.getCellByPosition(i, j).setValue(42) FormatColumn("A") FormatColumn("B") FormatColumn("C") FormatColumn("D") FormatColumn("E") # If column C is run last, you can see the entire column is selected # FormatColumn("C") ######################################################################## ######################################################################## # Originally this routine formatted some columns to $xx.xx # I changed it so it writes desired cell location instead. def FormatColumn(column): for i in range(2,7): # create newcell, e.g. 'A3' newcell = column+str(i) # Can't find another way to do this except dispatcher. Ugh. oProp = PropertyValue() # move "cursor" to cell locaton 'newcell' # However if newcell points to column C, the row number # becomes the column number, and the entire column is # selected. For example, "C2" selects the entire B column, # C3 -> C column, C4 -> D column, etc. oProp.Name = 'ToPoint' oProp.Value = newcell properties = (oProp,) dispatcher.executeDispatch( frame, '.uno:GoToCell', '', 0, properties ) # Idea was to set $xx.xx format for selected cell, but # if cell is Ci, it changes the entire column to $xx.xx oProp.Name = 'NumberFormatValue' oProp.Value = 21 # currency properties = (oProp,) dispatcher.executeDispatch( frame, '.uno:NumberFormatValue', '', 0, properties ) # Writes the (intended) cell location to that cell oProp.Name = 'StringName' oProp.Value = newcell properties = (oProp,) dispatcher.executeDispatch( frame, '.uno:EnterString', '', 0, properties ) if __name__ == '__main__': StartHere(1)