Format Range to Number and 2 decimal places

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 ?

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 com.sun.star.lang.Locale
	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

1 Like