Ask Your Question

Format Range to Number and 2 decimal places

asked 2017-12-04 00:47:43 +0200

digifoss gravatar image

updated 2017-12-04 01:49:36 +0200

I can define my Range in column D with: myRange = mySheet.GetCellRangeByPosition(3,1,3,1040) How do I format it in Basic ?

I'm new at using LO Basic, where can I find an object model, methods and properties reference ?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2017-12-04 05:01:29 +0200

librebel gravatar image

Hello @digifoss,

To set the desired NumberFormat for a Cell range using LibreOffice Basic, you could use the following Function:

Function cellRange_SetNumberFormat( strSourceRange As String, strNumberFormat As String ) As Long
REM Sets the Number Format of the specified CellRange to <strNumberFormat>.
REM <strNumberFormat> : The desired Number Format specification string (e.g. "0.00").
REM NB. The given Format will be added to the NumberFormats, if it doesn't exist already.
    Dim aLocale As New
    Dim oNumberFormats As Object    : oNumberFormats = ThisComponent.getNumberFormats()
    Dim oSheet As Object    : oSheet = ThisComponent.CurrentController.ActiveSheet
    Dim oRange As Object    : oRange = oSheet.getCellRangebyName( strSourceRange )
    Dim lFormatKey  As Long : lFormatKey = oNumberFormats.queryKey( strNumberFormat, aLocale, FALSE )
    If lFormatKey = -1 Then   lFormatKey = oNumberFormats.addNew( strNumberFormat, aLocale )
    oRange.NumberFormat = lFormatKey
    cellRange_SetNumberFormat = lFormatKey
End Function

For further information please refer to the following sites :

LibreOffice Basic Help ( same as pressing F1 in the IDE )

Programming with LibreOffice Basic

UNO API Reference

image description

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-12-04 00:47:43 +0200

Seen: 844 times

Last updated: Dec 04 '17