Format numbers as '###.##'

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Archdeacon
Posts: 3
Joined: Tue Mar 20, 2012 5:30 am

Format numbers as '###.##'

Post by Archdeacon »

How exactly do I apply this in a macro to set any values to something like 123.45, 234.66, etc?
OpenOffice 3.1 on Windows 7
User avatar
kingfisher
Volunteer
Posts: 2123
Joined: Tue Nov 20, 2007 10:53 am

Re: Format numbers as '###.##'

Post by kingfisher »

Unless you have very large numbers of cells to format, you don't need a macro. Create a cell style using that format and allocate the style a tool bar icon, key binding or even menu selection. If you do need a macro, use one that applies a cell style rather than alter one of the attributes of each cell.

To create a style, use Format > Formatting and Styles (F11) > right-click 'default' and select 'new'. You can also apply a style by selecting cells and double-clicking the style name in the F11 dialogue.
Apache OpenOffice 4.1.9 on Linux
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Format numbers as '###.##'

Post by Charlie Young »

Archdeacon wrote:How exactly do I apply this in a macro to set any values to something like 123.45, 234.66, etc?
Sometimes it is nice to have a macro for this.

Basic:

Code: Select all


Function getformat(f As String) As Long
   Dim oDoc As Object
   Dim NumberFormats As Object
   Dim Loc as New com.sun.star.lang.Locale
   Dim formatID As Long
      
   oDoc = ThisComponent
         
   Loc.Language = "en"
   Loc.Country = "US"
   
   NumberFormats = oDoc.NumberFormats
   
   formatId = NumberFormats.queryKey(f, Loc, False)
   If formatId = -1 Then
      formatId = NumberFormats.addNew(f, Loc)
   End If
   
   getformat  = formatID
   
End Function

And since I have it handy, c++:

[code]
long getformat(Reference< XSpreadsheetDocument > rSheetDoc, char *f)
{
	Reference< XNumberFormatsSupplier > xNumberFormatsSupplier(rSheetDoc, UNO_QUERY);
	Reference< XNumberFormats > xNumberFormats = xNumberFormatsSupplier->getNumberFormats();
   
	Locale Loc;
	long formatId;
          
	Loc.Language = OUString::createFromAscii("en");
	Loc.Country = OUString::createFromAscii("US");
   
   formatId = xNumberFormats->queryKey(OUString::createFromAscii(f), Loc, 0);
   if(formatId == -1)
      formatId = xNumberFormats->addNew(OUString::createFromAscii(f), Loc);
      
   return formatId;
}

[/code]
Apache OpenOffice 4.1.1
Windows XP
Post Reply